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.

299 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. subtitles = {}
  87. subtitle_url = media_info.get('_subtitleUrl')
  88. if subtitle_url:
  89. subtitles['de'] = [{
  90. 'ext': 'ttml',
  91. 'url': subtitle_url,
  92. }]
  93. return {
  94. 'id': video_id,
  95. 'duration': duration,
  96. 'thumbnail': thumbnail,
  97. 'formats': formats,
  98. 'subtitles': subtitles,
  99. }
  100. def _extract_formats(self, media_info, video_id):
  101. type_ = media_info.get('_type')
  102. media_array = media_info.get('_mediaArray', [])
  103. formats = []
  104. for num, media in enumerate(media_array):
  105. for stream in media.get('_mediaStreamArray', []):
  106. stream_urls = stream.get('_stream')
  107. if not stream_urls:
  108. continue
  109. if not isinstance(stream_urls, list):
  110. stream_urls = [stream_urls]
  111. quality = stream.get('_quality')
  112. server = stream.get('_server')
  113. for stream_url in stream_urls:
  114. ext = determine_ext(stream_url)
  115. if quality != 'auto' and ext in ('f4m', 'm3u8'):
  116. continue
  117. if ext == 'f4m':
  118. formats.extend(self._extract_f4m_formats(
  119. update_url_query(stream_url, {
  120. 'hdcore': '3.1.1',
  121. 'plugin': 'aasp-3.1.1.69.124'
  122. }),
  123. video_id, f4m_id='hds', fatal=False))
  124. elif ext == 'm3u8':
  125. formats.extend(self._extract_m3u8_formats(
  126. stream_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  127. else:
  128. if server and server.startswith('rtmp'):
  129. f = {
  130. 'url': server,
  131. 'play_path': stream_url,
  132. 'format_id': 'a%s-rtmp-%s' % (num, quality),
  133. }
  134. elif stream_url.startswith('http'):
  135. f = {
  136. 'url': stream_url,
  137. 'format_id': 'a%s-%s-%s' % (num, ext, quality)
  138. }
  139. else:
  140. continue
  141. m = re.search(r'_(?P<width>\d+)x(?P<height>\d+)\.mp4$', stream_url)
  142. if m:
  143. f.update({
  144. 'width': int(m.group('width')),
  145. 'height': int(m.group('height')),
  146. })
  147. if type_ == 'audio':
  148. f['vcodec'] = 'none'
  149. formats.append(f)
  150. return formats
  151. def _real_extract(self, url):
  152. # determine video id from url
  153. m = re.match(self._VALID_URL, url)
  154. numid = re.search(r'documentId=([0-9]+)', url)
  155. if numid:
  156. video_id = numid.group(1)
  157. else:
  158. video_id = m.group('video_id')
  159. webpage = self._download_webpage(url, video_id)
  160. ERRORS = (
  161. ('>Leider liegt eine Störung vor.', 'Video %s is unavailable'),
  162. ('>Der gewünschte Beitrag ist nicht mehr verfügbar.<',
  163. 'Video %s is no longer available'),
  164. )
  165. for pattern, message in ERRORS:
  166. if pattern in webpage:
  167. raise ExtractorError(message % video_id, expected=True)
  168. if re.search(r'[\?&]rss($|[=&])', url):
  169. doc = compat_etree_fromstring(webpage.encode('utf-8'))
  170. if doc.tag == 'rss':
  171. return GenericIE()._extract_rss(url, video_id, doc)
  172. title = self._html_search_regex(
  173. [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
  174. r'<meta name="dcterms.title" content="(.*?)"/>',
  175. r'<h4 class="headline">(.*?)</h4>'],
  176. webpage, 'title')
  177. description = self._html_search_meta(
  178. 'dcterms.abstract', webpage, 'description', default=None)
  179. if description is None:
  180. description = self._html_search_meta(
  181. 'description', webpage, 'meta description')
  182. # Thumbnail is sometimes not present.
  183. # It is in the mobile version, but that seems to use a different URL
  184. # structure altogether.
  185. thumbnail = self._og_search_thumbnail(webpage, default=None)
  186. media_streams = re.findall(r'''(?x)
  187. mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
  188. "([^"]+)"''', webpage)
  189. if media_streams:
  190. QUALITIES = qualities(['lo', 'hi', 'hq'])
  191. formats = []
  192. for furl in set(media_streams):
  193. if furl.endswith('.f4m'):
  194. fid = 'f4m'
  195. else:
  196. fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
  197. fid = fid_m.group(1) if fid_m else None
  198. formats.append({
  199. 'quality': QUALITIES(fid),
  200. 'format_id': fid,
  201. 'url': furl,
  202. })
  203. self._sort_formats(formats)
  204. info = {
  205. 'formats': formats,
  206. }
  207. else: # request JSON file
  208. info = self._extract_media_info(
  209. 'http://www.ardmediathek.de/play/media/%s' % video_id, webpage, video_id)
  210. info.update({
  211. 'id': video_id,
  212. 'title': title,
  213. 'description': description,
  214. 'thumbnail': thumbnail,
  215. })
  216. return info
  217. class ARDIE(InfoExtractor):
  218. _VALID_URL = r'(?P<mainurl>https?://(www\.)?daserste\.de/[^?#]+/videos/(?P<display_id>[^/?#]+)-(?P<id>[0-9]+))\.html'
  219. _TEST = {
  220. 'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
  221. 'md5': 'd216c3a86493f9322545e045ddc3eb35',
  222. 'info_dict': {
  223. 'display_id': 'die-story-im-ersten-mission-unter-falscher-flagge',
  224. 'id': '100',
  225. 'ext': 'mp4',
  226. 'duration': 2600,
  227. 'title': 'Die Story im Ersten: Mission unter falscher Flagge',
  228. 'upload_date': '20140804',
  229. 'thumbnail': r're:^https?://.*\.jpg$',
  230. },
  231. 'skip': 'HTTP Error 404: Not Found',
  232. }
  233. def _real_extract(self, url):
  234. mobj = re.match(self._VALID_URL, url)
  235. display_id = mobj.group('display_id')
  236. player_url = mobj.group('mainurl') + '~playerXml.xml'
  237. doc = self._download_xml(player_url, display_id)
  238. video_node = doc.find('./video')
  239. upload_date = unified_strdate(xpath_text(
  240. video_node, './broadcastDate'))
  241. thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
  242. formats = []
  243. for a in video_node.findall('.//asset'):
  244. f = {
  245. 'format_id': a.attrib['type'],
  246. 'width': int_or_none(a.find('./frameWidth').text),
  247. 'height': int_or_none(a.find('./frameHeight').text),
  248. 'vbr': int_or_none(a.find('./bitrateVideo').text),
  249. 'abr': int_or_none(a.find('./bitrateAudio').text),
  250. 'vcodec': a.find('./codecVideo').text,
  251. 'tbr': int_or_none(a.find('./totalBitrate').text),
  252. }
  253. if a.find('./serverPrefix').text:
  254. f['url'] = a.find('./serverPrefix').text
  255. f['playpath'] = a.find('./fileName').text
  256. else:
  257. f['url'] = a.find('./fileName').text
  258. formats.append(f)
  259. self._sort_formats(formats)
  260. return {
  261. 'id': mobj.group('id'),
  262. 'formats': formats,
  263. 'display_id': display_id,
  264. 'title': video_node.find('./title').text,
  265. 'duration': parse_duration(video_node.find('./duration').text),
  266. 'upload_date': upload_date,
  267. 'thumbnail': thumbnail,
  268. }