hrfensehen.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import json
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. traverse_obj,
  7. try_call,
  8. unescapeHTML,
  9. unified_timestamp,
  10. )
  11. class HRFernsehenIE(InfoExtractor):
  12. IE_NAME = 'hrfernsehen'
  13. _VALID_URL = r'^https?://www\.(?:hr-fernsehen|hessenschau)\.de/.*,video-(?P<id>[0-9]{6})\.html'
  14. _TESTS = [{
  15. 'url': 'https://www.hessenschau.de/tv-sendung/hessenschau-vom-26082020,video-130546.html',
  16. 'md5': '5c4e0ba94677c516a2f65a84110fc536',
  17. 'info_dict': {
  18. 'id': '130546',
  19. 'ext': 'mp4',
  20. 'description': 'Sturmtief Kirsten fegt über Hessen / Die Corona-Pandemie – eine Chronologie / '
  21. 'Sterbehilfe: Die Lage in Hessen / Miss Hessen leitet zwei eigene Unternehmen / '
  22. 'Pop-Up Museum zeigt Schwarze Unterhaltung und Black Music',
  23. 'subtitles': {'de': [{
  24. 'url': 'https://hr-a.akamaihd.net/video/as/hessenschau/2020_08/hrLogo_200826200407_L385592_512x288-25p-500kbit.vtt',
  25. }]},
  26. 'timestamp': 1598400000,
  27. 'upload_date': '20200826',
  28. 'thumbnail': 'https://www.hessenschau.de/tv-sendung/hs_ganz-1554~_t-1598465545029_v-16to9.jpg',
  29. 'title': 'hessenschau vom 26.08.2020',
  30. 'duration': 1654,
  31. },
  32. }, {
  33. 'url': 'https://www.hr-fernsehen.de/sendungen-a-z/mex/sendungen/fair-und-gut---was-hinter-aldis-eigenem-guetesiegel-steckt,video-130544.html',
  34. 'only_matching': True,
  35. }]
  36. _GEO_COUNTRIES = ['DE']
  37. def extract_formats(self, loader_data):
  38. stream_formats = []
  39. data = loader_data['mediaCollection']['streams'][0]['media']
  40. for inner in data[1:]:
  41. stream_format = {
  42. 'format_id': try_call(lambda: f'{inner["maxHResolutionPx"]}p'),
  43. 'height': inner.get('maxHResolutionPx'),
  44. 'url': inner['url'],
  45. }
  46. quality_information = re.search(r'([0-9]{3,4})x([0-9]{3,4})-([0-9]{2})p-([0-9]{3,4})kbit',
  47. inner['url'])
  48. if quality_information:
  49. stream_format['width'] = int_or_none(quality_information.group(1))
  50. stream_format['height'] = int_or_none(quality_information.group(2))
  51. stream_format['fps'] = int_or_none(quality_information.group(3))
  52. stream_format['tbr'] = int_or_none(quality_information.group(4))
  53. stream_formats.append(stream_format)
  54. return stream_formats
  55. def _real_extract(self, url):
  56. video_id = self._match_id(url)
  57. webpage = self._download_webpage(url, video_id)
  58. title = self._html_search_meta(
  59. ['og:title', 'twitter:title', 'name'], webpage)
  60. description = self._html_search_meta(
  61. ['description'], webpage)
  62. loader_str = unescapeHTML(self._search_regex(r"data-(?:new-)?hr-mediaplayer-loader='([^']*)'", webpage, 'ardloader'))
  63. loader_data = json.loads(loader_str)
  64. subtitle = traverse_obj(loader_data, ('mediaCollection', 'subTitles', 0, 'sources', 0, 'url'))
  65. return {
  66. 'id': video_id,
  67. 'title': title,
  68. 'description': description,
  69. 'formats': self.extract_formats(loader_data),
  70. 'subtitles': {'de': [{'url': subtitle}]},
  71. 'timestamp': unified_timestamp(self._search_regex(
  72. r'<time\sdatetime="(\d{4}\W\d{1,2}\W\d{1,2})', webpage, 'datetime', fatal=False)),
  73. 'duration': int_or_none(traverse_obj(
  74. loader_data, ('playerConfig', 'pluginData', 'trackingAti@all', 'richMedia', 'duration'))),
  75. 'thumbnail': self._search_regex(r'thumbnailUrl\W*([^"]+)', webpage, 'thumbnail', default=None),
  76. }