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.

318 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. f4m_formats = 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. if f4m_formats:
  107. formats.extend(f4m_formats)
  108. elif ext == 'm3u8':
  109. m3u8_formats = self._extract_m3u8_formats(
  110. stream_url, video_id, 'mp4', preference=1, m3u8_id='hls', fatal=False)
  111. if m3u8_formats:
  112. formats.extend(m3u8_formats)
  113. else:
  114. if server and server.startswith('rtmp'):
  115. f = {
  116. 'url': server,
  117. 'play_path': stream_url,
  118. 'format_id': 'a%s-rtmp-%s' % (num, quality),
  119. }
  120. elif stream_url.startswith('http'):
  121. f = {
  122. 'url': stream_url,
  123. 'format_id': 'a%s-%s-%s' % (num, ext, quality)
  124. }
  125. else:
  126. continue
  127. m = re.search(r'_(?P<width>\d+)x(?P<height>\d+)\.mp4$', stream_url)
  128. if m:
  129. f.update({
  130. 'width': int(m.group('width')),
  131. 'height': int(m.group('height')),
  132. })
  133. if type_ == 'audio':
  134. f['vcodec'] = 'none'
  135. formats.append(f)
  136. return formats
  137. def _real_extract(self, url):
  138. # determine video id from url
  139. m = re.match(self._VALID_URL, url)
  140. numid = re.search(r'documentId=([0-9]+)', url)
  141. if numid:
  142. video_id = numid.group(1)
  143. else:
  144. video_id = m.group('video_id')
  145. webpage = self._download_webpage(url, video_id)
  146. if '>Der gewünschte Beitrag ist nicht mehr verfügbar.<' in webpage:
  147. raise ExtractorError('Video %s is no longer available' % video_id, expected=True)
  148. 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:
  149. 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)
  150. if re.search(r'[\?&]rss($|[=&])', url):
  151. doc = compat_etree_fromstring(webpage.encode('utf-8'))
  152. if doc.tag == 'rss':
  153. return GenericIE()._extract_rss(url, video_id, doc)
  154. title = self._html_search_regex(
  155. [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
  156. r'<meta name="dcterms.title" content="(.*?)"/>',
  157. r'<h4 class="headline">(.*?)</h4>'],
  158. webpage, 'title')
  159. description = self._html_search_meta(
  160. 'dcterms.abstract', webpage, 'description', default=None)
  161. if description is None:
  162. description = self._html_search_meta(
  163. 'description', webpage, 'meta description')
  164. # Thumbnail is sometimes not present.
  165. # It is in the mobile version, but that seems to use a different URL
  166. # structure altogether.
  167. thumbnail = self._og_search_thumbnail(webpage, default=None)
  168. media_streams = re.findall(r'''(?x)
  169. mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
  170. "([^"]+)"''', webpage)
  171. if media_streams:
  172. QUALITIES = qualities(['lo', 'hi', 'hq'])
  173. formats = []
  174. for furl in set(media_streams):
  175. if furl.endswith('.f4m'):
  176. fid = 'f4m'
  177. else:
  178. fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
  179. fid = fid_m.group(1) if fid_m else None
  180. formats.append({
  181. 'quality': QUALITIES(fid),
  182. 'format_id': fid,
  183. 'url': furl,
  184. })
  185. self._sort_formats(formats)
  186. info = {
  187. 'formats': formats,
  188. }
  189. else: # request JSON file
  190. info = self._extract_media_info(
  191. 'http://www.ardmediathek.de/play/media/%s' % video_id, webpage, video_id)
  192. info.update({
  193. 'id': video_id,
  194. 'title': title,
  195. 'description': description,
  196. 'thumbnail': thumbnail,
  197. })
  198. return info
  199. class ARDIE(InfoExtractor):
  200. _VALID_URL = '(?P<mainurl>https?://(www\.)?daserste\.de/[^?#]+/videos/(?P<display_id>[^/?#]+)-(?P<id>[0-9]+))\.html'
  201. _TEST = {
  202. 'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
  203. 'md5': 'd216c3a86493f9322545e045ddc3eb35',
  204. 'info_dict': {
  205. 'display_id': 'die-story-im-ersten-mission-unter-falscher-flagge',
  206. 'id': '100',
  207. 'ext': 'mp4',
  208. 'duration': 2600,
  209. 'title': 'Die Story im Ersten: Mission unter falscher Flagge',
  210. 'upload_date': '20140804',
  211. 'thumbnail': 're:^https?://.*\.jpg$',
  212. }
  213. }
  214. def _real_extract(self, url):
  215. mobj = re.match(self._VALID_URL, url)
  216. display_id = mobj.group('display_id')
  217. player_url = mobj.group('mainurl') + '~playerXml.xml'
  218. doc = self._download_xml(player_url, display_id)
  219. video_node = doc.find('./video')
  220. upload_date = unified_strdate(xpath_text(
  221. video_node, './broadcastDate'))
  222. thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
  223. formats = []
  224. for a in video_node.findall('.//asset'):
  225. f = {
  226. 'format_id': a.attrib['type'],
  227. 'width': int_or_none(a.find('./frameWidth').text),
  228. 'height': int_or_none(a.find('./frameHeight').text),
  229. 'vbr': int_or_none(a.find('./bitrateVideo').text),
  230. 'abr': int_or_none(a.find('./bitrateAudio').text),
  231. 'vcodec': a.find('./codecVideo').text,
  232. 'tbr': int_or_none(a.find('./totalBitrate').text),
  233. }
  234. if a.find('./serverPrefix').text:
  235. f['url'] = a.find('./serverPrefix').text
  236. f['playpath'] = a.find('./fileName').text
  237. else:
  238. f['url'] = a.find('./fileName').text
  239. formats.append(f)
  240. self._sort_formats(formats)
  241. return {
  242. 'id': mobj.group('id'),
  243. 'formats': formats,
  244. 'display_id': display_id,
  245. 'title': video_node.find('./title').text,
  246. 'duration': parse_duration(video_node.find('./duration').text),
  247. 'upload_date': upload_date,
  248. 'thumbnail': thumbnail,
  249. }
  250. class SportschauIE(ARDMediathekIE):
  251. IE_NAME = 'Sportschau'
  252. _VALID_URL = r'(?P<baseurl>https?://(?:www\.)?sportschau\.de/(?:[^/]+/)+video(?P<id>[^/#?]+))\.html'
  253. _TESTS = [{
  254. 'url': 'http://www.sportschau.de/tourdefrance/videoseppeltkokainhatnichtsmitklassischemdopingzutun100.html',
  255. 'info_dict': {
  256. 'id': 'seppeltkokainhatnichtsmitklassischemdopingzutun100',
  257. 'ext': 'mp4',
  258. 'title': 'Seppelt: "Kokain hat nichts mit klassischem Doping zu tun"',
  259. 'thumbnail': 're:^https?://.*\.jpg$',
  260. '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.',
  261. },
  262. 'params': {
  263. # m3u8 download
  264. 'skip_download': True,
  265. },
  266. }]
  267. def _real_extract(self, url):
  268. mobj = re.match(self._VALID_URL, url)
  269. video_id = mobj.group('id')
  270. base_url = mobj.group('baseurl')
  271. webpage = self._download_webpage(url, video_id)
  272. title = get_element_by_attribute('class', 'headline', webpage)
  273. description = self._html_search_meta('description', webpage, 'description')
  274. info = self._extract_media_info(
  275. base_url + '-mc_defaultQuality-h.json', webpage, video_id)
  276. info.update({
  277. 'title': title,
  278. 'description': description,
  279. })
  280. return info