ard.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. import functools
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. OnDemandPagedList,
  6. bug_reports_message,
  7. determine_ext,
  8. int_or_none,
  9. join_nonempty,
  10. jwt_decode_hs256,
  11. make_archive_id,
  12. parse_duration,
  13. parse_iso8601,
  14. remove_start,
  15. str_or_none,
  16. unified_strdate,
  17. update_url_query,
  18. url_or_none,
  19. xpath_text,
  20. )
  21. from ..utils.traversal import traverse_obj
  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. self.raise_no_formats(
  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, metadata_available=True)
  38. subtitles = {}
  39. subtitle_url = media_info.get('_subtitleUrl')
  40. if subtitle_url:
  41. subtitles['de'] = [{
  42. 'ext': 'ttml',
  43. 'url': subtitle_url,
  44. }, {
  45. 'ext': 'vtt',
  46. 'url': subtitle_url.replace('/ebutt/', '/webvtt/') + '.vtt',
  47. }]
  48. return {
  49. 'id': video_id,
  50. 'duration': int_or_none(media_info.get('_duration')),
  51. 'thumbnail': media_info.get('_previewImage'),
  52. 'is_live': media_info.get('_isLive') is True,
  53. 'formats': formats,
  54. 'subtitles': subtitles,
  55. }
  56. def _extract_formats(self, media_info, video_id):
  57. type_ = media_info.get('_type')
  58. media_array = media_info.get('_mediaArray', [])
  59. formats = []
  60. for num, media in enumerate(media_array):
  61. for stream in media.get('_mediaStreamArray', []):
  62. stream_urls = stream.get('_stream')
  63. if not stream_urls:
  64. continue
  65. if not isinstance(stream_urls, list):
  66. stream_urls = [stream_urls]
  67. quality = stream.get('_quality')
  68. server = stream.get('_server')
  69. for stream_url in stream_urls:
  70. if not url_or_none(stream_url):
  71. continue
  72. ext = determine_ext(stream_url)
  73. if quality != 'auto' and ext in ('f4m', 'm3u8'):
  74. continue
  75. if ext == 'f4m':
  76. formats.extend(self._extract_f4m_formats(
  77. update_url_query(stream_url, {
  78. 'hdcore': '3.1.1',
  79. 'plugin': 'aasp-3.1.1.69.124',
  80. }), video_id, f4m_id='hds', fatal=False))
  81. elif ext == 'm3u8':
  82. formats.extend(self._extract_m3u8_formats(
  83. stream_url, video_id, 'mp4', 'm3u8_native',
  84. m3u8_id='hls', fatal=False))
  85. else:
  86. if server and server.startswith('rtmp'):
  87. f = {
  88. 'url': server,
  89. 'play_path': stream_url,
  90. 'format_id': f'a{num}-rtmp-{quality}',
  91. }
  92. else:
  93. f = {
  94. 'url': stream_url,
  95. 'format_id': f'a{num}-{ext}-{quality}',
  96. }
  97. m = re.search(
  98. r'_(?P<width>\d+)x(?P<height>\d+)\.mp4$',
  99. stream_url)
  100. if m:
  101. f.update({
  102. 'width': int(m.group('width')),
  103. 'height': int(m.group('height')),
  104. })
  105. if type_ == 'audio':
  106. f['vcodec'] = 'none'
  107. formats.append(f)
  108. return formats
  109. class ARDIE(InfoExtractor):
  110. _VALID_URL = r'(?P<mainurl>https?://(?:www\.)?daserste\.de/(?:[^/?#&]+/)+(?P<id>[^/?#&]+))\.html'
  111. _TESTS = [{
  112. # available till 7.12.2023
  113. 'url': 'https://www.daserste.de/information/talk/maischberger/videos/maischberger-video-424.html',
  114. 'md5': '94812e6438488fb923c361a44469614b',
  115. 'info_dict': {
  116. 'id': 'maischberger-video-424',
  117. 'display_id': 'maischberger-video-424',
  118. 'ext': 'mp4',
  119. 'duration': 4452.0,
  120. 'title': 'maischberger am 07.12.2022',
  121. 'upload_date': '20221207',
  122. 'thumbnail': r're:^https?://.*\.jpg$',
  123. },
  124. }, {
  125. 'url': 'https://www.daserste.de/information/politik-weltgeschehen/morgenmagazin/videosextern/dominik-kahun-aus-der-nhl-direkt-zur-weltmeisterschaft-100.html',
  126. 'only_matching': True,
  127. }, {
  128. 'url': 'https://www.daserste.de/information/nachrichten-wetter/tagesthemen/videosextern/tagesthemen-17736.html',
  129. 'only_matching': True,
  130. }, {
  131. 'url': 'https://www.daserste.de/unterhaltung/serie/in-aller-freundschaft-die-jungen-aerzte/videos/diversity-tag-sanam-afrashteh100.html',
  132. 'only_matching': True,
  133. }, {
  134. 'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
  135. 'only_matching': True,
  136. }, {
  137. 'url': 'https://www.daserste.de/unterhaltung/serie/in-aller-freundschaft-die-jungen-aerzte/Drehpause-100.html',
  138. 'only_matching': True,
  139. }, {
  140. 'url': 'https://www.daserste.de/unterhaltung/film/filmmittwoch-im-ersten/videos/making-ofwendezeit-video-100.html',
  141. 'only_matching': True,
  142. }]
  143. def _real_extract(self, url):
  144. mobj = self._match_valid_url(url)
  145. display_id = mobj.group('id')
  146. player_url = mobj.group('mainurl') + '~playerXml.xml'
  147. doc = self._download_xml(player_url, display_id)
  148. video_node = doc.find('./video')
  149. upload_date = unified_strdate(xpath_text(
  150. video_node, './broadcastDate'))
  151. thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
  152. formats = []
  153. for a in video_node.findall('.//asset'):
  154. file_name = xpath_text(a, './fileName', default=None)
  155. if not file_name:
  156. continue
  157. format_type = a.attrib.get('type')
  158. format_url = url_or_none(file_name)
  159. if format_url:
  160. ext = determine_ext(file_name)
  161. if ext == 'm3u8':
  162. formats.extend(self._extract_m3u8_formats(
  163. format_url, display_id, 'mp4', entry_protocol='m3u8_native',
  164. m3u8_id=format_type or 'hls', fatal=False))
  165. continue
  166. elif ext == 'f4m':
  167. formats.extend(self._extract_f4m_formats(
  168. update_url_query(format_url, {'hdcore': '3.7.0'}),
  169. display_id, f4m_id=format_type or 'hds', fatal=False))
  170. continue
  171. f = {
  172. 'format_id': format_type,
  173. 'width': int_or_none(xpath_text(a, './frameWidth')),
  174. 'height': int_or_none(xpath_text(a, './frameHeight')),
  175. 'vbr': int_or_none(xpath_text(a, './bitrateVideo')),
  176. 'abr': int_or_none(xpath_text(a, './bitrateAudio')),
  177. 'vcodec': xpath_text(a, './codecVideo'),
  178. 'tbr': int_or_none(xpath_text(a, './totalBitrate')),
  179. }
  180. server_prefix = xpath_text(a, './serverPrefix', default=None)
  181. if server_prefix:
  182. f.update({
  183. 'url': server_prefix,
  184. 'playpath': file_name,
  185. })
  186. else:
  187. if not format_url:
  188. continue
  189. f['url'] = format_url
  190. formats.append(f)
  191. _SUB_FORMATS = (
  192. ('./dataTimedText', 'ttml'),
  193. ('./dataTimedTextNoOffset', 'ttml'),
  194. ('./dataTimedTextVtt', 'vtt'),
  195. )
  196. subtitles = {}
  197. for subsel, subext in _SUB_FORMATS:
  198. for node in video_node.findall(subsel):
  199. subtitles.setdefault('de', []).append({
  200. 'url': node.attrib['url'],
  201. 'ext': subext,
  202. })
  203. return {
  204. 'id': xpath_text(video_node, './videoId', default=display_id),
  205. 'formats': formats,
  206. 'subtitles': subtitles,
  207. 'display_id': display_id,
  208. 'title': video_node.find('./title').text,
  209. 'duration': parse_duration(video_node.find('./duration').text),
  210. 'upload_date': upload_date,
  211. 'thumbnail': thumbnail,
  212. }
  213. class ARDBetaMediathekIE(InfoExtractor):
  214. IE_NAME = 'ARDMediathek'
  215. _VALID_URL = r'''(?x)https://
  216. (?:(?:beta|www)\.)?ardmediathek\.de/
  217. (?:[^/]+/)?
  218. (?:player|live|video)/
  219. (?:[^?#]+/)?
  220. (?P<id>[a-zA-Z0-9]+)
  221. /?(?:[?#]|$)'''
  222. _GEO_COUNTRIES = ['DE']
  223. _TOKEN_URL = 'https://sso.ardmediathek.de/sso/token'
  224. _TESTS = [{
  225. 'url': 'https://www.ardmediathek.de/video/filme-im-mdr/liebe-auf-vier-pfoten/mdr-fernsehen/Y3JpZDovL21kci5kZS9zZW5kdW5nLzI4MjA0MC80MjIwOTEtNDAyNTM0',
  226. 'md5': 'b6e8ab03f2bcc6e1f9e6cef25fcc03c4',
  227. 'info_dict': {
  228. 'display_id': 'Y3JpZDovL21kci5kZS9zZW5kdW5nLzI4MjA0MC80MjIwOTEtNDAyNTM0',
  229. 'id': '12939099',
  230. 'title': 'Liebe auf vier Pfoten',
  231. 'description': r're:^Claudia Schmitt, Anwältin in Salzburg',
  232. 'duration': 5222,
  233. 'thumbnail': 'https://api.ardmediathek.de/image-service/images/urn:ard:image:aee7cbf8f06de976?w=960&ch=ae4d0f2ee47d8b9b',
  234. 'timestamp': 1701343800,
  235. 'upload_date': '20231130',
  236. 'ext': 'mp4',
  237. 'episode': 'Liebe auf vier Pfoten',
  238. 'series': 'Filme im MDR',
  239. 'age_limit': 0,
  240. 'channel': 'MDR',
  241. '_old_archive_ids': ['ardbetamediathek Y3JpZDovL21kci5kZS9zZW5kdW5nLzI4MjA0MC80MjIwOTEtNDAyNTM0'],
  242. },
  243. }, {
  244. 'url': 'https://www.ardmediathek.de/mdr/video/die-robuste-roswita/Y3JpZDovL21kci5kZS9iZWl0cmFnL2Ntcy84MWMxN2MzZC0wMjkxLTRmMzUtODk4ZS0wYzhlOWQxODE2NGI/',
  245. 'md5': 'a1dc75a39c61601b980648f7c9f9f71d',
  246. 'info_dict': {
  247. 'display_id': 'die-robuste-roswita',
  248. 'id': '78566716',
  249. 'title': 'Die robuste Roswita',
  250. 'description': r're:^Der Mord.*totgeglaubte Ehefrau Roswita',
  251. 'duration': 5316,
  252. 'thumbnail': 'https://img.ardmediathek.de/standard/00/78/56/67/84/575672121/16x9/960?mandant=ard',
  253. 'timestamp': 1596658200,
  254. 'upload_date': '20200805',
  255. 'ext': 'mp4',
  256. },
  257. 'skip': 'Error',
  258. }, {
  259. 'url': 'https://www.ardmediathek.de/video/tagesschau-oder-tagesschau-20-00-uhr/das-erste/Y3JpZDovL2Rhc2Vyc3RlLmRlL3RhZ2Vzc2NoYXUvZmM4ZDUxMjgtOTE0ZC00Y2MzLTgzNzAtNDZkNGNiZWJkOTll',
  260. 'md5': '1e73ded21cb79bac065117e80c81dc88',
  261. 'info_dict': {
  262. 'id': '10049223',
  263. 'ext': 'mp4',
  264. 'title': 'tagesschau, 20:00 Uhr',
  265. 'timestamp': 1636398000,
  266. 'description': 'md5:39578c7b96c9fe50afdf5674ad985e6b',
  267. 'upload_date': '20211108',
  268. 'display_id': 'Y3JpZDovL2Rhc2Vyc3RlLmRlL3RhZ2Vzc2NoYXUvZmM4ZDUxMjgtOTE0ZC00Y2MzLTgzNzAtNDZkNGNiZWJkOTll',
  269. 'duration': 915,
  270. 'episode': 'tagesschau, 20:00 Uhr',
  271. 'series': 'tagesschau',
  272. 'thumbnail': 'https://api.ardmediathek.de/image-service/images/urn:ard:image:fbb21142783b0a49?w=960&ch=ee69108ae344f678',
  273. 'channel': 'ARD-Aktuell',
  274. '_old_archive_ids': ['ardbetamediathek Y3JpZDovL2Rhc2Vyc3RlLmRlL3RhZ2Vzc2NoYXUvZmM4ZDUxMjgtOTE0ZC00Y2MzLTgzNzAtNDZkNGNiZWJkOTll'],
  275. },
  276. }, {
  277. 'url': 'https://www.ardmediathek.de/video/7-tage/7-tage-unter-harten-jungs/hr-fernsehen/N2I2YmM5MzgtNWFlOS00ZGFlLTg2NzMtYzNjM2JlNjk4MDg3',
  278. 'md5': 'c428b9effff18ff624d4f903bda26315',
  279. 'info_dict': {
  280. 'id': '94834686',
  281. 'ext': 'mp4',
  282. 'duration': 2700,
  283. 'episode': '7 Tage ... unter harten Jungs',
  284. 'description': 'md5:0f215470dcd2b02f59f4bd10c963f072',
  285. 'upload_date': '20231005',
  286. 'timestamp': 1696491171,
  287. 'display_id': 'N2I2YmM5MzgtNWFlOS00ZGFlLTg2NzMtYzNjM2JlNjk4MDg3',
  288. 'series': '7 Tage ...',
  289. 'channel': 'HR',
  290. 'thumbnail': 'https://api.ardmediathek.de/image-service/images/urn:ard:image:f6e6d5ffac41925c?w=960&ch=fa32ba69bc87989a',
  291. 'title': '7 Tage ... unter harten Jungs',
  292. '_old_archive_ids': ['ardbetamediathek N2I2YmM5MzgtNWFlOS00ZGFlLTg2NzMtYzNjM2JlNjk4MDg3'],
  293. },
  294. }, {
  295. 'url': 'https://beta.ardmediathek.de/ard/video/Y3JpZDovL2Rhc2Vyc3RlLmRlL3RhdG9ydC9mYmM4NGM1NC0xNzU4LTRmZGYtYWFhZS0wYzcyZTIxNGEyMDE',
  296. 'only_matching': True,
  297. }, {
  298. 'url': 'https://ardmediathek.de/ard/video/saartalk/saartalk-gesellschaftsgift-haltung-gegen-hass/sr-fernsehen/Y3JpZDovL3NyLW9ubGluZS5kZS9TVF84MTY4MA/',
  299. 'only_matching': True,
  300. }, {
  301. 'url': 'https://www.ardmediathek.de/ard/video/trailer/private-eyes-s01-e01/one/Y3JpZDovL3dkci5kZS9CZWl0cmFnLTE1MTgwYzczLWNiMTEtNGNkMS1iMjUyLTg5MGYzOWQxZmQ1YQ/',
  302. 'only_matching': True,
  303. }, {
  304. 'url': 'https://www.ardmediathek.de/ard/player/Y3JpZDovL3N3ci5kZS9hZXgvbzEwNzE5MTU/',
  305. 'only_matching': True,
  306. }, {
  307. 'url': 'https://www.ardmediathek.de/swr/live/Y3JpZDovL3N3ci5kZS8xMzQ4MTA0Mg',
  308. 'only_matching': True,
  309. }, {
  310. 'url': 'https://www.ardmediathek.de/video/coronavirus-update-ndr-info/astrazeneca-kurz-lockdown-und-pims-syndrom-81/ndr/Y3JpZDovL25kci5kZS84NzE0M2FjNi0wMWEwLTQ5ODEtOTE5NS1mOGZhNzdhOTFmOTI/',
  311. 'only_matching': True,
  312. }]
  313. def _extract_episode_info(self, title):
  314. patterns = [
  315. # Pattern for title like "Homo sapiens (S06/E07) - Originalversion"
  316. # from: https://www.ardmediathek.de/one/sendung/doctor-who/Y3JpZDovL3dkci5kZS9vbmUvZG9jdG9yIHdobw
  317. r'.*(?P<ep_info> \(S(?P<season_number>\d+)/E(?P<episode_number>\d+)\)).*',
  318. # E.g.: title="Fritjof aus Norwegen (2) (AD)"
  319. # from: https://www.ardmediathek.de/ard/sammlung/der-krieg-und-ich/68cMkqJdllm639Skj4c7sS/
  320. r'.*(?P<ep_info> \((?:Folge |Teil )?(?P<episode_number>\d+)(?:/\d+)?\)).*',
  321. r'.*(?P<ep_info>Folge (?P<episode_number>\d+)(?:\:| -|) )\"(?P<episode>.+)\".*',
  322. # E.g.: title="Folge 25/42: Symmetrie"
  323. # from: https://www.ardmediathek.de/ard/video/grips-mathe/folge-25-42-symmetrie/ard-alpha/Y3JpZDovL2JyLmRlL3ZpZGVvLzMyYzI0ZjczLWQ1N2MtNDAxNC05ZmZhLTFjYzRkZDA5NDU5OQ/
  324. # E.g.: title="Folge 1063 - Vertrauen"
  325. # from: https://www.ardmediathek.de/ard/sendung/die-fallers/Y3JpZDovL3N3ci5kZS8yMzAyMDQ4/
  326. r'.*(?P<ep_info>Folge (?P<episode_number>\d+)(?:/\d+)?(?:\:| -|) ).*',
  327. # As a fallback use the full title
  328. r'(?P<title>.*)',
  329. ]
  330. return traverse_obj(patterns, (..., {functools.partial(re.match, string=title)}, {
  331. 'season_number': ('season_number', {int_or_none}),
  332. 'episode_number': ('episode_number', {int_or_none}),
  333. 'episode': ((
  334. ('episode', {str_or_none}),
  335. ('ep_info', {lambda x: title.replace(x, '')}),
  336. ('title', {str}),
  337. ), {str.strip}),
  338. }), get_all=False)
  339. def _real_extract(self, url):
  340. display_id = self._match_id(url)
  341. query = {'embedded': 'false', 'mcV6': 'true'}
  342. headers = {}
  343. if self._get_cookies(self._TOKEN_URL).get('ams'):
  344. token = self._download_json(
  345. self._TOKEN_URL, display_id, 'Fetching token for age verification',
  346. 'Unable to fetch age verification token', fatal=False)
  347. id_token = traverse_obj(token, ('idToken', {str}))
  348. decoded_token = traverse_obj(id_token, ({jwt_decode_hs256}, {dict}))
  349. user_id = traverse_obj(decoded_token, (('user_id', 'sub'), {str}), get_all=False)
  350. if not user_id:
  351. self.report_warning('Unable to extract token, continuing without authentication')
  352. else:
  353. headers['x-authorization'] = f'Bearer {id_token}'
  354. query['userId'] = user_id
  355. if decoded_token.get('age_rating') != 18:
  356. self.report_warning('Account is not verified as 18+; video may be unavailable')
  357. page_data = self._download_json(
  358. f'https://api.ardmediathek.de/page-gateway/pages/ard/item/{display_id}',
  359. display_id, query=query, headers=headers)
  360. # For user convenience we use the old contentId instead of the longer crid
  361. # Ref: https://github.com/yt-dlp/yt-dlp/issues/8731#issuecomment-1874398283
  362. old_id = traverse_obj(page_data, ('tracking', 'atiCustomVars', 'contentId', {int}))
  363. if old_id is not None:
  364. video_id = str(old_id)
  365. archive_ids = [make_archive_id(ARDBetaMediathekIE, display_id)]
  366. else:
  367. self.report_warning(f'Could not extract contentId{bug_reports_message()}')
  368. video_id = display_id
  369. archive_ids = None
  370. player_data = traverse_obj(
  371. page_data, ('widgets', lambda _, v: v['type'] in ('player_ondemand', 'player_live'), {dict}), get_all=False)
  372. is_live = player_data.get('type') == 'player_live'
  373. media_data = traverse_obj(player_data, ('mediaCollection', 'embedded', {dict}))
  374. if player_data.get('blockedByFsk'):
  375. self.raise_login_required('This video is only available for age verified users or after 22:00')
  376. formats = []
  377. subtitles = {}
  378. for stream in traverse_obj(media_data, ('streams', ..., {dict})):
  379. kind = stream.get('kind')
  380. # Prioritize main stream over sign language and others
  381. preference = 1 if kind == 'main' else None
  382. for media in traverse_obj(stream, ('media', lambda _, v: url_or_none(v['url']))):
  383. media_url = media['url']
  384. audio_kind = traverse_obj(media, (
  385. 'audios', 0, 'kind', {str}), default='').replace('standard', '')
  386. lang_code = traverse_obj(media, ('audios', 0, 'languageCode', {str})) or 'deu'
  387. lang = join_nonempty(lang_code, audio_kind)
  388. language_preference = 10 if lang == 'deu' else -10
  389. if determine_ext(media_url) == 'm3u8':
  390. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  391. media_url, video_id, m3u8_id=f'hls-{kind}', preference=preference, fatal=False, live=is_live)
  392. for f in fmts:
  393. f['language'] = lang
  394. f['language_preference'] = language_preference
  395. formats.extend(fmts)
  396. self._merge_subtitles(subs, target=subtitles)
  397. else:
  398. formats.append({
  399. 'url': media_url,
  400. 'format_id': f'http-{kind}',
  401. 'preference': preference,
  402. 'language': lang,
  403. 'language_preference': language_preference,
  404. **traverse_obj(media, {
  405. 'format_note': ('forcedLabel', {str}),
  406. 'width': ('maxHResolutionPx', {int_or_none}),
  407. 'height': ('maxVResolutionPx', {int_or_none}),
  408. 'vcodec': ('videoCodec', {str}),
  409. }),
  410. })
  411. for sub in traverse_obj(media_data, ('subtitles', ..., {dict})):
  412. for sources in traverse_obj(sub, ('sources', lambda _, v: url_or_none(v['url']))):
  413. subtitles.setdefault(sub.get('languageCode') or 'deu', []).append({
  414. 'url': sources['url'],
  415. 'ext': {'webvtt': 'vtt', 'ebutt': 'ttml'}.get(sources.get('kind')),
  416. })
  417. age_limit = traverse_obj(page_data, ('fskRating', {lambda x: remove_start(x, 'FSK')}, {int_or_none}))
  418. return {
  419. 'id': video_id,
  420. 'display_id': display_id,
  421. 'formats': formats,
  422. 'subtitles': subtitles,
  423. 'is_live': is_live,
  424. 'age_limit': age_limit,
  425. **traverse_obj(media_data, ('meta', {
  426. 'title': 'title',
  427. 'description': 'synopsis',
  428. 'timestamp': ('broadcastedOnDateTime', {parse_iso8601}),
  429. 'series': 'seriesTitle',
  430. 'thumbnail': ('images', 0, 'url', {url_or_none}),
  431. 'duration': ('durationSeconds', {int_or_none}),
  432. 'channel': 'clipSourceName',
  433. })),
  434. **self._extract_episode_info(page_data.get('title')),
  435. '_old_archive_ids': archive_ids,
  436. }
  437. class ARDMediathekCollectionIE(InfoExtractor):
  438. _VALID_URL = r'''(?x)https://
  439. (?:(?:beta|www)\.)?ardmediathek\.de/
  440. (?:[^/?#]+/)?
  441. (?P<playlist>sendung|serie|sammlung)/
  442. (?:(?P<display_id>[^?#]+?)/)?
  443. (?P<id>[a-zA-Z0-9]+)
  444. (?:/(?P<season>\d+)(?:/(?P<version>OV|AD))?)?/?(?:[?#]|$)'''
  445. _GEO_COUNTRIES = ['DE']
  446. _TESTS = [{
  447. 'url': 'https://www.ardmediathek.de/serie/quiz/staffel-1-originalversion/Y3JpZDovL3dkci5kZS9vbmUvcXVpeg/1/OV',
  448. 'info_dict': {
  449. 'id': 'Y3JpZDovL3dkci5kZS9vbmUvcXVpeg_1_OV',
  450. 'display_id': 'quiz/staffel-1-originalversion',
  451. 'title': 'Staffel 1 Originalversion',
  452. },
  453. 'playlist_count': 3,
  454. }, {
  455. 'url': 'https://www.ardmediathek.de/serie/babylon-berlin/staffel-4-mit-audiodeskription/Y3JpZDovL2Rhc2Vyc3RlLmRlL2JhYnlsb24tYmVybGlu/4/AD',
  456. 'info_dict': {
  457. 'id': 'Y3JpZDovL2Rhc2Vyc3RlLmRlL2JhYnlsb24tYmVybGlu_4_AD',
  458. 'display_id': 'babylon-berlin/staffel-4-mit-audiodeskription',
  459. 'title': 'Staffel 4 mit Audiodeskription',
  460. },
  461. 'playlist_count': 12,
  462. }, {
  463. 'url': 'https://www.ardmediathek.de/serie/babylon-berlin/staffel-1/Y3JpZDovL2Rhc2Vyc3RlLmRlL2JhYnlsb24tYmVybGlu/1/',
  464. 'info_dict': {
  465. 'id': 'Y3JpZDovL2Rhc2Vyc3RlLmRlL2JhYnlsb24tYmVybGlu_1',
  466. 'display_id': 'babylon-berlin/staffel-1',
  467. 'title': 'Staffel 1',
  468. },
  469. 'playlist_count': 8,
  470. }, {
  471. 'url': 'https://www.ardmediathek.de/sendung/tatort/Y3JpZDovL2Rhc2Vyc3RlLmRlL3RhdG9ydA',
  472. 'info_dict': {
  473. 'id': 'Y3JpZDovL2Rhc2Vyc3RlLmRlL3RhdG9ydA',
  474. 'display_id': 'tatort',
  475. 'title': 'Tatort',
  476. },
  477. 'playlist_mincount': 500,
  478. }, {
  479. 'url': 'https://www.ardmediathek.de/sammlung/die-kirche-bleibt-im-dorf/5eOHzt8XB2sqeFXbIoJlg2',
  480. 'info_dict': {
  481. 'id': '5eOHzt8XB2sqeFXbIoJlg2',
  482. 'display_id': 'die-kirche-bleibt-im-dorf',
  483. 'title': 'Die Kirche bleibt im Dorf',
  484. 'description': 'Die Kirche bleibt im Dorf',
  485. },
  486. 'playlist_count': 4,
  487. }, {
  488. # playlist of type 'sendung'
  489. 'url': 'https://www.ardmediathek.de/ard/sendung/doctor-who/Y3JpZDovL3dkci5kZS9vbmUvZG9jdG9yIHdobw/',
  490. 'only_matching': True,
  491. }, {
  492. # playlist of type 'serie'
  493. 'url': 'https://www.ardmediathek.de/serie/nachtstreife/staffel-1/Y3JpZDovL3N3ci5kZS9zZGIvc3RJZC8xMjQy/1',
  494. 'only_matching': True,
  495. }, {
  496. # playlist of type 'sammlung'
  497. 'url': 'https://www.ardmediathek.de/ard/sammlung/team-muenster/5JpTzLSbWUAK8184IOvEir/',
  498. 'only_matching': True,
  499. }]
  500. _PAGE_SIZE = 100
  501. def _real_extract(self, url):
  502. playlist_id, display_id, playlist_type, season_number, version = self._match_valid_url(url).group(
  503. 'id', 'display_id', 'playlist', 'season', 'version')
  504. def call_api(page_num):
  505. api_path = 'compilations/ard' if playlist_type == 'sammlung' else 'widgets/ard/asset'
  506. return self._download_json(
  507. f'https://api.ardmediathek.de/page-gateway/{api_path}/{playlist_id}', playlist_id,
  508. f'Downloading playlist page {page_num}', query={
  509. 'pageNumber': page_num,
  510. 'pageSize': self._PAGE_SIZE,
  511. **({
  512. 'seasoned': 'true',
  513. 'seasonNumber': season_number,
  514. 'withOriginalversion': 'true' if version == 'OV' else 'false',
  515. 'withAudiodescription': 'true' if version == 'AD' else 'false',
  516. } if season_number else {}),
  517. })
  518. def fetch_page(page_num):
  519. for item in traverse_obj(call_api(page_num), ('teasers', ..., {dict})):
  520. item_id = traverse_obj(item, ('links', 'target', ('urlId', 'id')), 'id', get_all=False)
  521. if not item_id or item_id == playlist_id:
  522. continue
  523. item_mode = 'sammlung' if item.get('type') == 'compilation' else 'video'
  524. yield self.url_result(
  525. f'https://www.ardmediathek.de/{item_mode}/{item_id}',
  526. ie=(ARDMediathekCollectionIE if item_mode == 'sammlung' else ARDBetaMediathekIE),
  527. **traverse_obj(item, {
  528. 'id': ('id', {str}),
  529. 'title': ('longTitle', {str}),
  530. 'duration': ('duration', {int_or_none}),
  531. 'timestamp': ('broadcastedOn', {parse_iso8601}),
  532. }))
  533. page_data = call_api(0)
  534. full_id = join_nonempty(playlist_id, season_number, version, delim='_')
  535. return self.playlist_result(
  536. OnDemandPagedList(fetch_page, self._PAGE_SIZE), full_id, display_id=display_id,
  537. title=page_data.get('title'), description=page_data.get('synopsis'))