simplified url import
This commit is contained in:
@ -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):
|
||||
|
Reference in New Issue
Block a user