You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
8.7 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. int_or_none,
  7. float_or_none,
  8. mimetype2ext,
  9. parse_iso8601,
  10. remove_end,
  11. update_url_query,
  12. )
  13. class DRTVIE(InfoExtractor):
  14. _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv/se|nyheder|radio/ondemand)/(?:[^/]+/)*(?P<id>[\da-z-]+)(?:[/#?]|$)'
  15. _GEO_BYPASS = False
  16. _GEO_COUNTRIES = ['DK']
  17. IE_NAME = 'drtv'
  18. _TESTS = [{
  19. 'url': 'https://www.dr.dk/tv/se/boern/ultra/klassen-ultra/klassen-darlig-taber-10',
  20. 'md5': '7ae17b4e18eb5d29212f424a7511c184',
  21. 'info_dict': {
  22. 'id': 'klassen-darlig-taber-10',
  23. 'ext': 'mp4',
  24. 'title': 'Klassen - Dårlig taber (10)',
  25. 'description': 'md5:815fe1b7fa656ed80580f31e8b3c79aa',
  26. 'timestamp': 1471991907,
  27. 'upload_date': '20160823',
  28. 'duration': 606.84,
  29. },
  30. }, {
  31. # embed
  32. 'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang',
  33. 'info_dict': {
  34. 'id': 'christiania-pusher-street-ryddes-drdkrjpo',
  35. 'ext': 'mp4',
  36. 'title': 'LIVE Christianias rydning af Pusher Street er i gang',
  37. 'description': 'md5:2a71898b15057e9b97334f61d04e6eb5',
  38. 'timestamp': 1472800279,
  39. 'upload_date': '20160902',
  40. 'duration': 131.4,
  41. },
  42. 'params': {
  43. 'skip_download': True,
  44. },
  45. }, {
  46. # with SignLanguage formats
  47. 'url': 'https://www.dr.dk/tv/se/historien-om-danmark/-/historien-om-danmark-stenalder',
  48. 'info_dict': {
  49. 'id': 'historien-om-danmark-stenalder',
  50. 'ext': 'mp4',
  51. 'title': 'Historien om Danmark: Stenalder (1)',
  52. 'description': 'md5:8c66dcbc1669bbc6f873879880f37f2a',
  53. 'timestamp': 1490401996,
  54. 'upload_date': '20170325',
  55. 'duration': 3502.04,
  56. 'formats': 'mincount:20',
  57. },
  58. 'params': {
  59. 'skip_download': True,
  60. },
  61. }]
  62. def _real_extract(self, url):
  63. video_id = self._match_id(url)
  64. webpage = self._download_webpage(url, video_id)
  65. if '>Programmet er ikke længere tilgængeligt' in webpage:
  66. raise ExtractorError(
  67. 'Video %s is not available' % video_id, expected=True)
  68. video_id = self._search_regex(
  69. (r'data-(?:material-identifier|episode-slug)="([^"]+)"',
  70. r'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
  71. webpage, 'video id')
  72. programcard = self._download_json(
  73. 'http://www.dr.dk/mu/programcard/expanded/%s' % video_id,
  74. video_id, 'Downloading video JSON')
  75. data = programcard['Data'][0]
  76. title = remove_end(self._og_search_title(
  77. webpage, default=None), ' | TV | DR') or data['Title']
  78. description = self._og_search_description(
  79. webpage, default=None) or data.get('Description')
  80. timestamp = parse_iso8601(data.get('CreatedTime'))
  81. thumbnail = None
  82. duration = None
  83. restricted_to_denmark = False
  84. formats = []
  85. subtitles = {}
  86. for asset in data['Assets']:
  87. kind = asset.get('Kind')
  88. if kind == 'Image':
  89. thumbnail = asset.get('Uri')
  90. elif kind in ('VideoResource', 'AudioResource'):
  91. duration = float_or_none(asset.get('DurationInMilliseconds'), 1000)
  92. restricted_to_denmark = asset.get('RestrictedToDenmark')
  93. asset_target = asset.get('Target')
  94. for link in asset.get('Links', []):
  95. uri = link.get('Uri')
  96. if not uri:
  97. continue
  98. target = link.get('Target')
  99. format_id = target or ''
  100. preference = None
  101. if asset_target in ('SpokenSubtitles', 'SignLanguage'):
  102. preference = -1
  103. format_id += '-%s' % asset_target
  104. if target == 'HDS':
  105. f4m_formats = self._extract_f4m_formats(
  106. uri + '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
  107. video_id, preference, f4m_id=format_id, fatal=False)
  108. if kind == 'AudioResource':
  109. for f in f4m_formats:
  110. f['vcodec'] = 'none'
  111. formats.extend(f4m_formats)
  112. elif target == 'HLS':
  113. formats.extend(self._extract_m3u8_formats(
  114. uri, video_id, 'mp4', entry_protocol='m3u8_native',
  115. preference=preference, m3u8_id=format_id,
  116. fatal=False))
  117. else:
  118. bitrate = link.get('Bitrate')
  119. if bitrate:
  120. format_id += '-%s' % bitrate
  121. formats.append({
  122. 'url': uri,
  123. 'format_id': format_id,
  124. 'tbr': int_or_none(bitrate),
  125. 'ext': link.get('FileFormat'),
  126. 'vcodec': 'none' if kind == 'AudioResource' else None,
  127. 'preference': preference,
  128. })
  129. subtitles_list = asset.get('SubtitlesList')
  130. if isinstance(subtitles_list, list):
  131. LANGS = {
  132. 'Danish': 'da',
  133. }
  134. for subs in subtitles_list:
  135. if not subs.get('Uri'):
  136. continue
  137. lang = subs.get('Language') or 'da'
  138. subtitles.setdefault(LANGS.get(lang, lang), []).append({
  139. 'url': subs['Uri'],
  140. 'ext': mimetype2ext(subs.get('MimeType')) or 'vtt'
  141. })
  142. if not formats and restricted_to_denmark:
  143. self.raise_geo_restricted(
  144. 'Unfortunately, DR is not allowed to show this program outside Denmark.',
  145. countries=self._GEO_COUNTRIES)
  146. self._sort_formats(formats)
  147. return {
  148. 'id': video_id,
  149. 'title': title,
  150. 'description': description,
  151. 'thumbnail': thumbnail,
  152. 'timestamp': timestamp,
  153. 'duration': duration,
  154. 'formats': formats,
  155. 'subtitles': subtitles,
  156. }
  157. class DRTVLiveIE(InfoExtractor):
  158. IE_NAME = 'drtv:live'
  159. _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
  160. _GEO_COUNTRIES = ['DK']
  161. _TEST = {
  162. 'url': 'https://www.dr.dk/tv/live/dr1',
  163. 'info_dict': {
  164. 'id': 'dr1',
  165. 'ext': 'mp4',
  166. 'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  167. },
  168. 'params': {
  169. # m3u8 download
  170. 'skip_download': True,
  171. },
  172. }
  173. def _real_extract(self, url):
  174. channel_id = self._match_id(url)
  175. channel_data = self._download_json(
  176. 'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id,
  177. channel_id)
  178. title = self._live_title(channel_data['Title'])
  179. formats = []
  180. for streaming_server in channel_data.get('StreamingServers', []):
  181. server = streaming_server.get('Server')
  182. if not server:
  183. continue
  184. link_type = streaming_server.get('LinkType')
  185. for quality in streaming_server.get('Qualities', []):
  186. for stream in quality.get('Streams', []):
  187. stream_path = stream.get('Stream')
  188. if not stream_path:
  189. continue
  190. stream_url = update_url_query(
  191. '%s/%s' % (server, stream_path), {'b': ''})
  192. if link_type == 'HLS':
  193. formats.extend(self._extract_m3u8_formats(
  194. stream_url, channel_id, 'mp4',
  195. m3u8_id=link_type, fatal=False, live=True))
  196. elif link_type == 'HDS':
  197. formats.extend(self._extract_f4m_formats(update_url_query(
  198. '%s/%s' % (server, stream_path), {'hdcore': '3.7.0'}),
  199. channel_id, f4m_id=link_type, fatal=False))
  200. self._sort_formats(formats)
  201. return {
  202. 'id': channel_id,
  203. 'title': title,
  204. 'thumbnail': channel_data.get('PrimaryImageUri'),
  205. 'formats': formats,
  206. 'is_live': True,
  207. }