3 from __future__ 
import unicode_literals
 
   7 from .common 
import InfoExtractor
 
   8 from ..compat 
import compat_urlparse
 
  16 from .dailymotion 
import DailymotionIE
 
  19 class FranceTVBaseInfoExtractor(InfoExtractor
): 
  20     def _extract_video(self
, video_id
, catalogue
=None): 
  21         info 
= self
._download
_json
( 
  22             'https://sivideo.webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/', 
  23             video_id
, 'Downloading video JSON', query
={ 
  24                 'idDiffusion': video_id
, 
  25                 'catalogue': catalogue 
or '', 
  28         if info
.get('status') == 'NOK': 
  30                 '%s returned error: %s' % (self
.IE_NAME
, info
['message']), expected
=True) 
  31         allowed_countries 
= info
['videos'][0].get('geoblocage') 
  34             geo_info 
= self
._download
_json
( 
  35                 'http://geo.francetv.fr/ws/edgescape.json', video_id
, 
  36                 'Downloading geo restriction info') 
  37             country 
= geo_info
['reponse']['geo_info']['country_code'] 
  38             if country 
not in allowed_countries
: 
  40                     'The video is not available from your location', 
  46         for video 
in info
['videos']: 
  47             if video
['statut'] != 'ONLINE': 
  49             video_url 
= video
['url'] 
  52             format_id 
= video
['format'] 
  53             ext 
= determine_ext(video_url
) 
  56                     # See https://github.com/rg3/youtube-dl/issues/3963 
  59                 f4m_url 
= self
._download
_webpage
( 
  60                     'http://hdfauth.francetv.fr/esi/TA?url=%s' % video_url
, 
  61                     video_id
, 'Downloading f4m manifest token', fatal
=False) 
  63                     formats
.extend(self
._extract
_f
4m
_formats
( 
  64                         f4m_url 
+ '&hdcore=3.7.0&plugin=aasp-3.7.0.39.44', 
  65                         video_id
, f4m_id
=format_id
, fatal
=False)) 
  67                 formats
.extend(self
._extract
_m
3u8_formats
( 
  68                     video_url
, video_id
, 'mp4', entry_protocol
='m3u8_native', 
  69                     m3u8_id
=format_id
, fatal
=False)) 
  70             elif video_url
.startswith('rtmp'): 
  73                     'format_id': 'rtmp-%s' % format_id
, 
  77                 if self
._is
_valid
_url
(video_url
, video_id
, format_id
): 
  80                         'format_id': format_id
, 
  82         self
._sort
_formats
(formats
) 
  85         subtitle 
= info
.get('sous_titre') 
  87             title 
+= ' - %s' % subtitle
 
  92             'url': subformat
['url'], 
  93             'ext': subformat
.get('format'), 
  94         } for subformat 
in info
.get('subtitles', []) if subformat
.get('url')] 
  96             subtitles
['fr'] = subtitles_list
 
 101             'description': clean_html(info
['synopsis']), 
 102             'thumbnail': compat_urlparse
.urljoin('http://pluzz.francetv.fr', info
['image']), 
 103             'duration': int_or_none(info
.get('real_duration')) or parse_duration(info
['duree']), 
 104             'timestamp': int_or_none(info
['diffusion']['timestamp']), 
 106             'subtitles': subtitles
, 
 110 class FranceTVIE(FranceTVBaseInfoExtractor
): 
 111     _VALID_URL 
