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.

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