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.

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