= r
'https?://(?:(?:www\.)?france\.tv|mobile\.france\.tv)/(?:[^/]+/)*(?P<id>[^/]+)\.html' 
 114         'url': 'https://www.france.tv/france-2/13h15-le-dimanche/140921-les-mysteres-de-jesus.html', 
 118             'title': '13h15, le dimanche... - Les mystères de Jésus', 
 119             'description': 'md5:75efe8d4c0a8205e5904498ffe1e1a42', 
 120             'timestamp': 1494156300, 
 121             'upload_date': '20170507', 
 125             'skip_download': True, 
 129         'url': 'https://www.france.tv/france-3/des-chiffres-et-des-lettres/139063-emission-du-mardi-9-mai-2017.html', 
 130         'only_matching': True, 
 133         'url': 'https://www.france.tv/france-4/hero-corp/saison-1/134151-apres-le-calme.html', 
 134         'only_matching': True, 
 137         'url': 'https://www.france.tv/france-5/c-a-dire/saison-10/137013-c-a-dire.html', 
 138         'only_matching': True, 
 141         'url': 'https://www.france.tv/france-o/archipels/132249-mon-ancetre-l-esclave.html', 
 142         'only_matching': True, 
 145         'url': 'https://www.france.tv/france-2/direct.html', 
 146         'only_matching': True, 
 148         'url': 'https://www.france.tv/documentaires/histoire/136517-argentine-les-500-bebes-voles-de-la-dictature.html', 
 149         'only_matching': True, 
 151         'url': 'https://www.france.tv/jeux-et-divertissements/divertissements/133965-le-web-contre-attaque.html', 
 152         'only_matching': True, 
 154         'url': 'https://mobile.france.tv/france-5/c-dans-l-air/137347-emission-du-vendredi-12-mai-2017.html', 
 155         'only_matching': True, 
 157         'url': 'https://www.france.tv/142749-rouge-sang.html', 
 158         'only_matching': True, 
 161     def _real_extract(self
, url
): 
 162         display_id 
= self
._match
_id
(url
) 
 164         webpage 
= self
._download
_webpage
(url
, display_id
) 
 167         video_id 
= self
._search
_regex
( 
 168             r
'data-main-video=(["\'])(?P
<id>(?
:(?
!\
1).)+)\
1', 
 169             webpage, 'video 
