ard.py 12 KB

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