3 from __future__ 
import unicode_literals
 
   8 from .common 
import InfoExtractor
 
   9 from ..compat 
import compat_urlparse
 
  17 from .dailymotion 
import ( 
  23 class FranceTVBaseInfoExtractor(InfoExtractor
): 
  24     def _extract_video(self
, video_id
, catalogue
=None): 
  25         info 
= self
._download
_json
( 
  26             'https://sivideo.webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/', 
  27             video_id
, 'Downloading video JSON', query
={ 
  28                 'idDiffusion': video_id
, 
  29                 'catalogue': catalogue 
or '', 
  32         if info
.get('status') == 'NOK': 
  34                 '%s returned error: %s' % (self
.IE_NAME
, info
['message']), expected
=True) 
  35         allowed_countries 
= info
['videos'][0].get('geoblocage') 
  38             geo_info 
= self
._download
_json
( 
  39                 'http://geo.francetv.fr/ws/edgescape.json', video_id
, 
  40                 'Downloading geo restriction info') 
  41             country 
= geo_info
['reponse']['geo_info']['country_code'] 
  42             if country 
not in allowed_countries
: 
  44                     'The video is not available from your location', 
  50         for video 
in info
['videos']: 
  51             if video
['statut'] != 'ONLINE': 
  53             video_url 
= video
['url'] 
  56             format_id 
= video
['format'] 
  57             ext 
= determine_ext(video_url
) 
  60                     # See https://github.com/rg3/youtube-dl/issues/3963 
  63                 f4m_url 
= self
._download
_webpage
( 
  64                     'http://hdfauth.francetv.fr/esi/TA?url=%s' % video_url
, 
  65                     video_id
, 'Downloading f4m manifest token', fatal
=False) 
  67                     formats
.extend(self
._extract
_f
4m
_formats
( 
  68                         f4m_url 
+ '&hdcore=3.7.0&plugin=aasp-3.7.0.39.44', 
  69                         video_id
, f4m_id
=format_id
, fatal
=False)) 
  71                 formats
.extend(self
._extract
_m
3u8_formats
( 
  72                     video_url
, video_id
, 'mp4', entry_protocol
='m3u8_native', 
  73                     m3u8_id
=format_id
, fatal
=False)) 
  74             elif video_url
.startswith('rtmp'): 
  77                     'format_id': 'rtmp-%s' % format_id
, 
  81                 if self
._is
_valid
_url
(video_url
, video_id
, format_id
): 
  84                         'format_id': format_id
, 
  86         self
._sort
_formats
(formats
) 
  89         subtitle 
= info
.get('sous_titre') 
  91             title 
+= ' - %s' % subtitle
 
  96             'url': subformat
['url'], 
  97             'ext': subformat
.get('format'), 
  98         } for subformat 
in info
.get('subtitles', []) if subformat
.get('url')] 
 100             subtitles
['fr'] = subtitles_list
 
 105             'description': clean_html(info
['synopsis']), 
 106             'thumbnail': compat_urlparse
.urljoin('http://pluzz.francetv.fr', info
['image']), 
 107             'duration': int_or_none(info
.get('real_duration')) or parse_duration(info
['duree']), 
 108             'timestamp': int_or_none(info
['diffusion']['timestamp']), 
 110             'subtitles': subtitles
, 
 114 class FranceTVIE(FranceTVBaseInfoExtractor
): 
 115     _VALID_URL 
= r
'https?://(?:(?:www\.)?france\.tv|mobile\.france\.tv)/(?:[^/]+/)*(?P<id>[^/]+)\.html' 
 118         'url': 'https://www.france.tv/france-2/13h15-le-dimanche/140921-les-mysteres-de-jesus.html', 
 122             'title': '13h15, le dimanche... - Les mystères de Jésus', 
 123             'description': 'md5:75efe8d4c0a8205e5904498ffe1e1a42', 
 124             'timestamp': 1494156300, 
 125             'upload_date': '20170507', 
 129             'skip_download': True, 
 133         'url': 'https://www.france.tv/france-3/des-chiffres-et-des-lettres/139063-emission-du-mardi-9-mai-2017.html', 
 134         'only_matching': True, 
 137         'url': 'https://www.france.tv/france-4/hero-corp/saison-1/134151-apres-le-calme.html', 
 138         'only_matching': True, 
 141         'url': 'https://www.france.tv/france-5/c-a-dire/saison-10/137013-c-a-dire.html', 
 142         'only_matching': True, 
 145         'url': 'https://www.france.tv/france-o/archipels/132249-mon-ancetre-l-esclave.html', 
 146         'only_matching': True, 
 149         'url': 'https://www.france.tv/france-2/direct.html', 
 150         'only_matching': True, 
 152         'url': 'https://www.france.tv/documentaires/histoire/136517-argentine-les-500-bebes-voles-de-la-dictature.html', 
 153         'only_matching': True, 
 155         'url': 'https://www.france.tv/jeux-et-divertissements/divertissements/133965-le-web-contre-attaque.html', 
 156         'only_matching': True, 
 158         'url': 'https://mobile.france.tv/france-5/c-dans-l-air/137347-emission-du-vendredi-12-mai-2017.html', 
 159         'only_matching': True, 
 161         'url': 'https://www.france.tv/142749-rouge-sang.html', 
 162         'only_matching': True, 
 165     def _real_extract(self
, url
): 
 166         display_id 
