logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

streamcz.py (5104B)


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