From efe4c4043dcf3b4474d9218a401d8c1798909ec9 Mon Sep 17 00:00:00 2001 From: smilerz Date: Mon, 5 Apr 2021 12:32:11 -0500 Subject: [PATCH] simplified url import --- cookbook/helper/recipe_html_import.py | 1 + cookbook/helper/recipe_url_import.py | 1 + cookbook/templates/url_import.html | 128 ++++---------------- cookbook/urls.py | 1 - cookbook/views/api.py | 165 +++++++++++++------------- 5 files changed, 107 insertions(+), 189 deletions(-) diff --git a/cookbook/helper/recipe_html_import.py b/cookbook/helper/recipe_html_import.py index 6df0ce1a..3bcc5158 100644 --- a/cookbook/helper/recipe_html_import.py +++ b/cookbook/helper/recipe_html_import.py @@ -58,6 +58,7 @@ def get_recipe_from_source(text, url, space): recipe_json = { 'name': '', + 'url': '', 'description': '', 'image': '', 'keywords': [], diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index 7e0eade9..9ac83685 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -102,6 +102,7 @@ def get_from_scraper(scrape, space): recipe_json['recipeInstructions'] = "" if scrape.url: + recipe_json['url'] = scrape.url recipe_json['recipeInstructions'] += "\n\nImported from " + scrape.url return recipe_json diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html index efffe718..65e11f48 100644 --- a/cookbook/templates/url_import.html +++ b/cookbook/templates/url_import.html @@ -22,20 +22,13 @@ {% block content %}
-
-

{% trans 'Import' %}

- - {% trans 'Bookmark Me!' %} -
-
- @@ -177,7 +170,7 @@
-
[[kw]]
+
[[kw.text]]
@@ -273,7 +266,7 @@
- @@ -638,6 +631,7 @@ recipe_app: 'DEFAULT', recipe_files: [], images: [], + mode: 'url' }, directives: { tabindex: { @@ -675,51 +669,20 @@ this.error = undefined this.loading = true this.preview = false - if (this.automatic) { - console.log('true') - } - this.$http.post("{% url 'api_recipe_from_url' %}", {'url': this.remote_url, 'auto':this.automatic}, {emulateJSON: true}).then((response) => { + this.$http.post("{% url 'api_recipe_from_source' %}", { + 'url': this.remote_url, + 'data': this.source_data, + 'auto':this.automatic, + 'mode':this.mode}, {emulateJSON: true}).then((response) => { console.log(response.data) + this.recipe_json = response.data['recipe_json']; + this.recipe_tree = response.data['recipe_tree']; + this.recipe_html = response.data['recipe_html']; + this.images = response.data['images']; if (this.automatic) { - this.recipe_data = response.data; - } else { - this.recipe_json = response.data['recipe_json']; - this.recipe_tree = response.data['recipe_tree']; - this.recipe_html = response.data['recipe_html']; - this.images = response.data['images']; - this.preview = true - } - this.loading = false - }).catch((err) => { - this.error = err.data - this.loading = false - console.log(err) - let msg = gettext('There was an error loading a resource!') - if (err.bodyText.length < 300) { - msg += err.bodyText - } else { - msg += ' ' + err.status + ' ' + err.statusText - } - this.makeToast(gettext('Error'), msg, 'danger') - }) - }, - loadSource: function() { - this.recipe_data = undefined - this.recipe_json = undefined - this.recipe_tree = undefined - this.images = [] - this.error = undefined - this.loading = true - this.preview = false - this.$http.post("{% url 'api_recipe_from_source' %}", {'data': this.source_data, 'url': this.remote_url, 'auto':this.automatic}, {emulateJSON: true}).then((response) => { - console.log(response.data) - if (this.automatic) { - this.recipe_data = response.data['recipe_json']; + this.recipe_data = this.recipe_json; this.preview = false } else { - this.recipe_json = response.data['recipe_json']; - this.recipe_tree = response.data['recipe_tree']; - this.recipe_html = response.data['recipe_html']; this.preview = true } this.loading = false @@ -736,51 +699,9 @@ this.makeToast(gettext('Error'), msg, 'danger') }) }, - loadBookmarklet: function(id_bkmk) { - let uri = window.location.search.substring(1); - let params = new URLSearchParams(uri); - q = params.get("id") - console.log(q) - this.error = undefined - this.loading = true - this.$http.get("{% url 'api:bookmarkletimport-list' %}?id=" + id_bkmk ).then((response) => { - console.log(response.data) - this.automatic = false - this.source_data = response.data[0]['html'] - this.remote_url = response.data[0]['url'] - this.loadSource() - }).catch((err) => { - this.error = err.data - this.loading = false - console.log(err) - this.makeToast(gettext('Error'), gettext('Bookmarklet not found!') , 'danger') - }) - }, - deleteBookmarklet: function(id_bkmk) { - this.error = undefined - this.$http.delete("{% url 'api:bookmarkletimport-list' %}" + id_bkmk +"/").then((response) => { - }).catch((err) => { - this.error = err.data - console.log(err) - this.makeToast(gettext('Error'), gettext('There was an error deleting bookmarklet!') + err.bodyText, 'danger') - }) - }, - loadRecipeManual: function () { - this.error = undefined + showRecipe: function() { this.preview = false - this.loading = true - this.recipe_json['@type'] = "Recipe" - this.$http.post("{% url 'api_recipe_from_source' %}", {'data': JSON.stringify(this.recipe_json), 'auto':'true'}, {emulateJSON: true}).then((response) => { - console.log(response.data) - this.recipe_data = response.data['recipe_json']; - this.loading = false - this.preview = false - }).catch((err) => { - this.error = err.data - this.loading = false - console.log(err) - this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger') - }) + this.recipe_data = this.recipe_json }, importRecipe: function () { if (this.recipe_data.name.length > 128) { @@ -955,7 +876,8 @@ this.recipe_json.image=v break; case 'keywords': - this.recipe_json.keywords.push(v) + let new_keyword = {'text': v, 'id': null} + this.recipe_json.keywords.push(new_keyword) break; case 'servings': this.recipe_json.servings=v diff --git a/cookbook/urls.py b/cookbook/urls.py index f9810aa0..f26ce8b4 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -93,7 +93,6 @@ urlpatterns = [ path('api/sync_all/', api.sync_all, name='api_sync'), path('api/log_cooking//', api.log_cooking, name='api_log_cooking'), path('api/plan-ical///', api.get_plan_ical, name='api_get_plan_ical'), - path('api/recipe-from-url/', api.recipe_from_url, name='api_recipe_from_url'), path('api/recipe-from-source/', api.recipe_from_source, name='api_recipe_from_source'), path('api/backup/', api.get_backup, name='api_backup'), path('api/ingredient-from-string/', api.ingredient_from_string, name='api_ingredient_from_string'), diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 173a6557..d472084f 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -548,100 +548,87 @@ def get_plan_ical(request, from_date, to_date): @group_required('user') -def recipe_from_url(request): - url = request.POST['url'] - if 'auto' in request.POST: - auto = request.POST['auto'] - else: - auto = 'true' +def recipe_from_source(request): + url = request.POST.get('url', None) + data = request.POST.get('data', None) + mode = request.POST.get('mode', None) + auto = request.POST.get('auto', 'true') - if auto == 'false': - headers = { - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36' # noqa: E501 - } - try: - response = requests.get(url, headers=headers) - except requests.exceptions.ConnectionError: - return JsonResponse( - { - 'error': True, - 'msg': _('The requested page could not be found.') - }, - status=400 - ) - - if response.status_code == 403: - return JsonResponse( - { - 'error': True, - 'msg': _('The requested page refused to provide any information (Status Code 403).') # noqa: E501 - }, - status=400 - ) - return recipe_from_source(request, url=url, url_text=response.text) - - try: - scrape = scrape_me(url) - except WebsiteNotImplementedError: - try: - scrape = scrape_me(url, wild_mode=True) - except NoSchemaFoundInWildMode: - return JsonResponse( - { - 'error': True, - 'msg': _('The requested site provided malformed data and cannot be read.') # noqa: E501 - }, - status=400) - except ConnectionError: + if (not url and not data) or (mode == 'url' and not url) or (mode == 'source' and not data): return JsonResponse( { 'error': True, - 'msg': _('The requested page could not be found.') + 'msg': _('Nothing to do.') }, status=400 ) - if len(scrape.schema.data) == 0: - return JsonResponse( - { - 'error': True, - 'msg': _('The requested site does not provide any recognized data format to import the recipe from.') # noqa: E501 - }, - status=400) - else: - return JsonResponse(get_from_scraper(scrape, request.space)) - -@group_required('user') -def recipe_from_source(request, url=None, url_text=None): - if url_text: - json_data = url_text - else: - json_data = request.POST['data'] - if 'auto' in request.POST: - auto = request.POST['auto'] - else: - auto = 'true' - if 'url' in request.POST: - url = request.POST['url'] - - recipe_json, recipe_tree, recipe_html, images = get_recipe_from_source(json_data, url, request.space) - if len(recipe_tree) == 0 and len(recipe_json) == 0: - return JsonResponse( - { - 'error': True, - 'msg': _('No useable data could be found.') # noqa: E501 - }, - status=400 - ) - else: - if auto == "true": - return JsonResponse({'recipe_json': recipe_json}) + if mode == 'url': + if auto == 'true': + try: + scrape = scrape_me(url) + except WebsiteNotImplementedError: + try: + scrape = scrape_me(url, wild_mode=True) + except NoSchemaFoundInWildMode: + return JsonResponse( + { + 'error': True, + 'msg': _('The requested site provided malformed data and cannot be read.') # noqa: E501 + }, + status=400) + except ConnectionError: + return JsonResponse( + { + 'error': True, + 'msg': _('The requested page could not be found.') + }, + status=400 + ) + if len(scrape.schema.data) == 0: + return JsonResponse( + { + 'error': True, + 'msg': _('The requested site does not provide any recognized data format to import the recipe from.') # noqa: E501 + }, + status=400) + else: + return JsonResponse(get_from_scraper(scrape, request.space)) + else: + headers = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36' # noqa: E501 + } + try: + response = requests.get(url, headers=headers) + except requests.exceptions.ConnectionError: + return JsonResponse( + { + 'error': True, + 'msg': _('The requested page could not be found.') + }, + status=400 + ) + + if response.status_code == 403: + return JsonResponse( + { + 'error': True, + 'msg': _('The requested page refused to provide any information (Status Code 403).') + }, + status=400 + ) + data = response.text + if (mode == 'source') or (mode == 'url' and auto == 'false'): + recipe_json, recipe_tree, recipe_html, images = get_recipe_from_source(data, url, request.space) + if len(recipe_tree) == 0 and len(recipe_json) == 0: + return JsonResponse( + { + 'error': True, + 'msg': _('No useable data could be found.') + }, + status=400 + ) else: - # overide keyword structure from dict to list - kws = [] - for kw in recipe_json['keywords']: - kws.append(kw['text']) - recipe_json['keywords'] = kws return JsonResponse({ 'recipe_tree': recipe_tree, 'recipe_json': recipe_json, @@ -649,6 +636,14 @@ def recipe_from_source(request, url=None, url_text=None): 'images': images, }) + return JsonResponse( + { + 'error': True, + 'msg': _('I couldn\'t find anything to do.') + }, + status=400 + ) + @group_required('admin') def get_backup(request):