updated bookmarklet to work with updated import process

This commit is contained in:
smilerz
2021-04-14 11:29:35 -05:00
parent faf458e8ef
commit 018fa0a62f
5 changed files with 24 additions and 8 deletions

View File

@ -73,11 +73,9 @@ def get_recipe_from_source(text, url, space):
html_data = [] html_data = []
images = [] images = []
# text = normalize_string(text)
try: try:
parse_list.append(remove_graph(json.loads(text))) parse_list.append(remove_graph(json.loads(text)))
if not url and 'url' in parse_list[0]:
url = parse_list[0]['url']
scrape = text_scraper("<script type='application/ld+json'>" + text + "</script>", url=url)
except JSONDecodeError: except JSONDecodeError:
soup = BeautifulSoup(text, "html.parser") soup = BeautifulSoup(text, "html.parser")
@ -85,8 +83,6 @@ def get_recipe_from_source(text, url, space):
images += get_images_from_source(soup, url) images += get_images_from_source(soup, url)
for el in soup.find_all('script', type='application/ld+json'): for el in soup.find_all('script', type='application/ld+json'):
el = remove_graph(el) el = remove_graph(el)
if not url and 'url' in el:
url = el['url']
if type(el) == list: if type(el) == list:
for le in el: for le in el:
parse_list.append(le) parse_list.append(le)
@ -99,6 +95,15 @@ def get_recipe_from_source(text, url, space):
parse_list.append(le) parse_list.append(le)
elif type(el) == dict: elif type(el) == dict:
parse_list.append(el) parse_list.append(el)
# if a url was not provided, try to find one in the first document
if not url and len(parse_list) > 0:
if 'url' in parse_list[0]:
url = parse_list[0]['url']
if type(text) == dict:
scrape = text_scraper("<script type='application/ld+json'>" + text + "</script>", url=url)
elif type(text) == str:
scrape = text_scraper(text, url=url) scrape = text_scraper(text, url=url)
recipe_json = helper.get_from_scraper(scrape, space) recipe_json = helper.get_from_scraper(scrape, space)

View File

@ -30,7 +30,7 @@ def text_scraper(text, url=None):
url=None url=None
): ):
self.wild_mode = False self.wild_mode = False
self.exception_handling = None self.exception_handling = _exception_handling
self.meta_http_equiv = False self.meta_http_equiv = False
self.soup = BeautifulSoup(page_data, "html.parser") self.soup = BeautifulSoup(page_data, "html.parser")
self.url = url self.url = url

View File

@ -44,4 +44,4 @@
} }
)(); )();
} }
})(); })();

View File

@ -1,6 +1,7 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load i18n %} {% load i18n %}
{% load static %} {% load static %}
{% load custom_tags %}
{% block title %}{% trans 'URL Import' %}{% endblock %} {% block title %}{% trans 'URL Import' %}{% endblock %}
@ -22,7 +23,14 @@
{% block content %} {% block content %}
<div id="app"> <div id="app">
<h2> {% trans 'Import' %} </h2> <div class="row px-3" style="justify-content:space-between;">
<h2> {% trans 'Import' %}</h2>
<a class="btn btn-outline-info btn-sm"
style="height:50%"
href="{% bookmarklet request %}"
title="{% trans 'Drag me to your bookmarks to import recipes from anywhere' %}">
<img src="{% static 'assets/favicon-16x16.png' %}">{% trans 'Bookmark Me!' %} </a>
</div>
<nav class="nav nav-pills flex-sm-row" style="margin-bottom:10px"> <nav class="nav nav-pills flex-sm-row" style="margin-bottom:10px">
<a class="nav-link active" href="#nav-url" data-toggle="tab" role="tab" aria-controls="nav-url" aria-selected="true" @click="mode='url'">URL</a> <a class="nav-link active" href="#nav-url" data-toggle="tab" role="tab" aria-controls="nav-url" aria-selected="true" @click="mode='url'">URL</a>
<a class="nav-link" href="#nav-app" data-toggle="tab" role="tab" aria-controls="nav-app" @click="mode='app'">App</a> <a class="nav-link" href="#nav-app" data-toggle="tab" role="tab" aria-controls="nav-app" @click="mode='app'">App</a>

View File

@ -1,13 +1,16 @@
import bleach import bleach
import markdown as md import markdown as md
import re
from bleach_allowlist import markdown_attrs, markdown_tags from bleach_allowlist import markdown_attrs, markdown_tags
from cookbook.helper.mdx_attributes import MarkdownFormatExtension from cookbook.helper.mdx_attributes import MarkdownFormatExtension
from cookbook.helper.mdx_urlize import UrlizeExtension from cookbook.helper.mdx_urlize import UrlizeExtension
from cookbook.models import Space, get_model_name from cookbook.models import Space, get_model_name
from django import template from django import template
from django.db.models import Avg from django.db.models import Avg
from django.templatetags.static import static
from django.urls import NoReverseMatch, reverse from django.urls import NoReverseMatch, reverse
from recipes import settings from recipes import settings
from rest_framework.authtoken.models import Token
from gettext import gettext as _ from gettext import gettext as _
register = template.Library() register = template.Library()