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.

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