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.

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