ard.py 15 KB

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