streamcz.py 4.9 KB

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