added youtube import

This commit is contained in:
vabene1111 2022-06-14 21:37:59 +02:00
parent b365f9adc1
commit 355f181574
3 changed files with 46 additions and 2 deletions

View File

@ -1,6 +1,8 @@
import random
import re
from html import unescape
from pytube import YouTube
from unicodedata import decomposition
from django.utils.dateparse import parse_duration
@ -117,7 +119,7 @@ def get_from_scraper(scrape, request):
try:
source_url = scrape.canonical_url()
except Exception:
try:
try:
source_url = scrape.url
except Exception:
pass
@ -183,6 +185,38 @@ def get_from_scraper(scrape, request):
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):
if type(name) == list:
try:

View File

@ -48,6 +48,7 @@ from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest, Cus
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_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.models import (Automation, BookmarkletImport, CookLog, CustomFilter, ExportLog, Food,
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
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:
if validators.url(serializer.validated_data['url'], public=True):
serializer.validated_data['data'] = requests.get(serializer.validated_data['url'], headers=external_request_headers).content

View File

@ -43,4 +43,5 @@ python-ldap==3.4.0
django-auth-ldap==4.1.0
pytest-factoryboy==2.4.0
pyppeteer==1.0.2
validators==0.19.0
validators==0.19.0
pytube==12.0.0