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.

326 lines
12 KiB

12 years ago
10 years ago
12 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'http://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)/)?(?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. embed_html = self._parse_json(
  99. self._search_regex(
  100. r'program\s*:\s*({.+?["\']embed_html["\'].+?}),?\s*\n',
  101. webpage, 'program'),
  102. video_id)['embed_html']
  103. iframe_url = find_iframe_url(embed_html)
  104. json_url = compat_parse_qs(
  105. compat_urllib_parse_urlparse(iframe_url).query)['json_url'][0]
  106. return self._extract_from_json_url(json_url, video_id, lang)
  107. def _extract_from_json_url(self, json_url, video_id, lang):
  108. info = self._download_json(json_url, video_id)
  109. player_info = info['videoJsonPlayer']
  110. upload_date_str = player_info.get('shootingDate')
  111. if not upload_date_str:
  112. upload_date_str = (player_info.get('VRA') or player_info.get('VDA') or '').split(' ')[0]
  113. title = player_info['VTI'].strip()
  114. subtitle = player_info.get('VSU', '').strip()
  115. if subtitle:
  116. title += ' - %s' % subtitle
  117. info_dict = {
  118. 'id': player_info['VID'],
  119. 'title': title,
  120. 'description': player_info.get('VDE'),
  121. 'upload_date': unified_strdate(upload_date_str),
  122. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  123. }
  124. qfunc = qualities(['HQ', 'MQ', 'EQ', 'SQ'])
  125. LANGS = {
  126. 'fr': 'F',
  127. 'de': 'A',
  128. 'en': 'E[ANG]',
  129. 'es': 'E[ESP]',
  130. }
  131. formats = []
  132. for format_id, format_dict in player_info['VSR'].items():
  133. f = dict(format_dict)
  134. versionCode = f.get('versionCode')
  135. langcode = LANGS.get(lang, lang)
  136. lang_rexs = [r'VO?%s-' % re.escape(langcode), r'VO?.-ST%s$' % re.escape(langcode)]
  137. lang_pref = None
  138. if versionCode:
  139. matched_lang_rexs = [r for r in lang_rexs if re.match(r, versionCode)]
  140. lang_pref = -10 if not matched_lang_rexs else 10 * len(matched_lang_rexs)
  141. source_pref = 0
  142. if versionCode is not None:
  143. # The original version with subtitles has lower relevance
  144. if re.match(r'VO-ST(F|A|E)', versionCode):
  145. source_pref -= 10
  146. # The version with sourds/mal subtitles has also lower relevance
  147. elif re.match(r'VO?(F|A|E)-STM\1', versionCode):
  148. source_pref -= 9
  149. format = {
  150. 'format_id': format_id,
  151. 'preference': -10 if f.get('videoFormat') == 'M3U8' else None,
  152. 'language_preference': lang_pref,
  153. 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')),
  154. 'width': int_or_none(f.get('width')),
  155. 'height': int_or_none(f.get('height')),
  156. 'tbr': int_or_none(f.get('bitrate')),
  157. 'quality': qfunc(f.get('quality')),
  158. 'source_preference': source_pref,
  159. }
  160. if f.get('mediaType') == 'rtmp':
  161. format['url'] = f['streamer']
  162. format['play_path'] = 'mp4:' + f['url']
  163. format['ext'] = 'flv'
  164. else:
  165. format['url'] = f['url']
  166. formats.append(format)
  167. self._check_formats(formats, video_id)
  168. self._sort_formats(formats)
  169. info_dict['formats'] = formats
  170. return info_dict
  171. # It also uses the arte_vp_url url from the webpage to extract the information
  172. class ArteTVCreativeIE(ArteTVPlus7IE):
  173. IE_NAME = 'arte.tv:creative'
  174. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de|en|es)/(?:magazine?/)?(?P<id>[^/?#&]+)'
  175. _TESTS = [{
  176. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  177. 'info_dict': {
  178. 'id': '72176',
  179. 'ext': 'mp4',
  180. 'title': 'Folge 2 - Corporate Design',
  181. 'upload_date': '20131004',
  182. },
  183. }, {
  184. 'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion',
  185. 'info_dict': {
  186. 'id': '160676',
  187. 'ext': 'mp4',
  188. 'title': 'Monty Python live (mostly)',
  189. 'description': 'Événement ! Quarante-cinq ans après leurs premiers succès, les légendaires Monty Python remontent sur scène.\n',
  190. 'upload_date': '20140805',
  191. }
  192. }]
  193. class ArteTVFutureIE(ArteTVPlus7IE):
  194. IE_NAME = 'arte.tv:future'
  195. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  196. _TESTS = [{
  197. 'url': 'http://future.arte.tv/fr/info-sciences/les-ecrevisses-aussi-sont-anxieuses',
  198. 'info_dict': {
  199. 'id': '050940-028-A',
  200. 'ext': 'mp4',
  201. 'title': 'Les écrevisses aussi peuvent être anxieuses',
  202. },
  203. }, {
  204. 'url': 'http://future.arte.tv/fr/la-science-est-elle-responsable',
  205. 'only_matching': True,
  206. }]
  207. class ArteTVDDCIE(ArteTVPlus7IE):
  208. IE_NAME = 'arte.tv:ddc'
  209. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>[^/?#&]+)'
  210. def _real_extract(self, url):
  211. video_id, lang = self._extract_url_info(url)
  212. if lang == 'folge':
  213. lang = 'de'
  214. elif lang == 'emission':
  215. lang = 'fr'
  216. webpage = self._download_webpage(url, video_id)
  217. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  218. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  219. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  220. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  221. return self._extract_from_json_url(json_url, video_id, lang)
  222. class ArteTVConcertIE(ArteTVPlus7IE):
  223. IE_NAME = 'arte.tv:concert'
  224. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  225. _TEST = {
  226. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  227. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  228. 'info_dict': {
  229. 'id': '186',
  230. 'ext': 'mp4',
  231. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  232. 'upload_date': '20140128',
  233. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  234. },
  235. }
  236. class ArteTVCinemaIE(ArteTVPlus7IE):
  237. IE_NAME = 'arte.tv:cinema'
  238. _VALID_URL = r'https?://cinema\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>.+)'
  239. _TEST = {
  240. 'url': 'http://cinema.arte.tv/de/node/38291',
  241. 'md5': '6b275511a5107c60bacbeeda368c3aa1',
  242. 'info_dict': {
  243. 'id': '055876-000_PWA12025-D',
  244. 'ext': 'mp4',
  245. 'title': 'Tod auf dem Nil',
  246. 'upload_date': '20160122',
  247. 'description': 'md5:7f749bbb77d800ef2be11d54529b96bc',
  248. },
  249. }
  250. class ArteTVMagazineIE(ArteTVPlus7IE):
  251. IE_NAME = 'arte.tv:magazine'
  252. _VALID_URL = r'https?://(?:www\.)?arte\.tv/magazine/[^/]+/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  253. _TESTS = [{
  254. 'url': 'http://www.arte.tv/magazine/trepalium/fr/entretien-avec-le-realisateur-vincent-lannoo-trepalium',
  255. 'md5': '66a093339c1278bb3719157ef07107b2',
  256. 'info_dict': {
  257. 'id': '065965-000-A',
  258. 'ext': 'mp4',
  259. 'title': 'Trepalium - Extrait Ep.01',
  260. },
  261. }, {
  262. 'url': 'http://www.arte.tv/magazine/metropolis/de/frank-woeste-german-paris-metropolis',
  263. 'only_matching': True,
  264. }]
  265. class ArteTVEmbedIE(ArteTVPlus7IE):
  266. IE_NAME = 'arte.tv:embed'
  267. _VALID_URL = r'''(?x)
  268. http://www\.arte\.tv
  269. /playerv2/embed\.php\?json_url=
  270. (?P<json_url>
  271. http://arte\.tv/papi/tvguide/videos/stream/player/
  272. (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
  273. )
  274. '''
  275. def _real_extract(self, url):
  276. mobj = re.match(self._VALID_URL, url)
  277. video_id = mobj.group('id')
  278. lang = mobj.group('lang')
  279. json_url = mobj.group('json_url')
  280. return self._extract_from_json_url(json_url, video_id, lang)