added youtube import
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
from html import unescape
|
from html import unescape
|
||||||
|
|
||||||
|
from pytube import YouTube
|
||||||
from unicodedata import decomposition
|
from unicodedata import decomposition
|
||||||
|
|
||||||
from django.utils.dateparse import parse_duration
|
from django.utils.dateparse import parse_duration
|
||||||
@ -117,7 +119,7 @@ def get_from_scraper(scrape, request):
|
|||||||
try:
|
try:
|
||||||
source_url = scrape.canonical_url()
|
source_url = scrape.canonical_url()
|
||||||
except Exception:
|
except Exception:
|
||||||
try:
|
try:
|
||||||
source_url = scrape.url
|
source_url = scrape.url
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@ -183,6 +185,38 @@ def get_from_scraper(scrape, request):
|
|||||||
return recipe_json
|
return recipe_json
|
||||||
|
|
||||||
|
|
||||||
|
def get_from_youtube_scraper(url, request):
|
||||||
|
"""A YouTube Information Scraper."""
|
||||||
|
kw, created = Keyword.objects.get_or_create(name='YouTube', space=request.space)
|
||||||
|
default_recipe_json = {
|
||||||
|
'name': '',
|
||||||
|
'internal': True,
|
||||||
|
'description': '',
|
||||||
|
'servings': 1,
|
||||||
|
'working_time': 0,
|
||||||
|
'waiting_time': 0,
|
||||||
|
'image': "",
|
||||||
|
'keywords': [{'name': kw.name,'label': kw.name, 'id': kw.pk}],
|
||||||
|
'source_url': url,
|
||||||
|
'steps': [
|
||||||
|
{
|
||||||
|
'ingredients': [],
|
||||||
|
'instruction': ''
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
video = YouTube(url=url)
|
||||||
|
default_recipe_json['name'] = video.title
|
||||||
|
default_recipe_json['image'] = video.thumbnail_url
|
||||||
|
default_recipe_json['steps'][0]['instruction'] = video.description
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return default_recipe_json
|
||||||
|
|
||||||
|
|
||||||
def parse_name(name):
|
def parse_name(name):
|
||||||
if type(name) == list:
|
if type(name) == list:
|
||||||
try:
|
try:
|
||||||
|
@ -48,6 +48,7 @@ from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest, Cus
|
|||||||
group_required, CustomIsSpaceOwner, switch_user_active_space, is_space_owner, CustomIsOwnerReadOnly)
|
group_required, CustomIsSpaceOwner, switch_user_active_space, is_space_owner, CustomIsOwnerReadOnly)
|
||||||
from cookbook.helper.recipe_html_import import get_recipe_from_source
|
from cookbook.helper.recipe_html_import import get_recipe_from_source
|
||||||
from cookbook.helper.recipe_search import RecipeFacet, RecipeSearch, old_search
|
from cookbook.helper.recipe_search import RecipeFacet, RecipeSearch, old_search
|
||||||
|
from cookbook.helper.recipe_url_import import get_from_youtube_scraper
|
||||||
from cookbook.helper.shopping_helper import RecipeShoppingEditor, shopping_helper
|
from cookbook.helper.shopping_helper import RecipeShoppingEditor, shopping_helper
|
||||||
from cookbook.models import (Automation, BookmarkletImport, CookLog, CustomFilter, ExportLog, Food,
|
from cookbook.models import (Automation, BookmarkletImport, CookLog, CustomFilter, ExportLog, Food,
|
||||||
FoodInheritField, ImportLog, Ingredient, Keyword, MealPlan, MealType,
|
FoodInheritField, ImportLog, Ingredient, Keyword, MealPlan, MealType,
|
||||||
@ -1135,6 +1136,14 @@ def recipe_from_source(request):
|
|||||||
|
|
||||||
# in manual mode request complete page to return it later
|
# in manual mode request complete page to return it later
|
||||||
if 'url' in serializer.validated_data:
|
if 'url' in serializer.validated_data:
|
||||||
|
if re.match('^(https?://)?(www\.youtube\.com|youtu\.be)/.+$', serializer.validated_data['url']):
|
||||||
|
if validators.url(serializer.validated_data['url'], public=True):
|
||||||
|
return Response({
|
||||||
|
'recipe_json': get_from_youtube_scraper(serializer.validated_data['url'], request),
|
||||||
|
'recipe_tree': '',
|
||||||
|
'recipe_html': '',
|
||||||
|
'recipe_images': [],
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
try:
|
try:
|
||||||
if validators.url(serializer.validated_data['url'], public=True):
|
if validators.url(serializer.validated_data['url'], public=True):
|
||||||
serializer.validated_data['data'] = requests.get(serializer.validated_data['url'], headers=external_request_headers).content
|
serializer.validated_data['data'] = requests.get(serializer.validated_data['url'], headers=external_request_headers).content
|
||||||
|
@ -43,4 +43,5 @@ python-ldap==3.4.0
|
|||||||
django-auth-ldap==4.1.0
|
django-auth-ldap==4.1.0
|
||||||
pytest-factoryboy==2.4.0
|
pytest-factoryboy==2.4.0
|
||||||
pyppeteer==1.0.2
|
pyppeteer==1.0.2
|
||||||
validators==0.19.0
|
validators==0.19.0
|
||||||
|
pytube==12.0.0
|
Reference in New Issue
Block a user