dvtv.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. determine_ext,
  6. int_or_none,
  7. join_nonempty,
  8. js_to_json,
  9. mimetype2ext,
  10. parse_iso8601,
  11. try_get,
  12. unescapeHTML,
  13. )
  14. class DVTVIE(InfoExtractor):
  15. IE_NAME = 'dvtv'
  16. IE_DESC = 'http://video.aktualne.cz/'
  17. _VALID_URL = r'https?://video\.aktualne\.cz/(?:[^/]+/)+r~(?P<id>[0-9a-f]{32})'
  18. _TESTS = [{
  19. 'url': 'http://video.aktualne.cz/dvtv/vondra-o-ceskem-stoleti-pri-pohledu-na-havla-mi-bylo-trapne/r~e5efe9ca855511e4833a0025900fea04/',
  20. 'md5': '67cb83e4a955d36e1b5d31993134a0c2',
  21. 'info_dict': {
  22. 'id': 'dc0768de855511e49e4b0025900fea04',
  23. 'ext': 'mp4',
  24. 'title': 'Vondra o Českém století: Při pohledu na Havla mi bylo trapně',
  25. 'duration': 1484,
  26. 'upload_date': '20141217',
  27. 'timestamp': 1418792400,
  28. },
  29. }, {
  30. 'url': 'http://video.aktualne.cz/dvtv/dvtv-16-12-2014-utok-talibanu-boj-o-kliniku-uprchlici/r~973eb3bc854e11e498be002590604f2e/',
  31. 'info_dict': {
  32. 'title': r'DVTV 16. 12. 2014: útok Talibanu, boj o kliniku, uprchlíci',
  33. 'id': '973eb3bc854e11e498be002590604f2e',
  34. },
  35. 'playlist': [{
  36. 'md5': 'da7ca6be4935532241fa9520b3ad91e4',
  37. 'info_dict': {
  38. 'id': 'b0b40906854d11e4bdad0025900fea04',
  39. 'ext': 'mp4',
  40. 'title': 'Drtinová Veselovský TV 16. 12. 2014: Témata dne',
  41. 'description': 'md5:0916925dea8e30fe84222582280b47a0',
  42. 'timestamp': 1418760010,
  43. 'upload_date': '20141216',
  44. },
  45. }, {
  46. 'md5': '5f7652a08b05009c1292317b449ffea2',
  47. 'info_dict': {
  48. 'id': '420ad9ec854a11e4bdad0025900fea04',
  49. 'ext': 'mp4',
  50. 'title': 'Školní masakr možná změní boj s Talibanem, říká novinářka',
  51. 'description': 'md5:ff2f9f6de73c73d7cef4f756c1c1af42',
  52. 'timestamp': 1418760010,
  53. 'upload_date': '20141216',
  54. },
  55. }, {
  56. 'md5': '498eb9dfa97169f409126c617e2a3d64',
  57. 'info_dict': {
  58. 'id': '95d35580846a11e4b6d20025900fea04',
  59. 'ext': 'mp4',
  60. 'title': 'Boj o kliniku: Veřejný zájem, nebo právo na majetek?',
  61. 'description': 'md5:889fe610a70fee5511dc3326a089188e',
  62. 'timestamp': 1418760010,
  63. 'upload_date': '20141216',
  64. },
  65. }, {
  66. 'md5': 'b8dc6b744844032dab6ba3781a7274b9',
  67. 'info_dict': {
  68. 'id': '6fe14d66853511e4833a0025900fea04',
  69. 'ext': 'mp4',
  70. 'title': 'Pánek: Odmítání syrských uprchlíků je ostudou české vlády',
  71. 'description': 'md5:544f86de6d20c4815bea11bf2ac3004f',
  72. 'timestamp': 1418760010,
  73. 'upload_date': '20141216',
  74. },
  75. }],
  76. }, {
  77. 'url': 'https://video.aktualne.cz/dvtv/zeman-si-jen-leci-mindraky-sobotku-nenavidi-a-babis-se-mu-te/r~960cdb3a365a11e7a83b0025900fea04/',
  78. 'md5': 'f8efe9656017da948369aa099788c8ea',
  79. 'info_dict': {
  80. 'id': '3c496fec365911e7a6500025900fea04',
  81. 'ext': 'mp4',
  82. 'title': 'Zeman si jen léčí mindráky, Sobotku nenávidí a Babiš se mu teď hodí, tvrdí Kmenta',
  83. 'duration': 1103,
  84. 'upload_date': '20170511',
  85. 'timestamp': 1494514200,
  86. },
  87. 'params': {
  88. 'skip_download': True,
  89. },
  90. }, {
  91. 'url': 'http://video.aktualne.cz/v-cechach-poprve-zazni-zelenkova-zrestaurovana-mse/r~45b4b00483ec11e4883b002590604f2e/',
  92. 'only_matching': True,
  93. }, {
  94. # Test live stream video (liveStarter) parsing
  95. 'url': 'https://video.aktualne.cz/dvtv/zive-mistryne-sveta-eva-samkova-po-navratu-ze-sampionatu/r~182654c2288811e990fd0cc47ab5f122/',
  96. 'md5': '2e552e483f2414851ca50467054f9d5d',
  97. 'info_dict': {
  98. 'id': '8d116360288011e98c840cc47ab5f122',
  99. 'ext': 'mp4',
  100. 'title': 'Živě: Mistryně světa Eva Samková po návratu ze šampionátu',
  101. 'upload_date': '20190204',
  102. 'timestamp': 1549289591,
  103. },
  104. 'params': {
  105. # Video content is no longer available
  106. 'skip_download': True,
  107. },
  108. }]
  109. def _parse_video_metadata(self, js, video_id, timestamp):
  110. data = self._parse_json(js, video_id, transform_source=js_to_json)
  111. title = unescapeHTML(data['title'])
  112. live_starter = try_get(data, lambda x: x['plugins']['liveStarter'], dict)
  113. if live_starter:
  114. data.update(live_starter)
  115. formats = []
  116. for tracks in data.get('tracks', {}).values():
  117. for video in tracks:
  118. video_url = video.get('src')
  119. if not video_url:
  120. continue
  121. video_type = video.get('type')
  122. ext = determine_ext(video_url, mimetype2ext(video_type))
  123. if video_type == 'application/vnd.apple.mpegurl' or ext == 'm3u8':
  124. formats.extend(self._extract_m3u8_formats(
  125. video_url, video_id, 'mp4', entry_protocol='m3u8_native',
  126. m3u8_id='hls', fatal=False))
  127. elif video_type == 'application/dash+xml' or ext == 'mpd':
  128. formats.extend(self._extract_mpd_formats(
  129. video_url, video_id, mpd_id='dash', fatal=False))
  130. else:
  131. label = video.get('label')
  132. height = self._search_regex(
  133. r'^(\d+)[pP]', label or '', 'height', default=None)
  134. formats.append({
  135. 'url': video_url,
  136. 'format_id': join_nonempty('http', ext, label),
  137. 'height': int_or_none(height),
  138. })
  139. return {
  140. 'id': data.get('mediaid') or video_id,
  141. 'title': title,
  142. 'description': data.get('description'),
  143. 'thumbnail': data.get('image'),
  144. 'duration': int_or_none(data.get('duration')),
  145. 'timestamp': int_or_none(timestamp),
  146. 'formats': formats,
  147. }
  148. def _real_extract(self, url):
  149. video_id = self._match_id(url)
  150. webpage = self._download_webpage(url, video_id)
  151. timestamp = parse_iso8601(self._html_search_meta(
  152. 'article:published_time', webpage, 'published time', default=None))
  153. items = re.findall(r'(?s)playlist\.push\(({.+?})\);', webpage)
  154. if items:
  155. return self.playlist_result(
  156. [self._parse_video_metadata(i, video_id, timestamp) for i in items],
  157. video_id, self._html_search_meta('twitter:title', webpage))
  158. item = self._search_regex(
  159. r'(?s)BBXPlayer\.setup\((.+?)\);',
  160. webpage, 'video', default=None)
  161. if item:
  162. # remove function calls (ex. htmldeentitize)
  163. # TODO: this should be fixed in a general way in the js_to_json
  164. item = re.sub(r'\w+?\((.+)\)', r'\1', item)
  165. return self._parse_video_metadata(item, video_id, timestamp)
  166. raise ExtractorError('Could not find neither video nor playlist')