streamcz.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # coding: utf-8
  2. import json
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. float_or_none,
  7. int_or_none,
  8. parse_codecs,
  9. traverse_obj,
  10. urljoin,
  11. )
  12. class StreamCZIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:www\.)?(?:stream|televizeseznam)\.cz/[^?#]+/(?P<display_id>[^?#]+)-(?P<id>[0-9]+)'
  14. _TESTS = [{
  15. 'url': 'https://www.televizeseznam.cz/video/lajna/buh-57953890',
  16. 'md5': '40c41ade1464a390a0b447e333df4239',
  17. 'info_dict': {
  18. 'id': '57953890',
  19. 'ext': 'mp4',
  20. 'title': 'Bůh',
  21. 'display_id': 'buh',
  22. 'description': 'md5:8f5f09b9b7bc67df910486cdd88f7165',
  23. }
  24. }, {
  25. 'url': 'https://www.stream.cz/tajemno/znicehonic-jim-skrz-strechu-prolitnul-zahadny-predmet-badatele-vse-objasnili-64147267',
  26. 'md5': '3ee4d0be040e8f4a543e67e509d55e3f',
  27. 'info_dict': {
  28. 'id': '64147267',
  29. 'ext': 'mp4',
  30. 'title': 'Zničehonic jim skrz střechu prolítnul záhadný předmět. Badatelé vše objasnili',
  31. 'display_id': 'znicehonic-jim-skrz-strechu-prolitnul-zahadny-predmet-badatele-vse-objasnili',
  32. 'description': 'md5:1dcb5e010eb697dedc5942f76c5b3744',
  33. }
  34. }]
  35. def _extract_formats(self, spl_url, video):
  36. for ext, pref, streams in (
  37. ('ts', -1, traverse_obj(video, ('http_stream', 'qualities'))),
  38. ('mp4', 1, video.get('mp4'))):
  39. for format_id, stream in streams.items():
  40. if not stream.get('url'):
  41. continue
  42. yield {
  43. 'format_id': f'{format_id}-{ext}',
  44. 'ext': ext,
  45. 'source_preference': pref,
  46. 'url': urljoin(spl_url, stream['url']),
  47. 'tbr': float_or_none(stream.get('bandwidth'), scale=1000),
  48. 'duration': float_or_none(stream.get('duration'), scale=1000),
  49. 'width': traverse_obj(stream, ('resolution', 0)),
  50. 'height': traverse_obj(stream, ('resolution', 1)) or int_or_none(format_id.replace('p', '')),
  51. **parse_codecs(stream.get('codec')),
  52. }
  53. def _real_extract(self, url):
  54. display_id, video_id = re.match(self._VALID_URL, url).groups()
  55. data = self._download_json(
  56. 'https://www.televizeseznam.cz/api/graphql', video_id, 'Downloading GraphQL result',
  57. data=json.dumps({
  58. 'variables': {'urlName': video_id},
  59. 'query': '''
  60. query LoadEpisode($urlName : String){ episode(urlName: $urlName){ ...VideoDetailFragmentOnEpisode } }
  61. fragment VideoDetailFragmentOnEpisode on Episode {
  62. id
  63. spl
  64. urlName
  65. name
  66. perex
  67. duration
  68. views
  69. }'''
  70. }).encode('utf-8'),
  71. headers={'Content-Type': 'application/json;charset=UTF-8'}
  72. )['data']['episode']
  73. spl_url = data['spl'] + 'spl2,3'
  74. metadata = self._download_json(spl_url, video_id, 'Downloading playlist')
  75. if 'Location' in metadata and 'data' not in metadata:
  76. spl_url = metadata['Location']
  77. metadata = self._download_json(spl_url, video_id, 'Downloading redirected playlist')
  78. video = metadata['data']
  79. subtitles = {}
  80. for subs in video.get('subtitles', {}).values():
  81. if not subs.get('language'):
  82. continue
  83. for ext, sub_url in subs.get('urls').items():
  84. subtitles.setdefault(subs['language'], []).append({
  85. 'ext': ext,
  86. 'url': urljoin(spl_url, sub_url)
  87. })
  88. formats = list(self._extract_formats(spl_url, video))
  89. self._sort_formats(formats)
  90. return {
  91. 'id': video_id,
  92. 'display_id': display_id,
  93. 'title': data.get('name'),
  94. 'description': data.get('perex'),
  95. 'duration': float_or_none(data.get('duration')),
  96. 'view_count': int_or_none(data.get('views')),
  97. 'formats': formats,
  98. 'subtitles': subtitles,
  99. }