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.

406 lines
15 KiB

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