diff --git a/cookbook/helper/recipe_html_import.py b/cookbook/helper/recipe_html_import.py
index 427d07b95..1ebe1d27b 100644
--- a/cookbook/helper/recipe_html_import.py
+++ b/cookbook/helper/recipe_html_import.py
@@ -7,10 +7,7 @@ from bs4.element import Tag
from cookbook.helper import recipe_url_import as helper
-# %%
-
-# %%
-def get_from_raw(text, space):
+def get_recipe_from_source(text, space):
def build_node(k, v):
if isinstance(v, dict):
node = {
@@ -113,17 +110,20 @@ def get_from_raw(text, space):
if '@graph' in el:
for x in el['@graph']:
if '@type' in x and x['@type'] == 'Recipe':
- recipe_json = helper.find_recipe_json(x, None, space)
- recipe_tree += [{'name': 'ld+json', 'children': temp_tree}]
+ el = x
+
+ if '@type' in el and el['@type'] == 'Recipe':
+ recipe_json = helper.find_recipe_json(el, None, space)
+ recipe_tree += [{'name': 'ld+json', 'children': temp_tree}]
else:
recipe_tree += [{'name': 'json', 'children': temp_tree}]
temp_tree = []
- # overide keyword structure from dict to list
- kws = []
- for kw in recipe_json['keywords']:
- kws.append(kw['text'])
- recipe_json['keywords'] = kws
-
return recipe_json, recipe_tree
+
+
+def get_from_html(text, space):
+ for s in soup.strings:
+ if ((s.parent.name not in INVISIBLE_ELEMS) and (len(s.strip()) > 0)):
+ print(s.parent.name, s, len(s))
\ No newline at end of file
diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py
index 9ab62e44a..cfe3c13be 100644
--- a/cookbook/helper/recipe_url_import.py
+++ b/cookbook/helper/recipe_url_import.py
@@ -14,8 +14,8 @@ from recipe_scrapers._schemaorg import SchemaOrgException
from recipe_scrapers._utils import get_minutes
-def get_from_scraper(scrape, space):
- # converting the scrape_me object to the existing json format based on ld+json
+def get_from_html_old(html_text, url, space):
+ soup = BeautifulSoup(html_text, "html.parser")
# first try finding ld+json as its most common
for ld in soup.find_all('script', type='application/ld+json'):
diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html
index a4d747f62..460a38151 100644
--- a/cookbook/templates/url_import.html
+++ b/cookbook/templates/url_import.html
@@ -25,72 +25,54 @@
{% trans 'Import' %}
URL
- ld+json
- json
- HTML
- text
- PDF
+ Source
+ Text
+ File
-
+
+
+
+ Automatic
+
-
-
-
-
-
-
-
-
{% trans 'Import' %}
+
+ Manual
+
+
+
-
-
-
-
-
-
-
-
-
{% trans 'Preview Import' %}
-
-
-
-
-
-
-
-
-
{% trans 'Preview Import' %}
-
-
-
+
+
+
+
+ Automatic
+
+
+ Manual
+
+
+
+
+
+
+
{% trans 'Import' %}
+
+
@@ -101,9 +83,7 @@
-
-
-
+
@@ -116,6 +96,7 @@
@@ -232,11 +213,13 @@
-
+ {% trans 'Import' %}
+
+
-
@@ -518,7 +501,7 @@
el: '#app',
data: {
remote_url: '',
- raw_data: undefined,
+ source_data: undefined,
keywords: [],
keywords_loading: false,
units: [],
@@ -528,11 +511,12 @@
recipe_data: undefined,
error: undefined,
loading: false,
- parsed: false,
+ preview: false,
all_keywords: false,
importing_recipe: false,
recipe_json: undefined,
recipe_tree: undefined,
+ automatic: true
},
directives: {
tabindex: {
@@ -564,12 +548,10 @@
this.images = []
this.error = undefined
this.loading = true
- this.preview = false
- 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) => {
+ if (this.automatic) {
+ console.log('true')
+ }
+ this.$http.post("{% url 'api_recipe_from_url' %}", {'url': this.remote_url}, {emulateJSON: true}).then((response) => {
console.log(response.data)
this.recipe_json = response.data['recipe_json'];
this.recipe_tree = response.data['recipe_tree'];
@@ -595,53 +577,43 @@
this.makeToast(gettext('Error'), msg, 'danger')
})
},
- loadRecipeJson: function () {
+ loadSource: function() {
this.recipe_data = undefined
- this.error = undefined
- this.loading = true
- this.$http.post("{% url 'api_recipe_from_json' %}", {'json': this.raw_data}, {emulateJSON: true}).then((response) => {
- console.log(response.data)
- this.recipe_data = response.data;
- this.loading = 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')
- })
- },
- loadRecipeRaw: function () {
- this.error = undefined
- this.loading = true
- this.parsed = false
- this.recipe_json['@type'] = "Recipe"
- this.$http.post("{% url 'api_recipe_from_json' %}", {'json': JSON.stringify(this.recipe_json)}, {emulateJSON: true}).then((response) => {
- console.log(response.data)
- this.recipe_data = response.data;
- this.loading = 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')
- })
- },
- loadPreviewRaw: function () {
this.recipe_json = undefined
this.recipe_tree = undefined
this.error = undefined
-
this.loading = true
- this.$http.post("{% url 'api_manual_recipe_from_json' %}", {'data': this.raw_data}, {emulateJSON: true}).then((response) => {
+ this.$http.post("{% url 'api_recipe_from_source' %}", {'data': this.source_data, 'auto':this.automatic}, {emulateJSON: true}).then((response) => {
console.log(response.data)
- this.recipe_json = response.data['recipe_json'];
- this.recipe_tree = response.data['recipe_tree'];
+ if (this.automatic) {
+ this.recipe_data = response.data['recipe_json'];
+ this.preview = false
+ } else {
+ this.recipe_json = response.data['recipe_json'];
+ this.recipe_tree = response.data['recipe_tree'];
+ this.preview = true
+ }
this.loading = false
- this.parsed = true
}).catch((err) => {
this.error = err.data
this.loading = false
- this.parsed = false
+ console.log(err)
+ this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger')
+ })
+ },
+ loadRecipeManual: function () {
+ this.error = undefined
+ 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')
})
@@ -869,16 +841,6 @@
break;
}
},
- parseIngredient: function(txt) {
- this.$http.post('{% url 'api_ingredient_from_string' %}', {text: txt}, {emulateJSON: true}).then((response) => {
- console.log(response)
- let ing = [response.body.amount, response.body.unit, response.body.food, response.body.note]
- return ing
- }).catch((err) => {
- console.log(err)
- this.makeToast(gettext('Error'), gettext('Something went wrong.'), 'danger')
- })
- }
}
});
diff --git a/cookbook/urls.py b/cookbook/urls.py
index 062fe7a44..246912be2 100644
--- a/cookbook/urls.py
+++ b/cookbook/urls.py
@@ -92,9 +92,8 @@ 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-html/', api.manual_recipe_from_json, name='api_manual_recipe_from_json'),
- path('api/recipe-from-json/', api.recipe_from_json, name='api_recipe_from_json'),
+ 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 e15fcadd5..f816bea4c 100644
--- a/cookbook/views/api.py
+++ b/cookbook/views/api.py
@@ -35,8 +35,8 @@ from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest,
CustomIsOwner, CustomIsShare,
CustomIsShared, CustomIsUser,
group_required)
-from cookbook.helper.recipe_url_import import get_from_html, find_recipe_json
-from cookbook.helper.recipe_html_import import get_from_raw
+from cookbook.helper.recipe_html_import import get_recipe_from_source
+from cookbook.helper.recipe_url_import import get_from_scraper, find_recipe_json
from cookbook.models import (CookLog, Food, Ingredient, Keyword, MealPlan,
MealType, Recipe, RecipeBook, ShoppingList,
ShoppingListEntry, ShoppingListRecipe, Step,
@@ -706,9 +706,11 @@ def recipe_from_url(request):
@group_required('user')
-def manual_recipe_from_json(request):
+def recipe_from_source(request):
json_data = request.POST['data']
- recipe_json, recipe_tree = get_from_raw(json_data, request.space)
+ auto = request.POST['auto']
+
+ recipe_json, recipe_tree = get_recipe_from_source(json_data, request.space)
if len(recipe_tree) == 0 and len(recipe_json) == 0:
return JsonResponse(
{
@@ -718,10 +720,18 @@ def manual_recipe_from_json(request):
status=400
)
else:
- return JsonResponse({
- 'recipe_tree': recipe_tree,
- 'recipe_json': recipe_json
- })
+ if auto == "true":
+ return JsonResponse({'recipe_json': recipe_json})
+ 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
+ })
@group_required('admin')