id', default=None, group='id') 
 172             video_id, catalogue = self._html_search_regex( 
 173                 r'(?
:href
=|player\
.setVideo\
(\s
*)"http://videos?\.francetv\.fr/video/([^@]+@[^"]+)"', 
 174                 webpage, 'video ID').split('@') 
 175         return self._extract_video(video_id, catalogue) 
 178 class FranceTVEmbedIE(FranceTVBaseInfoExtractor): 
 179     _VALID_URL = r'https?://embed\.francetv\.fr/*\?.*?\bue=(?P<id>[^&]+)' 
 182         'url': 'http://embed.francetv.fr/?ue=7fd581a2ccf59d2fc5719c5c13cf6961', 
 186             'title': 'Le Pen Reims', 
 187             'upload_date': '20170505', 
 188             'timestamp': 1493981780, 
 193     def _real_extract(self, url): 
 194         video_id = self._match_id(url) 
 196         video = self._download_json( 
 197             'http://api-embed.webservices.francetelevisions.fr/key/%s' % video_id, 
 200         return self._extract_video(video['video_id'], video.get('catalog')) 
 203 class FranceTVInfoIE(FranceTVBaseInfoExtractor): 
 204     IE_NAME = 'francetvinfo.fr' 
 205     _VALID_URL = r'https?://(?:www|mobile|france3-regions)\.francetvinfo\.fr/(?:[^/]+/)*(?P<title>[^/?#&.]+)' 
 208         'url': 'http://www.francetvinfo.fr/replay-jt/france-3/soir-3/jt-grand-soir-3-lundi-26-aout-2013_393427.html', 
 213             'upload_date': '20130826', 
 214             'timestamp': 1377548400, 
 221             'skip_download': True, 
 224         'url': 'http://www.francetvinfo.fr/elections/europeennes/direct-europeennes-regardez-le-debat-entre-les-candidats-a-la-presidence-de-la-commission_600639.html', 
 228             'title': 'Débat des candidats à la Commission européenne', 
 229             'description': 'Débat des candidats à la Commission européenne', 
 232             'skip_download': 'HLS (reqires ffmpeg)' 
 234         'skip': 'Ce direct est terminé et sera disponible en rattrapage dans quelques minutes.', 
 236         'url': 'http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html', 
 237         'md5': 'f485bda6e185e7d15dbc69b72bae993e', 
 241             'title': 'Les entreprises familiales : le secret de la réussite', 
 242             'thumbnail': r're:^https?://.*\.jpe?g$', 
 243             'timestamp': 1433273139, 
 244             'upload_date': '20150602', 
 248             'skip_download': True, 
 251         'url': 'http://france3-regions.francetvinfo.fr/bretagne/cotes-d-armor/thalassa-echappee-breizh-ce-venredi-dans-les-cotes-d-armor-954961.html', 
 252         'md5': 'f485bda6e185e7d15dbc69b72bae993e', 
 256             'title': 'Olivier Monthus, réalisateur de "Bretagne
, le choix de l’Armor
"', 
 257             'description': 'md5:a3264114c9d29aeca11ced113c37b16c', 
 258             'thumbnail': r're:^https?://.*\.jpe?g$', 
 259             'timestamp': 1458300695, 
 260             'upload_date': '20160318', 
 263             'skip_download': True, 
 267         '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', 
 268         'md5': 'ee7f1828f25a648addc90cb2687b1f12', 
 272             'title': 'NDDL, référendum, Brexit : Cécile Duflot répond à Patrick Cohen', 
 273             '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', 
 274             'timestamp': 1467011958, 
 275             'upload_date': '20160627', 
 276             'uploader': 'France Inter', 
 277             'uploader_id': 'x2q2ez', 
 279         'add_ie': ['Dailymotion'], 
 281         'url': 'http://france3-regions.francetvinfo.fr/limousin/emissions/jt-1213-limousin', 
 282         'only_matching': True, 
 285     def _real_extract(self, url): 
 286         mobj = re.match(self._VALID_URL, url) 
 287         page_title = mobj.group('title') 
 288         webpage = self._download_webpage(url, page_title) 
 290         dailymotion_urls = DailymotionIE._extract_urls(webpage) 
 292             return self.playlist_result([ 
 293                 self.url_result(dailymotion_url, DailymotionIE.ie_key()) 
 294                 for dailymotion_url in dailymotion_urls]) 
 296         video_id, catalogue = self._search_regex( 
 297             (r'id-video=([^@]+@[^"]+)', 
 298              r'<a
[^
>]+href
="(?:https?:)?//videos\.francetv\.fr/video/([^@]+@[^"]+)"'), 
 299             webpage, 'video id').split('@') 
 300         return self._extract_video(video_id, catalogue) 
 303 class GenerationWhatIE(InfoExtractor): 
 304     IE_NAME = 'france2.fr:generation-what' 
 305     _VALID_URL = r'https?://generation-what\.francetv\.fr/[^/]+/video/(?P<id>[^/?#]+)' 
 308         'url': 'http://generation-what.francetv.fr/portrait/video/present-arms', 
 312             'title': 'Generation What - Garde à vous - FRA', 
 313             'uploader': 'Generation What', 
 314             'uploader_id': 'UCHH9p1eetWCgt4kXBYCb3_w', 
 315             'upload_date': '20160411', 
 318         'url': 'http://generation-what.francetv.fr/europe/video/present-arms', 
 319         'only_matching': True, 
 322     def _real_extract(self, url): 
 323         display_id = self._match_id(url) 
 324         webpage = self._download_webpage(url, display_id) 
 325         youtube_id = self._search_regex( 
 326             r"window\
.videoURL\s
*=\s
*'([0-9A-Za-z_-]{11})';", 
 327             webpage, 'youtube id') 
 328         return self.url_result(youtube_id, 'Youtube', youtube_id) 
 331 class CultureboxIE(FranceTVBaseInfoExtractor): 
 332     IE_NAME = 'culturebox.francetvinfo.fr' 
 333     _VALID_URL = r'https?://(?:m\.)?culturebox\.francetvinfo\.fr/(?P<name>.*?)(\?|$)' 
 336         'url': 'http://culturebox.francetvinfo.fr/live/musique/musique-classique/le-livre-vermeil-de-montserrat-a-la-cathedrale-delne-214511', 
 337         'md5': '9b88dc156781c4dbebd4c3e066e0b1d6', 
 341             'title': "Le Livre Vermeil de Montserrat à la Cathédrale d
'Elne", 
 342             'description
': 'md5
:f8a4ad202e8fe533e2c493cc12e739d9
', 
 343             'upload_date
': '20150320', 
 344             'timestamp
': 1426892400, 
 349     def _real_extract(self, url): 
 350         mobj = re.match(self._VALID_URL, url) 
 351         name = mobj.group('name
') 
 353         webpage = self._download_webpage(url, name) 
 355         if ">Ce live n'est plus disponible en replay
<" in webpage: 
 356             raise ExtractorError('Video %s is not available' % name, expected=True) 
 358         video_id, catalogue = self._search_regex( 
 359             r'["\'>]https?
://videos\
.francetv\
.fr
/video
/([^
@]+@.+?
)["\'<]', 
 360             webpage, 'video id').split('@') 
 362         return self._extract_video(video_id, catalogue)