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.

400 lines
16 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from .generic import GenericIE
  6. from ..utils import (
  7. determine_ext,
  8. ExtractorError,
  9. int_or_none,
  10. parse_duration,
  11. qualities,
  12. str_or_none,
  13. try_get,
  14. unified_strdate,
  15. unified_timestamp,
  16. update_url_query,
  17. url_or_none,
  18. xpath_text,
  19. )
  20. from ..compat import compat_etree_fromstring
  21. class ARDMediathekIE(InfoExtractor):
  22. IE_NAME = 'ARD:mediathek'
  23. _VALID_URL = r'^https?://(?:(?:(?:www|classic)\.)?ardmediathek\.de|mediathek\.(?:daserste|rbb-online)\.de|one\.ard\.de)/(?:.*/)(?P<video_id>[0-9]+|[^0-9][^/\?]+)[^/\?]*(?:\?.*)?'
  24. _TESTS = [{
  25. # available till 26.07.2022
  26. 'url': 'http://www.ardmediathek.de/tv/S%C3%9CDLICHT/Was-ist-die-Kunst-der-Zukunft-liebe-Ann/BR-Fernsehen/Video?bcastId=34633636&documentId=44726822',
  27. 'info_dict': {
  28. 'id': '44726822',
  29. 'ext': 'mp4',
  30. 'title': 'Was ist die Kunst der Zukunft, liebe Anna McCarthy?',
  31. 'description': 'md5:4ada28b3e3b5df01647310e41f3a62f5',
  32. 'duration': 1740,
  33. },
  34. 'params': {
  35. # m3u8 download
  36. 'skip_download': True,
  37. }
  38. }, {
  39. 'url': 'https://one.ard.de/tv/Mord-mit-Aussicht/Mord-mit-Aussicht-6-39-T%C3%B6dliche-Nach/ONE/Video?bcastId=46384294&documentId=55586872',
  40. 'only_matching': True,
  41. }, {
  42. # audio
  43. 'url': 'http://www.ardmediathek.de/tv/WDR-H%C3%B6rspiel-Speicher/Tod-eines-Fu%C3%9Fballers/WDR-3/Audio-Podcast?documentId=28488308&bcastId=23074086',
  44. 'only_matching': True,
  45. }, {
  46. 'url': 'http://mediathek.daserste.de/sendungen_a-z/328454_anne-will/22429276_vertrauen-ist-gut-spionieren-ist-besser-geht',
  47. 'only_matching': True,
  48. }, {
  49. # audio
  50. 'url': 'http://mediathek.rbb-online.de/radio/Hörspiel/Vor-dem-Fest/kulturradio/Audio?documentId=30796318&topRessort=radio&bcastId=9839158',
  51. 'only_matching': True,
  52. }, {
  53. 'url': 'https://classic.ardmediathek.de/tv/Panda-Gorilla-Co/Panda-Gorilla-Co-Folge-274/Das-Erste/Video?bcastId=16355486&documentId=58234698',
  54. 'only_matching': True,
  55. }]
  56. @classmethod
  57. def suitable(cls, url):
  58. return False if ARDBetaMediathekIE.suitable(url) else super(ARDMediathekIE, cls).suitable(url)
  59. def _extract_media_info(self, media_info_url, webpage, video_id):
  60. media_info = self._download_json(
  61. media_info_url, video_id, 'Downloading media JSON')
  62. formats = self._extract_formats(media_info, video_id)
  63. if not formats:
  64. if '"fsk"' in webpage:
  65. raise ExtractorError(
  66. 'This video is only available after 20:00', expected=True)
  67. elif media_info.get('_geoblocked'):
  68. raise ExtractorError('This video is not available due to geo restriction', expected=True)
  69. self._sort_formats(formats)
  70. duration = int_or_none(media_info.get('_duration'))
  71. thumbnail = media_info.get('_previewImage')
  72. is_live = media_info.get('_isLive') is True
  73. subtitles = {}
  74. subtitle_url = media_info.get('_subtitleUrl')
  75. if subtitle_url:
  76. subtitles['de'] = [{
  77. 'ext': 'ttml',
  78. 'url': subtitle_url,
  79. }]
  80. return {
  81. 'id': video_id,
  82. 'duration': duration,
  83. 'thumbnail': thumbnail,
  84. 'is_live': is_live,
  85. 'formats': formats,
  86. 'subtitles': subtitles,
  87. }
  88. def _extract_formats(self, media_info, video_id):
  89. type_ = media_info.get('_type')
  90. media_array = media_info.get('_mediaArray', [])
  91. formats = []
  92. for num, media in enumerate(media_array):
  93. for stream in media.get('_mediaStreamArray', []):
  94. stream_urls = stream.get('_stream')
  95. if not stream_urls:
  96. continue
  97. if not isinstance(stream_urls, list):
  98. stream_urls = [stream_urls]
  99. quality = stream.get('_quality')
  100. server = stream.get('_server')
  101. for stream_url in stream_urls:
  102. if not url_or_none(stream_url):
  103. continue
  104. ext = determine_ext(stream_url)
  105. if quality != 'auto' and ext in ('f4m', 'm3u8'):
  106. continue
  107. if ext == 'f4m':
  108. formats.extend(self._extract_f4m_formats(
  109. update_url_query(stream_url, {
  110. 'hdcore': '3.1.1',
  111. 'plugin': 'aasp-3.1.1.69.124'
  112. }),
  113. video_id, f4m_id='hds', fatal=False))
  114. elif ext == 'm3u8':
  115. formats.extend(self._extract_m3u8_formats(
  116. stream_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  117. else:
  118. if server and server.startswith('rtmp'):
  119. f = {
  120. 'url': server,
  121. 'play_path': stream_url,
  122. 'format_id': 'a%s-rtmp-%s' % (num, quality),
  123. }
  124. else:
  125. f = {
  126. 'url': stream_url,
  127. 'format_id': 'a%s-%s-%s' % (num, ext, quality)
  128. }
  129. m = re.search(r'_(?P<width>\d+)x(?P<height>\d+)\.mp4$', stream_url)
  130. if m:
  131. f.update({
  132. 'width': int(m.group('width')),
  133. 'height': int(m.group('height')),
  134. })
  135. if type_ == 'audio':
  136. f['vcodec'] = 'none'
  137. formats.append(f)
  138. return formats
  139. def _real_extract(self, url):
  140. # determine video id from url
  141. m = re.match(self._VALID_URL, url)
  142. document_id = None
  143. numid = re.search(r'documentId=([0-9]+)', url)
  144. if numid:
  145. document_id = video_id = numid.group(1)
  146. else:
  147. video_id = m.group('video_id')
  148. webpage = self._download_webpage(url, video_id)
  149. ERRORS = (
  150. ('>Leider liegt eine Störung vor.', 'Video %s is unavailable'),
  151. ('>Der gewünschte Beitrag ist nicht mehr verfügbar.<',
  152. 'Video %s is no longer available'),
  153. )
  154. for pattern, message in ERRORS:
  155. if pattern in webpage:
  156. raise ExtractorError(message % video_id, expected=True)
  157. if re.search(r'[\?&]rss($|[=&])', url):
  158. doc = compat_etree_fromstring(webpage.encode('utf-8'))
  159. if doc.tag == 'rss':
  160. return GenericIE()._extract_rss(url, video_id, doc)
  161. title = self._html_search_regex(
  162. [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
  163. r'<meta name="dcterms\.title" content="(.*?)"/>',
  164. r'<h4 class="headline">(.*?)</h4>',
  165. r'<title[^>]*>(.*?)</title>'],
  166. webpage, 'title')
  167. description = self._html_search_meta(
  168. 'dcterms.abstract', webpage, 'description', default=None)
  169. if description is None:
  170. description = self._html_search_meta(
  171. 'description', webpage, 'meta description', default=None)
  172. if description is None:
  173. description = self._html_search_regex(
  174. r'<p\s+class="teasertext">(.+?)</p>',
  175. webpage, 'teaser text', default=None)
  176. # Thumbnail is sometimes not present.
  177. # It is in the mobile version, but that seems to use a different URL
  178. # structure altogether.
  179. thumbnail = self._og_search_thumbnail(webpage, default=None)
  180. media_streams = re.findall(r'''(?x)
  181. mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
  182. "([^"]+)"''', webpage)
  183. if media_streams:
  184. QUALITIES = qualities(['lo', 'hi', 'hq'])
  185. formats = []
  186. for furl in set(media_streams):
  187. if furl.endswith('.f4m'):
  188. fid = 'f4m'
  189. else:
  190. fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
  191. fid = fid_m.group(1) if fid_m else None
  192. formats.append({
  193. 'quality': QUALITIES(fid),
  194. 'format_id': fid,
  195. 'url': furl,
  196. })
  197. self._sort_formats(formats)
  198. info = {
  199. 'formats': formats,
  200. }
  201. else: # request JSON file
  202. if not document_id:
  203. video_id = self._search_regex(
  204. r'/play/(?:config|media)/(\d+)', webpage, 'media id')
  205. info = self._extract_media_info(
  206. 'http://www.ardmediathek.de/play/media/%s' % video_id,
  207. webpage, video_id)
  208. info.update({
  209. 'id': video_id,
  210. 'title': self._live_title(title) if info.get('is_live') else title,
  211. 'description': description,
  212. 'thumbnail': thumbnail,
  213. })
  214. return info
  215. class ARDIE(InfoExtractor):
  216. _VALID_URL = r'(?P<mainurl>https?://(www\.)?daserste\.de/[^?#]+/videos/(?P<display_id>[^/?#]+)-(?P<id>[0-9]+))\.html'
  217. _TESTS = [{
  218. # available till 14.02.2019
  219. 'url': 'http://www.daserste.de/information/talk/maischberger/videos/das-groko-drama-zerlegen-sich-die-volksparteien-video-102.html',
  220. 'md5': '8e4ec85f31be7c7fc08a26cdbc5a1f49',
  221. 'info_dict': {
  222. 'display_id': 'das-groko-drama-zerlegen-sich-die-volksparteien-video',
  223. 'id': '102',
  224. 'ext': 'mp4',
  225. 'duration': 4435.0,
  226. 'title': 'Das GroKo-Drama: Zerlegen sich die Volksparteien?',
  227. 'upload_date': '20180214',
  228. 'thumbnail': r're:^https?://.*\.jpg$',
  229. },
  230. }, {
  231. 'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
  232. 'only_matching': True,
  233. }]
  234. def _real_extract(self, url):
  235. mobj = re.match(self._VALID_URL, url)
  236. display_id = mobj.group('display_id')
  237. player_url = mobj.group('mainurl') + '~playerXml.xml'
  238. doc = self._download_xml(player_url, display_id)
  239. video_node = doc.find('./video')
  240. upload_date = unified_strdate(xpath_text(
  241. video_node, './broadcastDate'))
  242. thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
  243. formats = []
  244. for a in video_node.findall('.//asset'):
  245. f = {
  246. 'format_id': a.attrib['type'],
  247. 'width': int_or_none(a.find('./frameWidth').text),
  248. 'height': int_or_none(a.find('./frameHeight').text),
  249. 'vbr': int_or_none(a.find('./bitrateVideo').text),
  250. 'abr': int_or_none(a.find('./bitrateAudio').text),
  251. 'vcodec': a.find('./codecVideo').text,
  252. 'tbr': int_or_none(a.find('./totalBitrate').text),
  253. }
  254. if a.find('./serverPrefix').text:
  255. f['url'] = a.find('./serverPrefix').text
  256. f['playpath'] = a.find('./fileName').text
  257. else:
  258. f['url'] = a.find('./fileName').text
  259. formats.append(f)
  260. self._sort_formats(formats)
  261. return {
  262. 'id': mobj.group('id'),
  263. 'formats': formats,
  264. 'display_id': display_id,
  265. 'title': video_node.find('./title').text,
  266. 'duration': parse_duration(video_node.find('./duration').text),
  267. 'upload_date': upload_date,
  268. 'thumbnail': thumbnail,
  269. }
  270. class ARDBetaMediathekIE(InfoExtractor):
  271. _VALID_URL = r'https://(?:beta|www)\.ardmediathek\.de/[^/]+/(?:player|live)/(?P<video_id>[a-zA-Z0-9]+)(?:/(?P<display_id>[^/?#]+))?'
  272. _TESTS = [{
  273. 'url': 'https://beta.ardmediathek.de/ard/player/Y3JpZDovL2Rhc2Vyc3RlLmRlL3RhdG9ydC9mYmM4NGM1NC0xNzU4LTRmZGYtYWFhZS0wYzcyZTIxNGEyMDE/die-robuste-roswita',
  274. 'md5': '2d02d996156ea3c397cfc5036b5d7f8f',
  275. 'info_dict': {
  276. 'display_id': 'die-robuste-roswita',
  277. 'id': 'Y3JpZDovL2Rhc2Vyc3RlLmRlL3RhdG9ydC9mYmM4NGM1NC0xNzU4LTRmZGYtYWFhZS0wYzcyZTIxNGEyMDE',
  278. 'title': 'Tatort: Die robuste Roswita',
  279. 'description': r're:^Der Mord.*trüber ist als die Ilm.',
  280. 'duration': 5316,
  281. 'thumbnail': 'https://img.ardmediathek.de/standard/00/55/43/59/34/-1774185891/16x9/960?mandant=ard',
  282. 'upload_date': '20180826',
  283. 'ext': 'mp4',
  284. },
  285. }, {
  286. 'url': 'https://www.ardmediathek.de/ard/player/Y3JpZDovL3N3ci5kZS9hZXgvbzEwNzE5MTU/',
  287. 'only_matching': True,
  288. }, {
  289. 'url': 'https://www.ardmediathek.de/swr/live/Y3JpZDovL3N3ci5kZS8xMzQ4MTA0Mg',
  290. 'only_matching': True,
  291. }]
  292. def _real_extract(self, url):
  293. mobj = re.match(self._VALID_URL, url)
  294. video_id = mobj.group('video_id')
  295. display_id = mobj.group('display_id') or video_id
  296. webpage = self._download_webpage(url, display_id)
  297. data_json = self._search_regex(r'window\.__APOLLO_STATE__\s*=\s*(\{.*);\n', webpage, 'json')
  298. data = self._parse_json(data_json, display_id)
  299. res = {
  300. 'id': video_id,
  301. 'display_id': display_id,
  302. }
  303. formats = []
  304. subtitles = {}
  305. geoblocked = False
  306. for widget in data.values():
  307. if widget.get('_geoblocked') is True:
  308. geoblocked = True
  309. if '_duration' in widget:
  310. res['duration'] = int_or_none(widget['_duration'])
  311. if 'clipTitle' in widget:
  312. res['title'] = widget['clipTitle']
  313. if '_previewImage' in widget:
  314. res['thumbnail'] = widget['_previewImage']
  315. if 'broadcastedOn' in widget:
  316. res['timestamp'] = unified_timestamp(widget['broadcastedOn'])
  317. if 'synopsis' in widget:
  318. res['description'] = widget['synopsis']
  319. subtitle_url = url_or_none(widget.get('_subtitleUrl'))
  320. if subtitle_url:
  321. subtitles.setdefault('de', []).append({
  322. 'ext': 'ttml',
  323. 'url': subtitle_url,
  324. })
  325. if '_quality' in widget:
  326. format_url = url_or_none(try_get(
  327. widget, lambda x: x['_stream']['json'][0]))
  328. if not format_url:
  329. continue
  330. ext = determine_ext(format_url)
  331. if ext == 'f4m':
  332. formats.extend(self._extract_f4m_formats(
  333. format_url + '?hdcore=3.11.0',
  334. video_id, f4m_id='hds', fatal=False))
  335. elif ext == 'm3u8':
  336. formats.extend(self._extract_m3u8_formats(
  337. format_url, video_id, 'mp4', m3u8_id='hls',
  338. fatal=False))
  339. else:
  340. # HTTP formats are not available when geoblocked is True,
  341. # other formats are fine though
  342. if geoblocked:
  343. continue
  344. quality = str_or_none(widget.get('_quality'))
  345. formats.append({
  346. 'format_id': ('http-' + quality) if quality else 'http',
  347. 'url': format_url,
  348. 'preference': 10, # Plain HTTP, that's nice
  349. })
  350. if not formats and geoblocked:
  351. self.raise_geo_restricted(
  352. msg='This video is not available due to geoblocking',
  353. countries=['DE'])
  354. self._sort_formats(formats)
  355. res.update({
  356. 'subtitles': subtitles,
  357. 'formats': formats,
  358. })
  359. return res