francetv.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_urllib_parse_urlparse,
  8. compat_urlparse,
  9. )
  10. from ..utils import (
  11. clean_html,
  12. ExtractorError,
  13. int_or_none,
  14. float_or_none,
  15. parse_duration,
  16. )
  17. class FranceTVBaseInfoExtractor(InfoExtractor):
  18. def _extract_video(self, video_id, catalogue):
  19. info = self._download_json(
  20. 'http://webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/?idDiffusion=%s&catalogue=%s'
  21. % (video_id, catalogue),
  22. video_id, 'Downloading video JSON')
  23. if info.get('status') == 'NOK':
  24. raise ExtractorError(
  25. '%s returned error: %s' % (self.IE_NAME, info['message']), expected=True)
  26. allowed_countries = info['videos'][0].get('geoblocage')
  27. if allowed_countries:
  28. georestricted = True
  29. geo_info = self._download_json(
  30. 'http://geo.francetv.fr/ws/edgescape.json', video_id,
  31. 'Downloading geo restriction info')
  32. country = geo_info['reponse']['geo_info']['country_code']
  33. if country not in allowed_countries:
  34. raise ExtractorError(
  35. 'The video is not available from your location',
  36. expected=True)
  37. else:
  38. georestricted = False
  39. formats = []
  40. for video in info['videos']:
  41. if video['statut'] != 'ONLINE':
  42. continue
  43. video_url = video['url']
  44. if not video_url:
  45. continue
  46. format_id = video['format']
  47. if video_url.endswith('.f4m'):
  48. if georestricted:
  49. # See https://github.com/rg3/youtube-dl/issues/3963
  50. # m3u8 urls work fine
  51. continue
  52. video_url_parsed = compat_urllib_parse_urlparse(video_url)
  53. f4m_url = self._download_webpage(
  54. 'http://hdfauth.francetv.fr/esi/urltokengen2.html?url=%s' % video_url_parsed.path,
  55. video_id, 'Downloading f4m manifest token', fatal=False)
  56. if f4m_url:
  57. f4m_formats = self._extract_f4m_formats(f4m_url, video_id)
  58. for f4m_format in f4m_formats:
  59. f4m_format['preference'] = 1
  60. formats.extend(f4m_formats)
  61. elif video_url.endswith('.m3u8'):
  62. formats.extend(self._extract_m3u8_formats(video_url, video_id, 'mp4'))
  63. elif video_url.startswith('rtmp'):
  64. formats.append({
  65. 'url': video_url,
  66. 'format_id': 'rtmp-%s' % format_id,
  67. 'ext': 'flv',
  68. 'preference': 1,
  69. })
  70. else:
  71. formats.append({
  72. 'url': video_url,
  73. 'format_id': format_id,
  74. 'preference': -1,
  75. })
  76. self._sort_formats(formats)
  77. return {
  78. 'id': video_id,
  79. 'title': info['titre'],
  80. 'description': clean_html(info['synopsis']),
  81. 'thumbnail': compat_urlparse.urljoin('http://pluzz.francetv.fr', info['image']),
  82. 'duration': float_or_none(info.get('real_duration'), 1000) or parse_duration(info['duree']),
  83. 'timestamp': int_or_none(info['diffusion']['timestamp']),
  84. 'formats': formats,
  85. }
  86. class PluzzIE(FranceTVBaseInfoExtractor):
  87. IE_NAME = 'pluzz.francetv.fr'
  88. _VALID_URL = r'https?://pluzz\.francetv\.fr/videos/(.*?)\.html'
  89. # Can't use tests, videos expire in 7 days
  90. def _real_extract(self, url):
  91. title = re.match(self._VALID_URL, url).group(1)
  92. webpage = self._download_webpage(url, title)
  93. video_id = self._search_regex(
  94. r'data-diffusion="(\d+)"', webpage, 'ID')
  95. return self._extract_video(video_id, 'Pluzz')
  96. class FranceTvInfoIE(FranceTVBaseInfoExtractor):
  97. IE_NAME = 'francetvinfo.fr'
  98. _VALID_URL = r'https?://(?:www|mobile)\.francetvinfo\.fr/.*/(?P<title>.+)\.html'
  99. _TESTS = [{
  100. 'url': 'http://www.francetvinfo.fr/replay-jt/france-3/soir-3/jt-grand-soir-3-lundi-26-aout-2013_393427.html',
  101. 'info_dict': {
  102. 'id': '84981923',
  103. 'ext': 'flv',
  104. 'title': 'Soir 3',
  105. 'upload_date': '20130826',
  106. 'timestamp': 1377548400,
  107. },
  108. }, {
  109. 'url': 'http://www.francetvinfo.fr/elections/europeennes/direct-europeennes-regardez-le-debat-entre-les-candidats-a-la-presidence-de-la-commission_600639.html',
  110. 'info_dict': {
  111. 'id': 'EV_20019',
  112. 'ext': 'mp4',
  113. 'title': 'Débat des candidats à la Commission européenne',
  114. 'description': 'Débat des candidats à la Commission européenne',
  115. },
  116. 'params': {
  117. 'skip_download': 'HLS (reqires ffmpeg)'
  118. },
  119. 'skip': 'Ce direct est terminé et sera disponible en rattrapage dans quelques minutes.',
  120. }]
  121. def _real_extract(self, url):
  122. mobj = re.match(self._VALID_URL, url)
  123. page_title = mobj.group('title')
  124. webpage = self._download_webpage(url, page_title)
  125. video_id, catalogue = self._search_regex(
  126. r'id-video=([^@]+@[^"]+)', webpage, 'video id').split('@')
  127. return self._extract_video(video_id, catalogue)
  128. class FranceTVIE(FranceTVBaseInfoExtractor):
  129. IE_NAME = 'francetv'
  130. IE_DESC = 'France 2, 3, 4, 5 and Ô'
  131. _VALID_URL = r'''(?x)https?://www\.france[2345o]\.fr/
  132. (?:
  133. emissions/.*?/(videos|emissions)/(?P<id>[^/?]+)
  134. | (emissions?|jt)/(?P<key>[^/?]+)
  135. )'''
  136. _TESTS = [
  137. # france2
  138. {
  139. 'url': 'http://www.france2.fr/emissions/13h15-le-samedi-le-dimanche/videos/75540104',
  140. 'md5': 'c03fc87cb85429ffd55df32b9fc05523',
  141. 'info_dict': {
  142. 'id': '109169362',
  143. 'ext': 'flv',
  144. 'title': '13h15, le dimanche...',
  145. 'description': 'md5:9a0932bb465f22d377a449be9d1a0ff7',
  146. 'upload_date': '20140914',
  147. 'timestamp': 1410693600,
  148. },
  149. },
  150. # france3
  151. {
  152. 'url': 'http://www.france3.fr/emissions/pieces-a-conviction/diffusions/13-11-2013_145575',
  153. 'md5': '679bb8f8921f8623bd658fa2f8364da0',
  154. 'info_dict': {
  155. 'id': '000702326_CAPP_PicesconvictionExtrait313022013_120220131722_Au',
  156. 'ext': 'mp4',
  157. 'title': 'Le scandale du prix des médicaments',
  158. 'description': 'md5:1384089fbee2f04fc6c9de025ee2e9ce',
  159. 'upload_date': '20131113',
  160. 'timestamp': 1384380000,
  161. },
  162. },
  163. # france4
  164. {
  165. 'url': 'http://www.france4.fr/emissions/hero-corp/videos/rhozet_herocorp_bonus_1_20131106_1923_06112013172108_F4',
  166. 'md5': 'a182bf8d2c43d88d46ec48fbdd260c1c',
  167. 'info_dict': {
  168. 'id': 'rhozet_herocorp_bonus_1_20131106_1923_06112013172108_F4',
  169. 'ext': 'mp4',
  170. 'title': 'Hero Corp Making of - Extrait 1',
  171. 'description': 'md5:c87d54871b1790679aec1197e73d650a',
  172. 'upload_date': '20131106',
  173. 'timestamp': 1383766500,
  174. },
  175. },
  176. # france5
  177. {
  178. 'url': 'http://www.france5.fr/emissions/c-a-dire/videos/92837968',
  179. 'md5': '78f0f4064f9074438e660785bbf2c5d9',
  180. 'info_dict': {
  181. 'id': '108961659',
  182. 'ext': 'flv',
  183. 'title': 'C à dire ?!',
  184. 'description': 'md5:1a4aeab476eb657bf57c4ff122129f81',
  185. 'upload_date': '20140915',
  186. 'timestamp': 1410795000,
  187. },
  188. },
  189. # franceo
  190. {
  191. 'url': 'http://www.franceo.fr/jt/info-afrique/04-12-2013',
  192. 'md5': '52f0bfe202848b15915a2f39aaa8981b',
  193. 'info_dict': {
  194. 'id': '108634970',
  195. 'ext': 'flv',
  196. 'title': 'Infô Afrique',
  197. 'description': 'md5:ebf346da789428841bee0fd2a935ea55',
  198. 'upload_date': '20140915',
  199. 'timestamp': 1410822000,
  200. },
  201. },
  202. ]
  203. def _real_extract(self, url):
  204. mobj = re.match(self._VALID_URL, url)
  205. webpage = self._download_webpage(url, mobj.group('key') or mobj.group('id'))
  206. video_id, catalogue = self._html_search_regex(
  207. r'href="http://videos\.francetv\.fr/video/([^@]+@[^"]+)"',
  208. webpage, 'video ID').split('@')
  209. return self._extract_video(video_id, catalogue)
  210. class GenerationQuoiIE(InfoExtractor):
  211. IE_NAME = 'france2.fr:generation-quoi'
  212. _VALID_URL = r'https?://generation-quoi\.france2\.fr/portrait/(?P<id>[^/?#]+)'
  213. _TEST = {
  214. 'url': 'http://generation-quoi.france2.fr/portrait/garde-a-vous',
  215. 'info_dict': {
  216. 'id': 'k7FJX8VBcvvLmX4wA5Q',
  217. 'ext': 'mp4',
  218. 'title': 'Génération Quoi - Garde à Vous',
  219. 'uploader': 'Génération Quoi',
  220. },
  221. 'params': {
  222. # It uses Dailymotion
  223. 'skip_download': True,
  224. },
  225. }
  226. def _real_extract(self, url):
  227. display_id = self._match_id(url)
  228. info_url = compat_urlparse.urljoin(url, '/medias/video/%s.json' % display_id)
  229. info_json = self._download_webpage(info_url, display_id)
  230. info = json.loads(info_json)
  231. return self.url_result('http://www.dailymotion.com/video/%s' % info['id'],
  232. ie='Dailymotion')
  233. class CultureboxIE(FranceTVBaseInfoExtractor):
  234. IE_NAME = 'culturebox.francetvinfo.fr'
  235. _VALID_URL = r'https?://(?:m\.)?culturebox\.francetvinfo\.fr/(?P<name>.*?)(\?|$)'
  236. _TEST = {
  237. 'url': 'http://culturebox.francetvinfo.fr/live/musique/musique-classique/le-livre-vermeil-de-montserrat-a-la-cathedrale-delne-214511',
  238. 'info_dict': {
  239. 'id': 'EV_50111',
  240. 'ext': 'mp4',
  241. 'title': "Le Livre Vermeil de Montserrat à la Cathédrale d'Elne",
  242. 'description': 'md5:f8a4ad202e8fe533e2c493cc12e739d9',
  243. 'upload_date': '20150320',
  244. 'timestamp': 1426892400,
  245. 'duration': 2760.9,
  246. },
  247. 'params': {
  248. 'skip_download': True,
  249. },
  250. }
  251. def _real_extract(self, url):
  252. mobj = re.match(self._VALID_URL, url)
  253. name = mobj.group('name')
  254. webpage = self._download_webpage(url, name)
  255. if ">Ce live n'est plus disponible en replay<" in webpage:
  256. raise ExtractorError('Video %s is not available' % name, expected=True)
  257. video_id, catalogue = self._search_regex(
  258. r'"http://videos\.francetv\.fr/video/([^@]+@[^"]+)"', webpage, 'video id').split('@')
  259. return self._extract_video(video_id, catalogue)