= self
._match
_id
(url
) 
 168         webpage 
= self
._download
_webpage
(url
, display_id
) 
 171         video_id 
= self
._search
_regex
( 
 172             r
'data-main-video=(["\'])(?P
<id>(?
:(?
!\
1).)+)\
1', 
 173             webpage, 'video 
id', default=None, group='id') 
 176             video_id, catalogue = self._html_search_regex( 
 177                 r'(?
:href
=|player\
.setVideo\
(\s
*)"http://videos?\.francetv\.fr/video/([^@]+@[^"]+)"', 
 178                 webpage, 'video ID').split('@') 
 179         return self._extract_video(video_id, catalogue) 
 182 class FranceTVEmbedIE(FranceTVBaseInfoExtractor): 
 183     _VALID_URL = r'https?://embed\.francetv\.fr/*\?.*?\bue=(?P<id>[^&]+)' 
 186         'url': 'http://embed.francetv.fr/?ue=7fd581a2ccf59d2fc5719c5c13cf6961', 
 190             'title': 'Le Pen Reims', 
 191             'upload_date': '20170505', 
 192             'timestamp': 1493981780, 
 197     def _real_extract(self, url): 
 198         video_id = self._match_id(url) 
 200         video = self._download_json( 
 201             'http://api-embed.webservices.francetelevisions.fr/key/%s' % video_id, 
 204         return self._extract_video(video['video_id'], video.get('catalog')) 
 207 class FranceTVInfoIE(FranceTVBaseInfoExtractor): 
 208     IE_NAME = 'francetvinfo.fr' 
 209     _VALID_URL = r'https?://(?:www|mobile|france3-regions)\.francetvinfo\.fr/(?:[^/]+/)*(?P<title>[^/?#&.]+)' 
 212         'url': 'http://www.francetvinfo.fr/replay-jt/france-3/soir-3/jt-grand-soir-3-lundi-26-aout-2013_393427.html', 
 217             'upload_date': '20130826', 
 218             'timestamp': 1377548400, 
 225             'skip_download': True, 
 228         'url': 'http://www.francetvinfo.fr/elections/europeennes/direct-europeennes-regardez-le-debat-entre-les-candidats-a-la-presidence-de-la-commission_600639.html', 
 232             'title': 'Débat des candidats à la Commission européenne', 
 233             'description': 'Débat des candidats à la Commission européenne', 
 236             'skip_download': 'HLS (reqires ffmpeg)' 
 238         'skip': 'Ce direct est terminé et sera disponible en rattrapage dans quelques minutes.', 
 240         'url': 'http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html', 
 241         'md5': 'f485bda6e185e7d15dbc69b72bae993e', 
 245             'title': 'Les entreprises familiales : le secret de la réussite', 
 246             'thumbnail': r're:^https?://.*\.jpe?g$', 
 247             'timestamp': 1433273139, 
 248             'upload_date': '20150602', 
 252             'skip_download': True, 
 255         'url': 'http://france3-regions.francetvinfo.fr/bretagne/cotes-d-armor/thalassa-echappee-breizh-ce-venredi-dans-les-cotes-d-armor-954961.html', 
 256         'md5': 'f485bda6e185e7d15dbc69b72bae993e', 
 260             'title': 'Olivier Monthus, réalisateur de "Bretagne
, le choix de l’Armor
"', 
 261             'description': 'md5:a3264114c9d29aeca11ced113c37b16c', 
 262             'thumbnail': r're:^https?://.*\.jpe?g$', 
 263             'timestamp': 1458300695, 
 264             'upload_date': '20160318', 
 267             'skip_download': True, 
 271         'url': 'http://www.francetvinfo.fr/politique/notre-dame-des-landes/video-sur-france-inter-cecile-duflot-denonce-le-regard-meprisant-de-patrick-cohen_1520091.html', 
 272         'md5': 'ee7f1828f25a648addc90cb2687b1f12', 
 276             'title': 'NDDL, référendum, Brexit : Cécile Duflot répond à Patrick Cohen', 
 277             'description': 'Au lendemain de la victoire du "oui
" au référendum sur l\'aéroport de Notre-Dame-des-Landes, l\'ancienne ministre écologiste est l\'invitée de Patrick Cohen. Plus d\'info : https://www.franceinter.fr/emissions/le-7-9/le-7-9-27-juin-2016', 
 278             'timestamp': 1467011958, 
 279             'upload_date': '20160627', 
 280             'uploader': 'France Inter', 
 281             'uploader_id': 'x2q2ez', 
 283         'add_ie': ['Dailymotion'], 
 285         'url': 'http://france3-regions.francetvinfo.fr/limousin/emissions/jt-1213-limousin', 
 286         'only_matching': True, 
 289     def _real_extract(self, url): 
 290         mobj = re.match(self._VALID_URL, url) 
 291         page_title = mobj.group('title') 
 292         webpage = self._download_webpage(url, page_title) 
 294         dmcloud_url = DailymotionCloudIE._extract_dmcloud_url(webpage) 
 296             return self.url_result(dmcloud_url, DailymotionCloudIE.ie_key()) 
 298         dailymotion_urls = DailymotionIE._extract_urls(webpage) 
 300             return self.playlist_result([ 
 301                 self.url_result(dailymotion_url, DailymotionIE.ie_key()) 
 302                 for dailymotion_url in dailymotion_urls]) 
 304         video_id, catalogue = self._search_regex( 
 305             (r'id-video=([^@]+@[^"]+)', 
 306              r'<a
[^
>]+href
="(?:https?:)?//videos\.francetv\.fr/video/([^@]+@[^"]+)"'), 
 307             webpage, 'video id').split('@') 
 308         return self._extract_video(video_id, catalogue) 
 311 class GenerationQuoiIE(InfoExtractor): 
 312     IE_NAME = 'france2.fr:generation-quoi' 
 313     _VALID_URL = r'https?://generation-quoi\.france2\.fr/portrait/(?P<id>[^/?#]+)' 
 316         'url': 'http://generation-quoi.france2.fr/portrait/garde-a-vous', 
 318             'id': 'k7FJX8VBcvvLmX4wA5Q', 
 320             'title': 'Génération Quoi - Garde à Vous', 
 321             'uploader': 'Génération Quoi', 
 324             # It uses Dailymotion 
 325             'skip_download': True, 
 329     def _real_extract(self, url): 
 330         display_id = self._match_id(url) 
 331         info_url = compat_urlparse.urljoin(url, '/medias/video/%s.json' % display_id) 
 332         info_json = self._download_webpage(info_url, display_id) 
 333         info = json.loads(info_json) 
 334         return self.url_result('http://www.dailymotion.com/video/%s' % info['id'], 
 338 class CultureboxIE(FranceTVBaseInfoExtractor): 
 339     IE_NAME = 'culturebox.francetvinfo.fr' 
 340     _VALID_URL = r'https?://(?:m\.)?culturebox\.francetvinfo\.fr/(?P<name>.*?)(\?|$)' 
 343         'url': 'http://culturebox.francetvinfo.fr/live/musique/musique-classique/le-livre-vermeil-de-montserrat-a-la-cathedrale-delne-214511', 
 344         'md5': '9b88dc156781c4dbebd4c3e066e0b1d6', 
 348             'title': "Le Livre Vermeil de Montserrat à la Cathédrale d
'Elne", 
 349             'description
': 'md5
:f8a4ad202e8fe533e2c493cc12e739d9
', 
 350             'upload_date
': '20150320', 
 351             'timestamp
': 1426892400, 
 356     def _real_extract(self, url): 
 357         mobj = re.match(self._VALID_URL, url) 
 358         name = mobj.group('name
') 
 360         webpage = self._download_webpage(url, name) 
 362         if ">Ce live n'est plus disponible en replay
<" in webpage: 
 363             raise ExtractorError('Video %s is not available' % name, expected=True) 
 365         video_id, catalogue = self._search_regex( 
 366             r'"http
://videos\
.francetv\
.fr
/video
/([^
@]+@[^
"]+)"', webpage, 'video 
id').split('@') 
 368         return self._extract_video(video_id, catalogue)