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.

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