wdr.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import re
  2. from .common import InfoExtractor
  3. from ..compat import (
  4. compat_str,
  5. compat_urlparse,
  6. )
  7. from ..utils import (
  8. determine_ext,
  9. dict_get,
  10. ExtractorError,
  11. js_to_json,
  12. strip_jsonp,
  13. try_get,
  14. unified_strdate,
  15. update_url_query,
  16. urlhandle_detect_ext,
  17. url_or_none,
  18. )
  19. class WDRIE(InfoExtractor):
  20. __API_URL_TPL = '//deviceids-medp.wdr.de/ondemand/%s/%s'
  21. _VALID_URL = r'''(?x)https?://
  22. (?:deviceids-medp\.wdr\.de/ondemand/\d+/|
  23. kinder\.wdr\.de/(?!mediathek/)[^#?]+-)
  24. (?P<id>\d+)\.(?:js|assetjsonp)
  25. '''
  26. _GEO_COUNTRIES = ['DE']
  27. _TESTS = [{
  28. 'url': 'http://deviceids-medp.wdr.de/ondemand/155/1557833.js',
  29. 'info_dict': {
  30. 'id': 'mdb-1557833',
  31. 'ext': 'mp4',
  32. 'title': 'Biathlon-Staffel verpasst Podest bei Olympia-Generalprobe',
  33. 'upload_date': '20180112',
  34. },
  35. }]
  36. def _asset_url(self, wdr_id):
  37. id_len = max(len(wdr_id), 5)
  38. return ''.join(('https:', self.__API_URL_TPL % (wdr_id[:id_len - 4], wdr_id, ), '.js'))
  39. def _real_extract(self, url):
  40. video_id = self._match_id(url)
  41. if url.startswith('wdr:'):
  42. video_id = url[4:]
  43. url = self._asset_url(video_id)
  44. metadata = self._download_json(
  45. url, video_id, transform_source=strip_jsonp)
  46. is_live = metadata.get('mediaType') == 'live'
  47. tracker_data = metadata['trackerData']
  48. title = tracker_data['trackerClipTitle']
  49. media_resource = metadata['mediaResource']
  50. formats = []
  51. subtitles = {}
  52. # check if the metadata contains a direct URL to a file
  53. for kind, media in media_resource.items():
  54. if kind == 'captionsHash':
  55. for ext, url in media.items():
  56. subtitles.setdefault('de', []).append({
  57. 'url': url,
  58. 'ext': ext,
  59. })
  60. continue
  61. if kind not in ('dflt', 'alt'):
  62. continue
  63. if not isinstance(media, dict):
  64. continue
  65. for tag_name, medium_url in media.items():
  66. if tag_name not in ('videoURL', 'audioURL'):
  67. continue
  68. ext = determine_ext(medium_url)
  69. if ext == 'm3u8':
  70. formats.extend(self._extract_m3u8_formats(
  71. medium_url, video_id, 'mp4', 'm3u8_native',
  72. m3u8_id='hls'))
  73. elif ext == 'f4m':
  74. manifest_url = update_url_query(
  75. medium_url, {'hdcore': '3.2.0', 'plugin': 'aasp-3.2.0.77.18'})
  76. formats.extend(self._extract_f4m_formats(
  77. manifest_url, video_id, f4m_id='hds', fatal=False))
  78. elif ext == 'smil':
  79. formats.extend(self._extract_smil_formats(
  80. medium_url, 'stream', fatal=False))
  81. else:
  82. a_format = {
  83. 'url': medium_url
  84. }
  85. if ext == 'unknown_video':
  86. urlh = self._request_webpage(
  87. medium_url, video_id, note='Determining extension')
  88. ext = urlhandle_detect_ext(urlh)
  89. a_format['ext'] = ext
  90. formats.append(a_format)
  91. caption_url = media_resource.get('captionURL')
  92. if caption_url:
  93. subtitles['de'] = [{
  94. 'url': caption_url,
  95. 'ext': 'ttml',
  96. }]
  97. captions_hash = media_resource.get('captionsHash')
  98. if isinstance(captions_hash, dict):
  99. for ext, format_url in captions_hash.items():
  100. format_url = url_or_none(format_url)
  101. if not format_url:
  102. continue
  103. subtitles.setdefault('de', []).append({
  104. 'url': format_url,
  105. 'ext': determine_ext(format_url, None) or ext,
  106. })
  107. return {
  108. 'id': tracker_data.get('trackerClipId', video_id),
  109. 'title': title,
  110. 'alt_title': tracker_data.get('trackerClipSubcategory'),
  111. 'formats': formats,
  112. 'subtitles': subtitles,
  113. 'upload_date': unified_strdate(tracker_data.get('trackerClipAirTime')),
  114. 'is_live': is_live,
  115. }
  116. class WDRPageIE(WDRIE): # XXX: Do not subclass from concrete IE
  117. _MAUS_REGEX = r'https?://(?:www\.)wdrmaus.de/(?:[^/]+/)*?(?P<maus_id>[^/?#.]+)(?:/?|/index\.php5|\.php5)$'
  118. _PAGE_REGEX = r'/(?:mediathek/)?(?:[^/]+/)*(?P<display_id>[^/]+)\.html'
  119. _VALID_URL = r'https?://(?:www\d?\.)?(?:(?:kinder\.)?wdr\d?|sportschau)\.de' + _PAGE_REGEX + '|' + _MAUS_REGEX
  120. _TESTS = [
  121. {
  122. 'url': 'http://www1.wdr.de/mediathek/video/sendungen/doku-am-freitag/video-geheimnis-aachener-dom-100.html',
  123. # HDS download, MD5 is unstable
  124. 'info_dict': {
  125. 'id': 'mdb-1058683',
  126. 'ext': 'flv',
  127. 'display_id': 'doku-am-freitag/video-geheimnis-aachener-dom-100',
  128. 'title': 'Geheimnis Aachener Dom',
  129. 'alt_title': 'Doku am Freitag',
  130. 'upload_date': '20160304',
  131. 'description': 'md5:87be8ff14d8dfd7a7ee46f0299b52318',
  132. 'is_live': False,
  133. 'subtitles': {'de': [{
  134. 'url': 'http://ondemand-ww.wdr.de/medp/fsk0/105/1058683/1058683_12220974.xml',
  135. 'ext': 'ttml',
  136. }]},
  137. },
  138. 'skip': 'HTTP Error 404: Not Found',
  139. },
  140. {
  141. 'url': 'http://www1.wdr.de/mediathek/audio/wdr3/wdr3-gespraech-am-samstag/audio-schriftstellerin-juli-zeh-100.html',
  142. 'md5': 'f4c1f96d01cf285240f53ea4309663d8',
  143. 'info_dict': {
  144. 'id': 'mdb-1072000',
  145. 'ext': 'mp3',
  146. 'display_id': 'wdr3-gespraech-am-samstag/audio-schriftstellerin-juli-zeh-100',
  147. 'title': 'Schriftstellerin Juli Zeh',
  148. 'alt_title': 'WDR 3 Gespräch am Samstag',
  149. 'upload_date': '20160312',
  150. 'description': 'md5:e127d320bc2b1f149be697ce044a3dd7',
  151. 'is_live': False,
  152. 'subtitles': {}
  153. },
  154. 'skip': 'HTTP Error 404: Not Found',
  155. },
  156. {
  157. 'url': 'http://www1.wdr.de/mediathek/video/live/index.html',
  158. 'info_dict': {
  159. 'id': 'mdb-2296252',
  160. 'ext': 'mp4',
  161. 'title': r're:^WDR Fernsehen im Livestream (?:\(nur in Deutschland erreichbar\) )?[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  162. 'alt_title': 'WDR Fernsehen Live',
  163. 'upload_date': '20201112',
  164. 'is_live': True,
  165. },
  166. 'params': {
  167. 'skip_download': True, # m3u8 download
  168. },
  169. },
  170. {
  171. 'url': 'http://www1.wdr.de/mediathek/video/sendungen/aktuelle-stunde/aktuelle-stunde-120.html',
  172. 'playlist_mincount': 6,
  173. 'info_dict': {
  174. 'id': 'aktuelle-stunde-120',
  175. },
  176. },
  177. {
  178. 'url': 'http://www.wdrmaus.de/aktuelle-sendung/index.php5',
  179. 'info_dict': {
  180. 'id': 'mdb-2627637',
  181. 'ext': 'mp4',
  182. 'upload_date': 're:^[0-9]{8}$',
  183. 'title': 're:^Die Sendung (?:mit der Maus )?vom [0-9.]{10}$',
  184. },
  185. 'skip': 'The id changes from week to week because of the new episode'
  186. },
  187. {
  188. 'url': 'http://www.wdrmaus.de/filme/sachgeschichten/achterbahn.php5',
  189. 'md5': '803138901f6368ee497b4d195bb164f2',
  190. 'info_dict': {
  191. 'id': 'mdb-186083',
  192. 'ext': 'mp4',
  193. 'upload_date': '20130919',
  194. 'title': 'Sachgeschichte - Achterbahn ',
  195. },
  196. 'skip': 'HTTP Error 404: Not Found',
  197. },
  198. {
  199. 'url': 'http://www1.wdr.de/radio/player/radioplayer116~_layout-popupVersion.html',
  200. # Live stream, MD5 unstable
  201. 'info_dict': {
  202. 'id': 'mdb-869971',
  203. 'ext': 'mp4',
  204. 'title': r're:^COSMO Livestream [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  205. 'upload_date': '20160101',
  206. },
  207. 'params': {
  208. 'skip_download': True, # m3u8 download
  209. }
  210. },
  211. {
  212. 'url': 'http://www.sportschau.de/handballem2018/handball-nationalmannschaft-em-stolperstein-vorrunde-100.html',
  213. 'info_dict': {
  214. 'id': 'mdb-1556012',
  215. 'ext': 'mp4',
  216. 'title': 'DHB-Vizepräsident Bob Hanning - "Die Weltspitze ist extrem breit"',
  217. 'upload_date': '20180111',
  218. },
  219. 'params': {
  220. 'skip_download': True,
  221. },
  222. 'skip': 'HTTP Error 404: Not Found',
  223. },
  224. {
  225. 'url': 'http://www.sportschau.de/handballem2018/audio-vorschau---die-handball-em-startet-mit-grossem-favoritenfeld-100.html',
  226. 'only_matching': True,
  227. },
  228. {
  229. 'url': 'https://kinder.wdr.de/tv/die-sendung-mit-dem-elefanten/av/video-folge---astronaut-100.html',
  230. 'only_matching': True,
  231. },
  232. ]
  233. def _real_extract(self, url):
  234. mobj = self._match_valid_url(url)
  235. display_id = dict_get(mobj.groupdict(), ('display_id', 'maus_id'), 'wdrmaus')
  236. webpage = self._download_webpage(url, display_id)
  237. entries = []
  238. # Article with several videos
  239. # for wdr.de the data-extension is in a tag with the class "mediaLink"
  240. # for wdr.de radio players, in a tag with the class "wdrrPlayerPlayBtn"
  241. # for wdrmaus, in a tag with the class "videoButton" (previously a link
  242. # to the page in a multiline "videoLink"-tag)
  243. for mobj in re.finditer(
  244. r'''(?sx)class=
  245. (?:
  246. (["\'])(?:mediaLink|wdrrPlayerPlayBtn|videoButton)\b.*?\1[^>]+|
  247. (["\'])videoLink\b.*?\2[\s]*>\n[^\n]*
  248. )data-extension=(["\'])(?P<data>(?:(?!\3).)+)\3
  249. ''', webpage):
  250. media_link_obj = self._parse_json(
  251. mobj.group('data'), display_id, transform_source=js_to_json,
  252. fatal=False)
  253. if not media_link_obj:
  254. continue
  255. jsonp_url = try_get(
  256. media_link_obj, lambda x: x['mediaObj']['url'], compat_str)
  257. if jsonp_url:
  258. # metadata, or player JS with ['ref'] giving WDR id, or just media, perhaps
  259. clip_id = media_link_obj['mediaObj'].get('ref')
  260. if jsonp_url.endswith('.assetjsonp'):
  261. asset = self._download_json(
  262. jsonp_url, display_id, fatal=False, transform_source=strip_jsonp)
  263. clip_id = try_get(asset, lambda x: x['trackerData']['trackerClipId'], compat_str)
  264. if clip_id:
  265. jsonp_url = self._asset_url(clip_id[4:])
  266. entries.append(self.url_result(jsonp_url, ie=WDRIE.ie_key()))
  267. # Playlist (e.g. https://www1.wdr.de/mediathek/video/sendungen/aktuelle-stunde/aktuelle-stunde-120.html)
  268. if not entries:
  269. entries = [
  270. self.url_result(
  271. compat_urlparse.urljoin(url, mobj.group('href')),
  272. ie=WDRPageIE.ie_key())
  273. for mobj in re.finditer(
  274. r'<a[^>]+\bhref=(["\'])(?P<href>(?:(?!\1).)+)\1[^>]+\bdata-extension=',
  275. webpage) if re.match(self._PAGE_REGEX, mobj.group('href'))
  276. ]
  277. return self.playlist_result(entries, playlist_id=display_id)
  278. class WDRElefantIE(InfoExtractor):
  279. _VALID_URL = r'https?://(?:www\.)wdrmaus\.de/elefantenseite/#(?P<id>.+)'
  280. _TEST = {
  281. 'url': 'http://www.wdrmaus.de/elefantenseite/#elefantenkino_wippe',
  282. # adaptive stream: unstable file MD5
  283. 'info_dict': {
  284. 'title': 'Wippe',
  285. 'id': 'mdb-1198320',
  286. 'ext': 'mp4',
  287. 'age_limit': None,
  288. 'upload_date': '20071003'
  289. },
  290. }
  291. def _real_extract(self, url):
  292. display_id = self._match_id(url)
  293. # Table of Contents seems to always be at this address, so fetch it directly.
  294. # The website fetches configurationJS.php5, which links to tableOfContentsJS.php5.
  295. table_of_contents = self._download_json(
  296. 'https://www.wdrmaus.de/elefantenseite/data/tableOfContentsJS.php5',
  297. display_id)
  298. if display_id not in table_of_contents:
  299. raise ExtractorError(
  300. 'No entry in site\'s table of contents for this URL. '
  301. 'Is the fragment part of the URL (after the #) correct?',
  302. expected=True)
  303. xml_metadata_path = table_of_contents[display_id]['xmlPath']
  304. xml_metadata = self._download_xml(
  305. 'https://www.wdrmaus.de/elefantenseite/' + xml_metadata_path,
  306. display_id)
  307. zmdb_url_element = xml_metadata.find('./movie/zmdb_url')
  308. if zmdb_url_element is None:
  309. raise ExtractorError(
  310. '%s is not a video' % display_id, expected=True)
  311. return self.url_result(zmdb_url_element.text, ie=WDRIE.ie_key())
  312. class WDRMobileIE(InfoExtractor):
  313. _VALID_URL = r'''(?x)
  314. https?://mobile-ondemand\.wdr\.de/
  315. .*?/fsk(?P<age_limit>[0-9]+)
  316. /[0-9]+/[0-9]+/
  317. (?P<id>[0-9]+)_(?P<title>[0-9]+)'''
  318. IE_NAME = 'wdr:mobile'
  319. _WORKING = False # no such domain
  320. _TEST = {
  321. 'url': 'http://mobile-ondemand.wdr.de/CMS2010/mdb/ondemand/weltweit/fsk0/42/421735/421735_4283021.mp4',
  322. 'info_dict': {
  323. 'title': '4283021',
  324. 'id': '421735',
  325. 'ext': 'mp4',
  326. 'age_limit': 0,
  327. },
  328. 'skip': 'Problems with loading data.'
  329. }
  330. def _real_extract(self, url):
  331. mobj = self._match_valid_url(url)
  332. return {
  333. 'id': mobj.group('id'),
  334. 'title': mobj.group('title'),
  335. 'age_limit': int(mobj.group('age_limit')),
  336. 'url': url,
  337. 'http_headers': {
  338. 'User-Agent': 'mobile',
  339. },
  340. }