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.

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