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.

295 lines
11 KiB

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