youtube_noapi.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Youtube (Videos)
  4. """
  5. from functools import reduce
  6. from json import loads
  7. from urllib.parse import quote_plus
  8. # about
  9. about = {
  10. "website": 'https://www.youtube.com/',
  11. "wikidata_id": 'Q866',
  12. "official_api_documentation": 'https://developers.google.com/youtube/v3/docs/search/list?apix=true',
  13. "use_official_api": False,
  14. "require_api_key": False,
  15. "results": 'HTML',
  16. }
  17. # engine dependent config
  18. categories = ['videos', 'music']
  19. paging = True
  20. time_range_support = True
  21. # search-url
  22. base_url = 'https://www.youtube.com/results'
  23. search_url = base_url + '?search_query={query}&page={page}'
  24. time_range_url = '&sp=EgII{time_range}%253D%253D'
  25. time_range_dict = {'day': 'Ag',
  26. 'week': 'Aw',
  27. 'month': 'BA',
  28. 'year': 'BQ'}
  29. embedded_url = '<iframe width="540" height="304" ' +\
  30. 'data-src="https://www.youtube-nocookie.com/embed/{videoid}" ' +\
  31. 'frameborder="0" allowfullscreen></iframe>'
  32. base_youtube_url = 'https://www.youtube.com/watch?v='
  33. # do search-request
  34. def request(query, params):
  35. params['url'] = search_url.format(query=quote_plus(query),
  36. page=params['pageno'])
  37. if params['time_range'] in time_range_dict:
  38. params['url'] += time_range_url.format(time_range=time_range_dict[params['time_range']])
  39. return params
  40. # get response from search-request
  41. def response(resp):
  42. results = []
  43. results_data = resp.text[resp.text.find('ytInitialData'):]
  44. results_data = results_data[results_data.find('{'):results_data.find(';</script>')]
  45. results_json = loads(results_data) if results_data else {}
  46. sections = results_json.get('contents', {})\
  47. .get('twoColumnSearchResultsRenderer', {})\
  48. .get('primaryContents', {})\
  49. .get('sectionListRenderer', {})\
  50. .get('contents', [])
  51. for section in sections:
  52. for video_container in section.get('itemSectionRenderer', {}).get('contents', []):
  53. video = video_container.get('videoRenderer', {})
  54. videoid = video.get('videoId')
  55. if videoid is not None:
  56. url = base_youtube_url + videoid
  57. thumbnail = 'https://i.ytimg.com/vi/' + videoid + '/hqdefault.jpg'
  58. title = get_text_from_json(video.get('title', {}))
  59. content = get_text_from_json(video.get('descriptionSnippet', {}))
  60. embedded = embedded_url.format(videoid=videoid)
  61. author = get_text_from_json(video.get('ownerText', {}))
  62. length = get_text_from_json(video.get('lengthText', {}))
  63. # append result
  64. results.append({'url': url,
  65. 'title': title,
  66. 'content': content,
  67. 'author': author,
  68. 'length': length,
  69. 'template': 'videos.html',
  70. 'embedded': embedded,
  71. 'thumbnail': thumbnail})
  72. # return results
  73. return results
  74. def get_text_from_json(element):
  75. if 'runs' in element:
  76. return reduce(lambda a, b: a + b.get('text', ''), element.get('runs'), '')
  77. else:
  78. return element.get('simpleText', '')