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.

284 lines
11 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 ..compat import compat_str
  7. from ..utils import (
  8. determine_ext,
  9. ExtractorError,
  10. qualities,
  11. int_or_none,
  12. parse_duration,
  13. unified_strdate,
  14. xpath_text,
  15. update_url_query,
  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)/(?:.*/)(?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. # audio
  37. 'url': 'http://www.ardmediathek.de/tv/WDR-H%C3%B6rspiel-Speicher/Tod-eines-Fu%C3%9Fballers/WDR-3/Audio-Podcast?documentId=28488308&bcastId=23074086',
  38. 'only_matching': True,
  39. }, {
  40. 'url': 'http://mediathek.daserste.de/sendungen_a-z/328454_anne-will/22429276_vertrauen-ist-gut-spionieren-ist-besser-geht',
  41. 'only_matching': True,
  42. }, {
  43. # audio
  44. 'url': 'http://mediathek.rbb-online.de/radio/Hörspiel/Vor-dem-Fest/kulturradio/Audio?documentId=30796318&topRessort=radio&bcastId=9839158',
  45. 'only_matching': True,
  46. }]
  47. def _extract_media_info(self, media_info_url, webpage, video_id):
  48. media_info = self._download_json(
  49. media_info_url, video_id, 'Downloading media JSON')
  50. formats = self._extract_formats(media_info, video_id)
  51. if not formats:
  52. if '"fsk"' in webpage:
  53. raise ExtractorError(
  54. 'This video is only available after 20:00', expected=True)
  55. elif media_info.get('_geoblocked'):
  56. raise ExtractorError('This video is not available due to geo restriction', expected=True)
  57. self._sort_formats(formats)
  58. duration = int_or_none(media_info.get('_duration'))
  59. thumbnail = media_info.get('_previewImage')
  60. is_live = media_info.get('_isLive') is True
  61. subtitles = {}
  62. subtitle_url = media_info.get('_subtitleUrl')
  63. if subtitle_url:
  64. subtitles['de'] = [{
  65. 'ext': 'ttml',
  66. 'url': subtitle_url,
  67. }]
  68. return {
  69. 'id': video_id,
  70. 'duration': duration,
  71. 'thumbnail': thumbnail,
  72. 'is_live': is_live,
  73. 'formats': formats,
  74. 'subtitles': subtitles,
  75. }
  76. def _extract_formats(self, media_info, video_id):
  77. type_ = media_info.get('_type')
  78. media_array = media_info.get('_mediaArray', [])
  79. formats = []
  80. for num, media in enumerate(media_array):
  81. for stream in media.get('_mediaStreamArray', []):
  82. stream_urls = stream.get('_stream')
  83. if not stream_urls:
  84. continue
  85. if not isinstance(stream_urls, list):
  86. stream_urls = [stream_urls]
  87. quality = stream.get('_quality')
  88. server = stream.get('_server')
  89. for stream_url in stream_urls:
  90. if not isinstance(stream_url, compat_str) or '//' not in stream_url:
  91. continue
  92. ext = determine_ext(stream_url)
  93. if quality != 'auto' and ext in ('f4m', 'm3u8'):
  94. continue
  95. if ext == 'f4m':
  96. formats.extend(self._extract_f4m_formats(
  97. update_url_query(stream_url, {
  98. 'hdcore': '3.1.1',
  99. 'plugin': 'aasp-3.1.1.69.124'
  100. }),
  101. video_id, f4m_id='hds', fatal=False))
  102. elif ext == 'm3u8':
  103. formats.extend(self._extract_m3u8_formats(
  104. stream_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  105. else:
  106. if server and server.startswith('rtmp'):
  107. f = {
  108. 'url': server,
  109. 'play_path': stream_url,
  110. 'format_id': 'a%s-rtmp-%s' % (num, quality),
  111. }
  112. else:
  113. f = {
  114. 'url': stream_url,
  115. 'format_id': 'a%s-%s-%s' % (num, ext, quality)
  116. }
  117. m = re.search(r'_(?P<width>\d+)x(?P<height>\d+)\.mp4$', stream_url)
  118. if m:
  119. f.update({
  120. 'width': int(m.group('width')),
  121. 'height': int(m.group('height')),
  122. })
  123. if type_ == 'audio':
  124. f['vcodec'] = 'none'
  125. formats.append(f)
  126. return formats
  127. def _real_extract(self, url):
  128. # determine video id from url
  129. m = re.match(self._VALID_URL, url)
  130. document_id = None
  131. numid = re.search(r'documentId=([0-9]+)', url)
  132. if numid:
  133. document_id = video_id = numid.group(1)
  134. else:
  135. video_id = m.group('video_id')
  136. webpage = self._download_webpage(url, video_id)
  137. ERRORS = (
  138. ('>Leider liegt eine Störung vor.', 'Video %s is unavailable'),
  139. ('>Der gewünschte Beitrag ist nicht mehr verfügbar.<',
  140. 'Video %s is no longer available'),
  141. )
  142. for pattern, message in ERRORS:
  143. if pattern in webpage:
  144. raise ExtractorError(message % video_id, expected=True)
  145. if re.search(r'[\?&]rss($|[=&])', url):
  146. doc = compat_etree_fromstring(webpage.encode('utf-8'))
  147. if doc.tag == 'rss':
  148. return GenericIE()._extract_rss(url, video_id, doc)
  149. title = self._html_search_regex(
  150. [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
  151. r'<meta name="dcterms\.title" content="(.*?)"/>',
  152. r'<h4 class="headline">(.*?)</h4>'],
  153. webpage, 'title')
  154. description = self._html_search_meta(
  155. 'dcterms.abstract', webpage, 'description', default=None)
  156. if description is None:
  157. description = self._html_search_meta(
  158. 'description', webpage, 'meta description')
  159. # Thumbnail is sometimes not present.
  160. # It is in the mobile version, but that seems to use a different URL
  161. # structure altogether.
  162. thumbnail = self._og_search_thumbnail(webpage, default=None)
  163. media_streams = re.findall(r'''(?x)
  164. mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
  165. "([^"]+)"''', webpage)
  166. if media_streams:
  167. QUALITIES = qualities(['lo', 'hi', 'hq'])
  168. formats = []
  169. for furl in set(media_streams):
  170. if furl.endswith('.f4m'):
  171. fid = 'f4m'
  172. else:
  173. fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
  174. fid = fid_m.group(1) if fid_m else None
  175. formats.append({
  176. 'quality': QUALITIES(fid),
  177. 'format_id': fid,
  178. 'url': furl,
  179. })
  180. self._sort_formats(formats)
  181. info = {
  182. 'formats': formats,
  183. }
  184. else: # request JSON file
  185. if not document_id:
  186. video_id = self._search_regex(
  187. r'/play/(?:config|media)/(\d+)', webpage, 'media id')
  188. info = self._extract_media_info(
  189. 'http://www.ardmediathek.de/play/media/%s' % video_id,
  190. webpage, video_id)
  191. info.update({
  192. 'id': video_id,
  193. 'title': self._live_title(title) if info.get('is_live') else title,
  194. 'description': description,
  195. 'thumbnail': thumbnail,
  196. })
  197. return info
  198. class ARDIE(InfoExtractor):
  199. _VALID_URL = r'(?P<mainurl>https?://(www\.)?daserste\.de/[^?#]+/videos/(?P<display_id>[^/?#]+)-(?P<id>[0-9]+))\.html'
  200. _TESTS = [{
  201. # available till 14.02.2019
  202. 'url': 'http://www.daserste.de/information/talk/maischberger/videos/das-groko-drama-zerlegen-sich-die-volksparteien-video-102.html',
  203. 'md5': '8e4ec85f31be7c7fc08a26cdbc5a1f49',
  204. 'info_dict': {
  205. 'display_id': 'das-groko-drama-zerlegen-sich-die-volksparteien-video',
  206. 'id': '102',
  207. 'ext': 'mp4',
  208. 'duration': 4435.0,
  209. 'title': 'Das GroKo-Drama: Zerlegen sich die Volksparteien?',
  210. 'upload_date': '20180214',
  211. 'thumbnail': r're:^https?://.*\.jpg$',
  212. },
  213. }, {
  214. 'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
  215. 'only_matching': True,
  216. }]
  217. def _real_extract(self, url):
  218. mobj = re.match(self._VALID_URL, url)
  219. display_id = mobj.group('display_id')
  220. player_url = mobj.group('mainurl') + '~playerXml.xml'
  221. doc = self._download_xml(player_url, display_id)
  222. video_node = doc.find('./video')
  223. upload_date = unified_strdate(xpath_text(
  224. video_node, './broadcastDate'))
  225. thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
  226. formats = []
  227. for a in video_node.findall('.//asset'):
  228. f = {
  229. 'format_id': a.attrib['type'],
  230. 'width': int_or_none(a.find('./frameWidth').text),
  231. 'height': int_or_none(a.find('./frameHeight').text),
  232. 'vbr': int_or_none(a.find('./bitrateVideo').text),
  233. 'abr': int_or_none(a.find('./bitrateAudio').text),
  234. 'vcodec': a.find('./codecVideo').text,
  235. 'tbr': int_or_none(a.find('./totalBitrate').text),
  236. }
  237. if a.find('./serverPrefix').text:
  238. f['url'] = a.find('./serverPrefix').text
  239. f['playpath'] = a.find('./fileName').text
  240. else:
  241. f['url'] = a.find('./fileName').text
  242. formats.append(f)
  243. self._sort_formats(formats)
  244. return {
  245. 'id': mobj.group('id'),
  246. 'formats': formats,
  247. 'display_id': display_id,
  248. 'title': video_node.find('./title').text,
  249. 'duration': parse_duration(video_node.find('./duration').text),
  250. 'upload_date': upload_date,
  251. 'thumbnail': thumbnail,
  252. }