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.

295 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. if '>Der gewünschte Beitrag ist nicht mehr verfügbar.<' in webpage:
  161. raise ExtractorError('Video %s is no longer available' % video_id, expected=True)
  162. if 'Diese Sendung ist für Jugendliche unter 12 Jahren nicht geeignet. Der Clip ist deshalb nur von 20 bis 6 Uhr verfügbar.' in webpage:
  163. raise ExtractorError('This program is only suitable for those aged 12 and older. Video %s is therefore only available between 20 pm and 6 am.' % video_id, expected=True)
  164. if re.search(r'[\?&]rss($|[=&])', url):
  165. doc = compat_etree_fromstring(webpage.encode('utf-8'))
  166. if doc.tag == 'rss':
  167. return GenericIE()._extract_rss(url, video_id, doc)
  168. title = self._html_search_regex(
  169. [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
  170. r'<meta name="dcterms.title" content="(.*?)"/>',
  171. r'<h4 class="headline">(.*?)</h4>'],
  172. webpage, 'title')
  173. description = self._html_search_meta(
  174. 'dcterms.abstract', webpage, 'description', default=None)
  175. if description is None:
  176. description = self._html_search_meta(
  177. 'description', webpage, 'meta description')
  178. # Thumbnail is sometimes not present.
  179. # It is in the mobile version, but that seems to use a different URL
  180. # structure altogether.
  181. thumbnail = self._og_search_thumbnail(webpage, default=None)
  182. media_streams = re.findall(r'''(?x)
  183. mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
  184. "([^"]+)"''', webpage)
  185. if media_streams:
  186. QUALITIES = qualities(['lo', 'hi', 'hq'])
  187. formats = []
  188. for furl in set(media_streams):
  189. if furl.endswith('.f4m'):
  190. fid = 'f4m'
  191. else:
  192. fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
  193. fid = fid_m.group(1) if fid_m else None
  194. formats.append({
  195. 'quality': QUALITIES(fid),
  196. 'format_id': fid,
  197. 'url': furl,
  198. })
  199. self._sort_formats(formats)
  200. info = {
  201. 'formats': formats,
  202. }
  203. else: # request JSON file
  204. info = self._extract_media_info(
  205. 'http://www.ardmediathek.de/play/media/%s' % video_id, webpage, video_id)
  206. info.update({
  207. 'id': video_id,
  208. 'title': title,
  209. 'description': description,
  210. 'thumbnail': thumbnail,
  211. })
  212. return info
  213. class ARDIE(InfoExtractor):
  214. _VALID_URL = '(?P<mainurl>https?://(www\.)?daserste\.de/[^?#]+/videos/(?P<display_id>[^/?#]+)-(?P<id>[0-9]+))\.html'
  215. _TEST = {
  216. 'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
  217. 'md5': 'd216c3a86493f9322545e045ddc3eb35',
  218. 'info_dict': {
  219. 'display_id': 'die-story-im-ersten-mission-unter-falscher-flagge',
  220. 'id': '100',
  221. 'ext': 'mp4',
  222. 'duration': 2600,
  223. 'title': 'Die Story im Ersten: Mission unter falscher Flagge',
  224. 'upload_date': '20140804',
  225. 'thumbnail': 're:^https?://.*\.jpg$',
  226. },
  227. 'skip': 'HTTP Error 404: Not Found',
  228. }
  229. def _real_extract(self, url):
  230. mobj = re.match(self._VALID_URL, url)
  231. display_id = mobj.group('display_id')
  232. player_url = mobj.group('mainurl') + '~playerXml.xml'
  233. doc = self._download_xml(player_url, display_id)
  234. video_node = doc.find('./video')
  235. upload_date = unified_strdate(xpath_text(
  236. video_node, './broadcastDate'))
  237. thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
  238. formats = []
  239. for a in video_node.findall('.//asset'):
  240. f = {
  241. 'format_id': a.attrib['type'],
  242. 'width': int_or_none(a.find('./frameWidth').text),
  243. 'height': int_or_none(a.find('./frameHeight').text),
  244. 'vbr': int_or_none(a.find('./bitrateVideo').text),
  245. 'abr': int_or_none(a.find('./bitrateAudio').text),
  246. 'vcodec': a.find('./codecVideo').text,
  247. 'tbr': int_or_none(a.find('./totalBitrate').text),
  248. }
  249. if a.find('./serverPrefix').text:
  250. f['url'] = a.find('./serverPrefix').text
  251. f['playpath'] = a.find('./fileName').text
  252. else:
  253. f['url'] = a.find('./fileName').text
  254. formats.append(f)
  255. self._sort_formats(formats)
  256. return {
  257. 'id': mobj.group('id'),
  258. 'formats': formats,
  259. 'display_id': display_id,
  260. 'title': video_node.find('./title').text,
  261. 'duration': parse_duration(video_node.find('./duration').text),
  262. 'upload_date': upload_date,
  263. 'thumbnail': thumbnail,
  264. }