ard.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 ..compat import compat_str
  7. from ..utils import (
  8. determine_ext,
  9. ExtractorError,
  10. qualities,
  11. int_or_none,
  12. parse_duration,
  13. unified_strdate,
  14. xpath_text,
  15. update_url_query,
  16. )
  17. from ..compat import compat_etree_fromstring
  18. class ARDMediathekIE(InfoExtractor):
  19. IE_NAME = 'ARD:mediathek'
  20. _VALID_URL = r'^https?://(?:(?:www\.)?ardmediathek\.de|mediathek\.(?:daserste|rbb-online)\.de)/(?:.*/)(?P<video_id>[0-9]+|[^0-9][^/\?]+)[^/\?]*(?:\?.*)?'
  21. _TESTS = [{
  22. 'url': 'http://www.ardmediathek.de/tv/S%C3%9CDLICHT/Was-ist-die-Kunst-der-Zukunft-liebe-Ann/BR-Fernsehen/Video?bcastId=34633636&documentId=44726822',
  23. 'info_dict': {
  24. 'id': '44726822',
  25. 'ext': 'mp4',
  26. 'title': 'Was ist die Kunst der Zukunft, liebe Anna McCarthy?',
  27. 'description': 'md5:4ada28b3e3b5df01647310e41f3a62f5',
  28. 'duration': 1740,
  29. },
  30. 'params': {
  31. # m3u8 download
  32. 'skip_download': True,
  33. }
  34. },
  35. {
  36. 'url': 'http://www.ardmediathek.de/tv/Dokumentation-und-Reportage/Ich-liebe-das-Leben-trotzdem/rbb-Fernsehen/Video?documentId=29582122&bcastId=3822114',
  37. 'info_dict': {
  38. 'id': '29582122',
  39. 'ext': 'mp4',
  40. 'title': 'Ich liebe das Leben trotzdem',
  41. 'description': 'md5:45e4c225c72b27993314b31a84a5261c',
  42. 'duration': 4557,
  43. },
  44. 'params': {
  45. # m3u8 download
  46. 'skip_download': True,
  47. },
  48. 'skip': 'HTTP Error 404: Not Found',
  49. }, {
  50. 'url': 'http://www.ardmediathek.de/tv/Tatort/Tatort-Scheinwelten-H%C3%B6rfassung-Video/Das-Erste/Video?documentId=29522730&bcastId=602916',
  51. 'md5': 'f4d98b10759ac06c0072bbcd1f0b9e3e',
  52. 'info_dict': {
  53. 'id': '29522730',
  54. 'ext': 'mp4',
  55. 'title': 'Tatort: Scheinwelten - Hörfassung (Video tgl. ab 20 Uhr)',
  56. 'description': 'md5:196392e79876d0ac94c94e8cdb2875f1',
  57. 'duration': 5252,
  58. },
  59. 'skip': 'HTTP Error 404: Not Found',
  60. }, {
  61. # audio
  62. 'url': 'http://www.ardmediathek.de/tv/WDR-H%C3%B6rspiel-Speicher/Tod-eines-Fu%C3%9Fballers/WDR-3/Audio-Podcast?documentId=28488308&bcastId=23074086',
  63. 'md5': '219d94d8980b4f538c7fcb0865eb7f2c',
  64. 'info_dict': {
  65. 'id': '28488308',
  66. 'ext': 'mp3',
  67. 'title': 'Tod eines Fußballers',
  68. 'description': 'md5:f6e39f3461f0e1f54bfa48c8875c86ef',
  69. 'duration': 3240,
  70. },
  71. 'skip': 'HTTP Error 404: Not Found',
  72. }, {
  73. 'url': 'http://mediathek.daserste.de/sendungen_a-z/328454_anne-will/22429276_vertrauen-ist-gut-spionieren-ist-besser-geht',
  74. 'only_matching': True,
  75. }, {
  76. # audio
  77. 'url': 'http://mediathek.rbb-online.de/radio/Hörspiel/Vor-dem-Fest/kulturradio/Audio?documentId=30796318&topRessort=radio&bcastId=9839158',
  78. 'md5': '4e8f00631aac0395fee17368ac0e9867',
  79. 'info_dict': {
  80. 'id': '30796318',
  81. 'ext': 'mp3',
  82. 'title': 'Vor dem Fest',
  83. 'description': 'md5:c0c1c8048514deaed2a73b3a60eecacb',
  84. 'duration': 3287,
  85. },
  86. 'skip': 'Video is no longer available',
  87. }]
  88. def _extract_media_info(self, media_info_url, webpage, video_id):
  89. media_info = self._download_json(
  90. media_info_url, video_id, 'Downloading media JSON')
  91. formats = self._extract_formats(media_info, video_id)
  92. if not formats:
  93. if '"fsk"' in webpage:
  94. raise ExtractorError(
  95. 'This video is only available after 20:00', expected=True)
  96. elif media_info.get('_geoblocked'):
  97. raise ExtractorError('This video is not available due to geo restriction', expected=True)
  98. self._sort_formats(formats)
  99. duration = int_or_none(media_info.get('_duration'))
  100. thumbnail = media_info.get('_previewImage')
  101. is_live = media_info.get('_isLive') is True
  102. subtitles = {}
  103. subtitle_url = media_info.get('_subtitleUrl')
  104. if subtitle_url:
  105. subtitles['de'] = [{
  106. 'ext': 'ttml',
  107. 'url': subtitle_url,
  108. }]
  109. return {
  110. 'id': video_id,
  111. 'duration': duration,
  112. 'thumbnail': thumbnail,
  113. 'is_live': is_live,
  114. 'formats': formats,
  115. 'subtitles': subtitles,
  116. }
  117. def _extract_formats(self, media_info, video_id):
  118. type_ = media_info.get('_type')
  119. media_array = media_info.get('_mediaArray', [])
  120. formats = []
  121. for num, media in enumerate(media_array):
  122. for stream in media.get('_mediaStreamArray', []):
  123. stream_urls = stream.get('_stream')
  124. if not stream_urls:
  125. continue
  126. if not isinstance(stream_urls, list):
  127. stream_urls = [stream_urls]
  128. quality = stream.get('_quality')
  129. server = stream.get('_server')
  130. for stream_url in stream_urls:
  131. if not isinstance(stream_url, compat_str) or '//' not in stream_url:
  132. continue
  133. ext = determine_ext(stream_url)
  134. if quality != 'auto' and ext in ('f4m', 'm3u8'):
  135. continue
  136. if ext == 'f4m':
  137. formats.extend(self._extract_f4m_formats(
  138. update_url_query(stream_url, {
  139. 'hdcore': '3.1.1',
  140. 'plugin': 'aasp-3.1.1.69.124'
  141. }),
  142. video_id, f4m_id='hds', fatal=False))
  143. elif ext == 'm3u8':
  144. formats.extend(self._extract_m3u8_formats(
  145. stream_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  146. else:
  147. if server and server.startswith('rtmp'):
  148. f = {
  149. 'url': server,
  150. 'play_path': stream_url,
  151. 'format_id': 'a%s-rtmp-%s' % (num, quality),
  152. }
  153. else:
  154. f = {
  155. 'url': stream_url,
  156. 'format_id': 'a%s-%s-%s' % (num, ext, quality)
  157. }
  158. m = re.search(r'_(?P<width>\d+)x(?P<height>\d+)\.mp4$', stream_url)
  159. if m:
  160. f.update({
  161. 'width': int(m.group('width')),
  162. 'height': int(m.group('height')),
  163. })
  164. if type_ == 'audio':
  165. f['vcodec'] = 'none'
  166. formats.append(f)
  167. return formats
  168. def _real_extract(self, url):
  169. # determine video id from url
  170. m = re.match(self._VALID_URL, url)
  171. document_id = None
  172. numid = re.search(r'documentId=([0-9]+)', url)
  173. if numid:
  174. document_id = video_id = numid.group(1)
  175. else:
  176. video_id = m.group('video_id')
  177. webpage = self._download_webpage(url, video_id)
  178. ERRORS = (
  179. ('>Leider liegt eine Störung vor.', 'Video %s is unavailable'),
  180. ('>Der gewünschte Beitrag ist nicht mehr verfügbar.<',
  181. 'Video %s is no longer available'),
  182. )
  183. for pattern, message in ERRORS:
  184. if pattern in webpage:
  185. raise ExtractorError(message % video_id, expected=True)
  186. if re.search(r'[\?&]rss($|[=&])', url):
  187. doc = compat_etree_fromstring(webpage.encode('utf-8'))
  188. if doc.tag == 'rss':
  189. return GenericIE()._extract_rss(url, video_id, doc)
  190. title = self._html_search_regex(
  191. [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
  192. r'<meta name="dcterms\.title" content="(.*?)"/>',
  193. r'<h4 class="headline">(.*?)</h4>'],
  194. webpage, 'title')
  195. description = self._html_search_meta(
  196. 'dcterms.abstract', webpage, 'description', default=None)
  197. if description is None:
  198. description = self._html_search_meta(
  199. 'description', webpage, 'meta description')
  200. # Thumbnail is sometimes not present.
  201. # It is in the mobile version, but that seems to use a different URL
  202. # structure altogether.
  203. thumbnail = self._og_search_thumbnail(webpage, default=None)
  204. media_streams = re.findall(r'''(?x)
  205. mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
  206. "([^"]+)"''', webpage)
  207. if media_streams:
  208. QUALITIES = qualities(['lo', 'hi', 'hq'])
  209. formats = []
  210. for furl in set(media_streams):
  211. if furl.endswith('.f4m'):
  212. fid = 'f4m'
  213. else:
  214. fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
  215. fid = fid_m.group(1) if fid_m else None
  216. formats.append({
  217. 'quality': QUALITIES(fid),
  218. 'format_id': fid,
  219. 'url': furl,
  220. })
  221. self._sort_formats(formats)
  222. info = {
  223. 'formats': formats,
  224. }
  225. else: # request JSON file
  226. if not document_id:
  227. video_id = self._search_regex(
  228. r'/play/(?:config|media)/(\d+)', webpage, 'media id')
  229. info = self._extract_media_info(
  230. 'http://www.ardmediathek.de/play/media/%s' % video_id,
  231. webpage, video_id)
  232. info.update({
  233. 'id': video_id,
  234. 'title': self._live_title(title) if info.get('is_live') else title,
  235. 'description': description,
  236. 'thumbnail': thumbnail,
  237. })
  238. return info
  239. class ARDIE(InfoExtractor):
  240. _VALID_URL = r'(?P<mainurl>https?://(www\.)?daserste\.de/[^?#]+/videos/(?P<display_id>[^/?#]+)-(?P<id>[0-9]+))\.html'
  241. _TESTS = [{
  242. 'url': 'http://www.daserste.de/information/talk/maischberger/videos/das-groko-drama-zerlegen-sich-die-volksparteien-video-102.html',
  243. 'md5': '8e4ec85f31be7c7fc08a26cdbc5a1f49',
  244. 'info_dict': {
  245. 'display_id': 'das-groko-drama-zerlegen-sich-die-volksparteien-video',
  246. 'id': '102',
  247. 'ext': 'mp4',
  248. 'duration': 4435.0,
  249. 'title': 'Das GroKo-Drama: Zerlegen sich die Volksparteien?',
  250. 'upload_date': '20180214',
  251. 'thumbnail': r're:^https?://.*\.jpg$',
  252. },
  253. },
  254. {
  255. 'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
  256. 'md5': 'd216c3a86493f9322545e045ddc3eb35',
  257. 'info_dict': {
  258. 'display_id': 'die-story-im-ersten-mission-unter-falscher-flagge',
  259. 'id': '100',
  260. 'ext': 'mp4',
  261. 'duration': 2600,
  262. 'title': 'Die Story im Ersten: Mission unter falscher Flagge',
  263. 'upload_date': '20140804',
  264. 'thumbnail': r're:^https?://.*\.jpg$',
  265. },
  266. 'skip': 'HTTP Error 404: Not Found',
  267. }]
  268. def _real_extract(self, url):
  269. mobj = re.match(self._VALID_URL, url)
  270. display_id = mobj.group('display_id')
  271. player_url = mobj.group('mainurl') + '~playerXml.xml'
  272. doc = self._download_xml(player_url, display_id)
  273. video_node = doc.find('./video')
  274. upload_date = unified_strdate(xpath_text(
  275. video_node, './broadcastDate'))
  276. thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
  277. formats = []
  278. for a in video_node.findall('.//asset'):
  279. f = {
  280. 'format_id': a.attrib['type'],
  281. 'width': int_or_none(a.find('./frameWidth').text),
  282. 'height': int_or_none(a.find('./frameHeight').text),
  283. 'vbr': int_or_none(a.find('./bitrateVideo').text),
  284. 'abr': int_or_none(a.find('./bitrateAudio').text),
  285. 'vcodec': a.find('./codecVideo').text,
  286. 'tbr': int_or_none(a.find('./totalBitrate').text),
  287. }
  288. if a.find('./serverPrefix').text:
  289. f['url'] = a.find('./serverPrefix').text
  290. f['playpath'] = a.find('./fileName').text
  291. else:
  292. f['url'] = a.find('./fileName').text
  293. formats.append(f)
  294. self._sort_formats(formats)
  295. return {
  296. 'id': mobj.group('id'),
  297. 'formats': formats,
  298. 'display_id': display_id,
  299. 'title': video_node.find('./title').text,
  300. 'duration': parse_duration(video_node.find('./duration').text),
  301. 'upload_date': upload_date,
  302. 'thumbnail': thumbnail,
  303. }