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.

370 lines
14 KiB

12 years ago
10 years ago
12 years ago
9 years ago
9 years ago
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_parse_qs,
  7. compat_urllib_parse_urlparse,
  8. )
  9. from ..utils import (
  10. find_xpath_attr,
  11. unified_strdate,
  12. get_element_by_attribute,
  13. int_or_none,
  14. NO_DEFAULT,
  15. qualities,
  16. )
  17. # There are different sources of video in arte.tv, the extraction process
  18. # is different for each one. The videos usually expire in 7 days, so we can't
  19. # add tests.
  20. class ArteTvIE(InfoExtractor):
  21. _VALID_URL = r'https?://videos\.arte\.tv/(?P<lang>fr|de|en|es)/.*-(?P<id>.*?)\.html'
  22. IE_NAME = 'arte.tv'
  23. def _real_extract(self, url):
  24. mobj = re.match(self._VALID_URL, url)
  25. lang = mobj.group('lang')
  26. video_id = mobj.group('id')
  27. ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  28. ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
  29. ref_xml_doc = self._download_xml(
  30. ref_xml_url, video_id, note='Downloading metadata')
  31. config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
  32. config_xml_url = config_node.attrib['ref']
  33. config = self._download_xml(
  34. config_xml_url, video_id, note='Downloading configuration')
  35. formats = [{
  36. 'format_id': q.attrib['quality'],
  37. # The playpath starts at 'mp4:', if we don't manually
  38. # split the url, rtmpdump will incorrectly parse them
  39. 'url': q.text.split('mp4:', 1)[0],
  40. 'play_path': 'mp4:' + q.text.split('mp4:', 1)[1],
  41. 'ext': 'flv',
  42. 'quality': 2 if q.attrib['quality'] == 'hd' else 1,
  43. } for q in config.findall('./urls/url')]
  44. self._sort_formats(formats)
  45. title = config.find('.//name').text
  46. thumbnail = config.find('.//firstThumbnailUrl').text
  47. return {
  48. 'id': video_id,
  49. 'title': title,
  50. 'thumbnail': thumbnail,
  51. 'formats': formats,
  52. }
  53. class ArteTVPlus7IE(InfoExtractor):
  54. IE_NAME = 'arte.tv:+7'
  55. _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de|en|es)/(?:(?:sendungen|emissions|embed)/)?(?P<id>[^/]+)/(?P<name>[^/?#&+])'
  56. @classmethod
  57. def _extract_url_info(cls, url):
  58. mobj = re.match(cls._VALID_URL, url)
  59. lang = mobj.group('lang')
  60. query = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
  61. if 'vid' in query:
  62. video_id = query['vid'][0]
  63. else:
  64. # This is not a real id, it can be for example AJT for the news
  65. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  66. video_id = mobj.group('id')
  67. return video_id, lang
  68. def _real_extract(self, url):
  69. video_id, lang = self._extract_url_info(url)
  70. webpage = self._download_webpage(url, video_id)
  71. return self._extract_from_webpage(webpage, video_id, lang)
  72. def _extract_from_webpage(self, webpage, video_id, lang):
  73. patterns_templates = (r'arte_vp_url=["\'](.*?%s.*?)["\']', r'data-url=["\']([^"]+%s[^"]+)["\']')
  74. ids = (video_id, '')
  75. # some pages contain multiple videos (like
  76. # http://www.arte.tv/guide/de/sendungen/XEN/xenius/?vid=055918-015_PLUS7-D),
  77. # so we first try to look for json URLs that contain the video id from
  78. # the 'vid' parameter.
  79. patterns = [t % re.escape(_id) for _id in ids for t in patterns_templates]
  80. json_url = self._html_search_regex(
  81. patterns, webpage, 'json vp url', default=None)
  82. if not json_url:
  83. def find_iframe_url(webpage, default=NO_DEFAULT):
  84. return self._html_search_regex(
  85. r'<iframe[^>]+src=(["\'])(?P<url>.+\bjson_url=.+?)\1',
  86. webpage, 'iframe url', group='url', default=default)
  87. iframe_url = find_iframe_url(webpage, None)
  88. if not iframe_url:
  89. embed_url = self._html_search_regex(
  90. r'arte_vp_url_oembed=\'([^\']+?)\'', webpage, 'embed url', default=None)
  91. if embed_url:
  92. player = self._download_json(
  93. embed_url, video_id, 'Downloading player page')
  94. iframe_url = find_iframe_url(player['html'])
  95. # en and es URLs produce react-based pages with different layout (e.g.
  96. # http://www.arte.tv/guide/en/053330-002-A/carnival-italy?zone=world)
  97. if not iframe_url:
  98. program = self._search_regex(
  99. r'program\s*:\s*({.+?["\']embed_html["\'].+?}),?\s*\n',
  100. webpage, 'program', default=None)
  101. if program:
  102. embed_html = self._parse_json(program, video_id)
  103. if embed_html:
  104. iframe_url = find_iframe_url(embed_html['embed_html'])
  105. if iframe_url:
  106. json_url = compat_parse_qs(
  107. compat_urllib_parse_urlparse(iframe_url).query)['json_url'][0]
  108. if json_url:
  109. title = self._search_regex(
  110. r'<h3[^>]+title=(["\'])(?P<title>.+?)\1',
  111. webpage, 'title', default=None, group='title')
  112. return self._extract_from_json_url(json_url, video_id, lang, title=title)
  113. # Different kind of embed URL (e.g.
  114. # http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium)
  115. embed_url = self._search_regex(
  116. r'<iframe[^>]+src=(["\'])(?P<url>.+?)\1',
  117. webpage, 'embed url', group='url')
  118. return self.url_result(embed_url)
  119. def _extract_from_json_url(self, json_url, video_id, lang, title=None):
  120. info = self._download_json(json_url, video_id)
  121. player_info = info['videoJsonPlayer']
  122. upload_date_str = player_info.get('shootingDate')
  123. if not upload_date_str:
  124. upload_date_str = (player_info.get('VRA') or player_info.get('VDA') or '').split(' ')[0]
  125. title = (player_info.get('VTI') or title or player_info['VID']).strip()
  126. subtitle = player_info.get('VSU', '').strip()
  127. if subtitle:
  128. title += ' - %s' % subtitle
  129. info_dict = {
  130. 'id': player_info['VID'],
  131. 'title': title,
  132. 'description': player_info.get('VDE'),
  133. 'upload_date': unified_strdate(upload_date_str),
  134. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  135. }
  136. qfunc = qualities(['HQ', 'MQ', 'EQ', 'SQ'])
  137. LANGS = {
  138. 'fr': 'F',
  139. 'de': 'A',
  140. 'en': 'E[ANG]',
  141. 'es': 'E[ESP]',
  142. }
  143. formats = []
  144. for format_id, format_dict in player_info['VSR'].items():
  145. f = dict(format_dict)
  146. versionCode = f.get('versionCode')
  147. langcode = LANGS.get(lang, lang)
  148. lang_rexs = [r'VO?%s-' % re.escape(langcode), r'VO?.-ST%s$' % re.escape(langcode)]
  149. lang_pref = None
  150. if versionCode:
  151. matched_lang_rexs = [r for r in lang_rexs if re.match(r, versionCode)]
  152. lang_pref = -10 if not matched_lang_rexs else 10 * len(matched_lang_rexs)
  153. source_pref = 0
  154. if versionCode is not None:
  155. # The original version with subtitles has lower relevance
  156. if re.match(r'VO-ST(F|A|E)', versionCode):
  157. source_pref -= 10
  158. # The version with sourds/mal subtitles has also lower relevance
  159. elif re.match(r'VO?(F|A|E)-STM\1', versionCode):
  160. source_pref -= 9
  161. format = {
  162. 'format_id': format_id,
  163. 'preference': -10 if f.get('videoFormat') == 'M3U8' else None,
  164. 'language_preference': lang_pref,
  165. 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')),
  166. 'width': int_or_none(f.get('width')),
  167. 'height': int_or_none(f.get('height')),
  168. 'tbr': int_or_none(f.get('bitrate')),
  169. 'quality': qfunc(f.get('quality')),
  170. 'source_preference': source_pref,
  171. }
  172. if f.get('mediaType') == 'rtmp':
  173. format['url'] = f['streamer']
  174. format['play_path'] = 'mp4:' + f['url']
  175. format['ext'] = 'flv'
  176. else:
  177. format['url'] = f['url']
  178. formats.append(format)
  179. self._check_formats(formats, video_id)
  180. self._sort_formats(formats)
  181. info_dict['formats'] = formats
  182. return info_dict
  183. # It also uses the arte_vp_url url from the webpage to extract the information
  184. class ArteTVCreativeIE(ArteTVPlus7IE):
  185. IE_NAME = 'arte.tv:creative'
  186. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  187. _TESTS = [{
  188. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  189. 'info_dict': {
  190. 'id': '72176',
  191. 'ext': 'mp4',
  192. 'title': 'Folge 2 - Corporate Design',
  193. 'upload_date': '20131004',
  194. },
  195. }, {
  196. 'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion',
  197. 'info_dict': {
  198. 'id': '160676',
  199. 'ext': 'mp4',
  200. 'title': 'Monty Python live (mostly)',
  201. 'description': 'Événement ! Quarante-cinq ans après leurs premiers succès, les légendaires Monty Python remontent sur scène.\n',
  202. 'upload_date': '20140805',
  203. }
  204. }, {
  205. 'url': 'http://creative.arte.tv/de/episode/agentur-amateur-4-der-erste-kunde',
  206. 'only_matching': True,
  207. }]
  208. class ArteTVInfoIE(ArteTVPlus7IE):
  209. IE_NAME = 'arte.tv:info'
  210. _VALID_URL = r'https?://info\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  211. _TEST = {
  212. 'url': 'http://info.arte.tv/fr/service-civique-un-cache-misere',
  213. 'info_dict': {
  214. 'id': '067528-000-A',
  215. 'ext': 'mp4',
  216. 'title': 'Service civique, un cache misère ?',
  217. 'upload_date': '20160403',
  218. },
  219. }
  220. class ArteTVFutureIE(ArteTVPlus7IE):
  221. IE_NAME = 'arte.tv:future'
  222. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  223. _TESTS = [{
  224. 'url': 'http://future.arte.tv/fr/info-sciences/les-ecrevisses-aussi-sont-anxieuses',
  225. 'info_dict': {
  226. 'id': '050940-028-A',
  227. 'ext': 'mp4',
  228. 'title': 'Les écrevisses aussi peuvent être anxieuses',
  229. 'upload_date': '20140902',
  230. },
  231. }, {
  232. 'url': 'http://future.arte.tv/fr/la-science-est-elle-responsable',
  233. 'only_matching': True,
  234. }]
  235. class ArteTVDDCIE(ArteTVPlus7IE):
  236. IE_NAME = 'arte.tv:ddc'
  237. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>[^/?#&]+)'
  238. def _real_extract(self, url):
  239. video_id, lang = self._extract_url_info(url)
  240. if lang == 'folge':
  241. lang = 'de'
  242. elif lang == 'emission':
  243. lang = 'fr'
  244. webpage = self._download_webpage(url, video_id)
  245. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  246. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  247. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  248. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  249. return self._extract_from_json_url(json_url, video_id, lang)
  250. class ArteTVConcertIE(ArteTVPlus7IE):
  251. IE_NAME = 'arte.tv:concert'
  252. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  253. _TEST = {
  254. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  255. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  256. 'info_dict': {
  257. 'id': '186',
  258. 'ext': 'mp4',
  259. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  260. 'upload_date': '20140128',
  261. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  262. },
  263. }
  264. class ArteTVCinemaIE(ArteTVPlus7IE):
  265. IE_NAME = 'arte.tv:cinema'
  266. _VALID_URL = r'https?://cinema\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>.+)'
  267. _TEST = {
  268. 'url': 'http://cinema.arte.tv/de/node/38291',
  269. 'md5': '6b275511a5107c60bacbeeda368c3aa1',
  270. 'info_dict': {
  271. 'id': '055876-000_PWA12025-D',
  272. 'ext': 'mp4',
  273. 'title': 'Tod auf dem Nil',
  274. 'upload_date': '20160122',
  275. 'description': 'md5:7f749bbb77d800ef2be11d54529b96bc',
  276. },
  277. }
  278. class ArteTVMagazineIE(ArteTVPlus7IE):
  279. IE_NAME = 'arte.tv:magazine'
  280. _VALID_URL = r'https?://(?:www\.)?arte\.tv/magazine/[^/]+/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  281. _TESTS = [{
  282. # Embedded via <iframe src="http://www.arte.tv/arte_vp/index.php?json_url=..."
  283. 'url': 'http://www.arte.tv/magazine/trepalium/fr/entretien-avec-le-realisateur-vincent-lannoo-trepalium',
  284. 'md5': '2a9369bcccf847d1c741e51416299f25',
  285. 'info_dict': {
  286. 'id': '065965-000-A',
  287. 'ext': 'mp4',
  288. 'title': 'Trepalium - Extrait Ep.01',
  289. 'upload_date': '20160121',
  290. },
  291. }, {
  292. # Embedded via <iframe src="http://www.arte.tv/guide/fr/embed/054813-004-A/medium"
  293. 'url': 'http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium',
  294. 'md5': 'fedc64fc7a946110fe311634e79782ca',
  295. 'info_dict': {
  296. 'id': '054813-004_PLUS7-F',
  297. 'ext': 'mp4',
  298. 'title': 'Trepalium (4/6)',
  299. 'description': 'md5:10057003c34d54e95350be4f9b05cb40',
  300. 'upload_date': '20160218',
  301. },
  302. }, {
  303. 'url': 'http://www.arte.tv/magazine/metropolis/de/frank-woeste-german-paris-metropolis',
  304. 'only_matching': True,
  305. }]
  306. class ArteTVEmbedIE(ArteTVPlus7IE):
  307. IE_NAME = 'arte.tv:embed'
  308. _VALID_URL = r'''(?x)
  309. http://www\.arte\.tv
  310. /(?:playerv2/embed|arte_vp/index)\.php\?json_url=
  311. (?P<json_url>
  312. http://arte\.tv/papi/tvguide/videos/stream/player/
  313. (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
  314. )
  315. '''
  316. def _real_extract(self, url):
  317. mobj = re.match(self._VALID_URL, url)
  318. video_id = mobj.group('id')
  319. lang = mobj.group('lang')
  320. json_url = mobj.group('json_url')
  321. return self._extract_from_json_url(json_url, video_id, lang)