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.

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