2 from __future__ 
import unicode_literals
 
   6 from .common 
import InfoExtractor
 
  10     compat_urllib_parse_urlparse
, 
  15     get_element_by_attribute
, 
  23 # There are different sources of video in arte.tv, the extraction process 
  24 # is different for each one. The videos usually expire in 7 days, so we can't 
  28 class ArteTvIE(InfoExtractor
): 
  29     _VALID_URL 
= r
'https?://videos\.arte\.tv/(?P<lang>fr|de|en|es)/.*-(?P<id>.*?)\.html' 
  32     def _real_extract(self
, url
): 
  33         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  34         lang 
= mobj
.group('lang') 
  35         video_id 
= mobj
.group('id') 
  37         ref_xml_url 
= url
.replace('/videos/', '/do_delegate/videos/') 
  38         ref_xml_url 
= ref_xml_url
.replace('.html', ',view,asPlayerXml.xml') 
  39         ref_xml_doc 
= self
._download
_xml
( 
  40             ref_xml_url
, video_id
, note
='Downloading metadata') 
  41         config_node 
= find_xpath_attr(ref_xml_doc
, './/video', 'lang', lang
) 
  42         config_xml_url 
= config_node
.attrib
['ref'] 
  43         config 
= self
._download
_xml
( 
  44             config_xml_url
, video_id
, note
='Downloading configuration') 
  47             'format_id': q
.attrib
['quality'], 
  48             # The playpath starts at 'mp4:', if we don't manually 
  49             # split the url, rtmpdump will incorrectly parse them 
  50             'url': q
.text
.split('mp4:', 1)[0], 
  51             'play_path': 'mp4:' + q
.text
.split('mp4:', 1)[1], 
  53             'quality': 2 if q
.attrib
['quality'] == 'hd' else 1, 
  54         } for q 
in config
.findall('./urls/url')] 
  55         self
._sort
_formats
(formats
) 
  57         title 
= config
.find('.//name').text
 
  58         thumbnail 
= config
.find('.//firstThumbnailUrl').text
 
  62             'thumbnail': thumbnail
, 
  67 class ArteTVBaseIE(InfoExtractor
): 
  69     def _extract_url_info(cls
, url
): 
  70         mobj 
= re
.match(cls
._VALID
_URL
, url
) 
  71         lang 
= mobj
.group('lang') 
  72         query 
= compat_parse_qs(compat_urllib_parse_urlparse(url
).query
) 
  74             video_id 
= query
['vid'][0] 
  76             # This is not a real id, it can be for example AJT for the news 
  77             # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal 
  78             video_id 
= mobj
.group('id') 
  81     def _extract_from_json_url(self
, json_url
, video_id
, lang
, title
=None): 
  82         info 
= self
._download
_json
(json_url
, video_id
) 
  83         player_info 
= info
['videoJsonPlayer'] 
  85         vsr 
= try_get(player_info
, lambda x
: x
['VSR'], dict) 
  88             if try_get(player_info
, lambda x
: x
['custom_msg']['type']) == 'error': 
  90                     player_info
, lambda x
: x
['custom_msg']['msg'], compat_str
) 
  92                 error 
= 'Video %s is not available' % player_info
.get('VID') or video_id
 
  93             raise ExtractorError(error
, expected
=True) 
  95         upload_date_str 
= player_info
.get('shootingDate') 
  96         if not upload_date_str
: 
  97             upload_date_str 
= (player_info
.get('VRA') or player_info
.get('VDA') or '').split(' ')[0] 
  99         title 
= (player_info
.get('VTI') or title 
or player_info
['VID']).strip() 
 100         subtitle 
= player_info
.get('VSU', '').strip() 
 102             title 
+= ' - %s' % subtitle
 
 105             'id': player_info
['VID'], 
 107             'description': player_info
.get('VDE'), 
 108             'upload_date': unified_strdate(upload_date_str
), 
 109             'thumbnail': player_info
.get('programImage') or player_info
.get('VTU', {}).get('IUR'), 
 111         qfunc 
= qualities(['HQ', 'MQ', 'EQ', 'SQ']) 
 120         langcode 
= LANGS
.get(lang
, lang
) 
 123         for format_id
, format_dict 
in vsr
.items(): 
 124             f 
= dict(format_dict
) 
 125             versionCode 
= f
.get('versionCode') 
 126             l 
= re
.escape(langcode
) 
 128             # Language preference from most to least priority 
 129             # Reference: section 5.6.3 of 
 130             # http://www.arte.tv/sites/en/corporate/files/complete-technical-guidelines-arte-geie-v1-05.pdf 
 132                 # original version in requested language, without subtitles 
 134                 # original version in requested language, with partial subtitles in requested language 
 135                 r
'VO{0}-ST{0}$'.format(l
), 
 136                 # original version in requested language, with subtitles for the deaf and hard-of-hearing in requested language 
 137                 r
'VO{0}-STM{0}$'.format(l
), 
 138                 # non-original (dubbed) version in requested language, without subtitles 
 140                 # non-original (dubbed) version in requested language, with subtitles partial subtitles in requested language 
 141                 r
'V{0}-ST{0}$'.format(l
), 
 142                 # non-original (dubbed) version in requested language, with subtitles for the deaf and hard-of-hearing in requested language 
 143                 r
'V{0}-STM{0}$'.format(l
), 
 144                 # original version in requested language, with partial subtitles in different language 
 145                 r
'VO{0}-ST(?!{0}).+?$'.format(l
), 
 146                 # original version in requested language, with subtitles for the deaf and hard-of-hearing in different language 
 147                 r
'VO{0}-STM(?!{0}).+?$'.format(l
), 
 148                 # original version in different language, with partial subtitles in requested language 
 149                 r
'VO(?:(?!{0}).+?)?-ST{0}$'.format(l
), 
 150                 # original version in different language, with subtitles for the deaf and hard-of-hearing in requested language 
 151                 r
'VO(?:(?!{0}).+?)?-STM{0}$'.format(l
), 
 152                 # original version in different language, without subtitles 
 153                 r
'VO(?:(?!{0}))?$'.format(l
), 
 154                 # original version in different language, with partial subtitles in different language 
 155                 r
'VO(?:(?!{0}).+?)?-ST(?!{0}).+?$'.format(l
), 
 156                 # original version in different language, with subtitles for the deaf and hard-of-hearing in different language 
 157                 r
'VO(?:(?!{0}).+?)?-STM(?!{0}).+?$'.format(l
), 
 160             for pref
, p 
in enumerate(PREFERENCES
): 
 161                 if re
.match(p
, versionCode
): 
 162                     lang_pref 
= len(PREFERENCES
) - pref
 
 168                 'format_id': format_id
, 
 169                 'preference': -10 if f
.get('videoFormat') == 'M3U8' else None, 
 170                 'language_preference': lang_pref
, 
 171                 'format_note': '%s, %s' % (f
.get('versionCode'), f
.get('versionLibelle')), 
 172                 'width': int_or_none(f
.get('width')), 
 173                 'height': int_or_none(f
.get('height')), 
 174                 'tbr': int_or_none(f
.get('bitrate')), 
 175                 'quality': qfunc(f
.get('quality')), 
 178             if f
.get('mediaType') == 'rtmp': 
 179                 format
['url'] = f
['streamer'] 
 180                 format
['play_path'] = 'mp4:' + f
['url'] 
 181                 format
['ext'] = 'flv' 
 183                 format
['url'] = f
['url'] 
 185             formats
.append(format
) 
 187         self
._check
_formats
(formats
, video_id
) 
 188         self
._sort
_formats
(formats
) 
 190         info_dict
['formats'] = formats
 
 194 class ArteTVPlus7IE(ArteTVBaseIE
): 
 195     IE_NAME 
= 'arte.tv:+7' 
 196     _VALID_URL 
= r
'https?://(?:(?:www|sites)\.)?arte\.tv/(?:[^/]+/)?(?P<lang>fr|de|en|es)/(?:videos/)?(?:[^/]+/)*(?P<id>[^/?#&]+)' 
 199         'url': 'http://www.arte.tv/guide/de/sendungen/XEN/xenius/?vid=055918-015_PLUS7-D', 
 200         'only_matching': True, 
 202         'url': 'http://sites.arte.tv/karambolage/de/video/karambolage-22', 
 203         'only_matching': True, 
 205         'url': 'http://www.arte.tv/de/videos/048696-000-A/der-kluge-bauch-unser-zweites-gehirn', 
 206         'only_matching': True, 
 210     def suitable(cls
, url
): 
 211         return False if ArteTVPlaylistIE
.suitable(url
) else super(ArteTVPlus7IE
, cls
).suitable(url
) 
 213     def _real_extract(self
, url
): 
 214         video_id
, lang 
= self
._extract
_url
_info
(url
) 
 215         webpage 
= self
._download
_webpage
(url
, video_id
) 
 216         return self
._extract
_from
_webpage
(webpage
, video_id
, lang
) 
 218     def _extract_from_webpage(self
, webpage
, video_id
, lang
): 
 219         patterns_templates 
= (r
'arte_vp_url=["\'](.*?
%s.*?
)["\']', r'data-url=["\']([^
"]+%s[^"]+)["\']') 
 221         # some pages contain multiple videos (like 
 222         # http://www.arte.tv/guide/de/sendungen/XEN/xenius/?vid=055918-015_PLUS7-D), 
 223         # so we first try to look for json URLs that contain the video id from 
 224         # the 'vid' parameter. 
 225         patterns = [t % re.escape(_id) for _id in ids for t in patterns_templates] 
 226         json_url = self._html_search_regex( 
 227             patterns, webpage, 'json vp url', default=None) 
 229             def find_iframe_url(webpage, default=NO_DEFAULT): 
 230                 return self._html_search_regex( 
 231                     r'<iframe[^>]+src=(["\'])(?P
<url
>.+\bjson
_url
=.+?
)\
1', 
 232                     webpage, 'iframe url
', group='url
', default=default) 
 234             iframe_url = find_iframe_url(webpage, None) 
 236                 embed_url = self._html_search_regex( 
 237                     r'arte_vp_url_oembed
=\'([^
\']+?
)\'', webpage, 'embed url
', default=None) 
 239                     player = self._download_json( 
 240                         embed_url, video_id, 'Downloading player page
') 
 241                     iframe_url = find_iframe_url(player['html
']) 
 242             # en and es URLs produce react-based pages with different layout (e.g. 
 243             # http://www.arte.tv/guide/en/053330-002-A/carnival-italy?zone=world) 
 245                 program = self._search_regex( 
 246                     r'program\s
*:\s
*({.+?
["\']embed_html["\'].+?
}),?\s
*\n', 
 247                     webpage, 'program
', default=None) 
 249                     embed_html = self._parse_json(program, video_id) 
 251                         iframe_url = find_iframe_url(embed_html['embed_html
']) 
 253                 json_url = compat_parse_qs( 
 254                     compat_urllib_parse_urlparse(iframe_url).query)['json_url
'][0] 
 256             title = self._search_regex( 
 257                 r'<h3
[^
>]+title
=(["\'])(?P<title>.+?)\1', 
 258                 webpage, 'title', default=None, group='title') 
 259             return self._extract_from_json_url(json_url, video_id, lang, title=title) 
 260         # Different kind of embed URL (e.g. 
 261         # http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium) 
 264             for _, url in re.findall(r'<iframe[^>]+src=(["\'])(?P
<url
>.+?
)\
1', webpage)] 
 265         return self.playlist_result(entries) 
 268 # It also uses the arte_vp_url url from the webpage to extract the information 
 269 class ArteTVCreativeIE(ArteTVPlus7IE): 
 270     IE_NAME = 'arte
.tv
:creative
' 
 271     _VALID_URL = r'https?
://creative\
.arte\
.tv
/(?P
<lang
>fr|de|en|es
)/(?
:[^
/]+/)*(?P
<id>[^
/?
#&]+)' 
 274         'url': 'http://creative.arte.tv/fr/episode/osmosis-episode-1', 
 276             'id': '057405-001-A', 
 278             'title': 'OSMOSIS - N\'AYEZ PLUS PEUR D\'AIMER (1)', 
 279             'upload_date': '20150716', 
 282         'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion', 
 283         'playlist_count': 11, 
 284         'add_ie': ['Youtube'], 
 286         'url': 'http://creative.arte.tv/de/episode/agentur-amateur-4-der-erste-kunde', 
 287         'only_matching': True, 
 291 class ArteTVInfoIE(ArteTVPlus7IE
): 
 292     IE_NAME 
= 'arte.tv:info' 
 293     _VALID_URL 
= r
'https?://info\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)' 
 296         'url': 'http://info.arte.tv/fr/service-civique-un-cache-misere', 
 298             'id': '067528-000-A', 
 300             'title': 'Service civique, un cache misère ?', 
 301             'upload_date': '20160403', 
 306 class ArteTVFutureIE(ArteTVPlus7IE
): 
 307     IE_NAME 
= 'arte.tv:future' 
 308     _VALID_URL 
= r
'https?://future\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)' 
 311         'url': 'http://future.arte.tv/fr/info-sciences/les-ecrevisses-aussi-sont-anxieuses', 
 313             'id': '050940-028-A', 
 315             'title': 'Les écrevisses aussi peuvent être anxieuses', 
 316             'upload_date': '20140902', 
 319         'url': 'http://future.arte.tv/fr/la-science-est-elle-responsable', 
 320         'only_matching': True, 
 324 class ArteTVDDCIE(ArteTVPlus7IE
): 
 325     IE_NAME 
= 'arte.tv:ddc' 
 326     _VALID_URL 
= r
'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>[^/?#&]+)' 
 330     def _real_extract(self
, url
): 
 331         video_id
, lang 
= self
._extract
_url
_info
(url
) 
 334         elif lang 
== 'emission': 
 336         webpage 
= self
._download
_webpage
(url
, video_id
) 
 337         scriptElement 
= get_element_by_attribute('class', 'visu_video_block', webpage
) 
 338         script_url 
= self
._html
_search
_regex
(r
'src="(.*?)"', scriptElement
, 'script url') 
 339         javascriptPlayerGenerator 
= self
._download
_webpage
(script_url
, video_id
, 'Download javascript player generator') 
 340         json_url 
= self
._search
_regex
(r
"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator
, 'json url') 
 341         return self
._extract
_from
_json
_url
(json_url
, video_id
, lang
) 
 344 class ArteTVConcertIE(ArteTVPlus7IE
): 
 345     IE_NAME 
= 'arte.tv:concert' 
 346     _VALID_URL 
= r
'https?://concert\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)' 
 349         'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde', 
 350         'md5': '9ea035b7bd69696b67aa2ccaaa218161', 
 354             'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"', 
 355             'upload_date': '20140128', 
 356             'description': 'md5:486eb08f991552ade77439fe6d82c305', 
 361 class ArteTVCinemaIE(ArteTVPlus7IE
): 
 362     IE_NAME 
= 'arte.tv:cinema' 
 363     _VALID_URL 
= r
'https?://cinema\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>.+)' 
 366         'url': 'http://cinema.arte.tv/fr/article/les-ailes-du-desir-de-julia-reck', 
 367         'md5': 'a5b9dd5575a11d93daf0e3f404f45438', 
 369             'id': '062494-000-A', 
 371             'title': 'Film lauréat du concours web - "Les ailes du désir" de Julia Reck', 
 372             'upload_date': '20150807', 
 377 class ArteTVMagazineIE(ArteTVPlus7IE
): 
 378     IE_NAME 
= 'arte.tv:magazine' 
 379     _VALID_URL 
= r
'https?://(?:www\.)?arte\.tv/magazine/[^/]+/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)' 
 382         # Embedded via <iframe src="http://www.arte.tv/arte_vp/index.php?json_url=..." 
 383         'url': 'http://www.arte.tv/magazine/trepalium/fr/entretien-avec-le-realisateur-vincent-lannoo-trepalium', 
 384         'md5': '2a9369bcccf847d1c741e51416299f25', 
 386             'id': '065965-000-A', 
 388             'title': 'Trepalium - Extrait Ep.01', 
 389             'upload_date': '20160121', 
 392         # Embedded via <iframe src="http://www.arte.tv/guide/fr/embed/054813-004-A/medium" 
 393         'url': 'http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium', 
 394         'md5': 'fedc64fc7a946110fe311634e79782ca', 
 396             'id': '054813-004_PLUS7-F', 
 398             'title': 'Trepalium (4/6)', 
 399             'description': 'md5:10057003c34d54e95350be4f9b05cb40', 
 400             'upload_date': '20160218', 
 403         'url': 'http://www.arte.tv/magazine/metropolis/de/frank-woeste-german-paris-metropolis', 
 404         'only_matching': True, 
 408 class ArteTVEmbedIE(ArteTVPlus7IE
): 
 409     IE_NAME 
= 'arte.tv:embed' 
 410     _VALID_URL 
= r
'''(?x) 
 412         /(?:playerv2/embed|arte_vp/index)\.php\?json_url= 
 414             http://arte\.tv/papi/tvguide/videos/stream/player/ 
 415             (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]* 
 421     def _real_extract(self
, url
): 
 422         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 423         video_id 
= mobj
.group('id') 
 424         lang 
= mobj
.group('lang') 
 425         json_url 
= mobj
.group('json_url') 
 426         return self
._extract
_from
_json
_url
(json_url
, video_id
, lang
) 
 429 class TheOperaPlatformIE(ArteTVPlus7IE
): 
 430     IE_NAME 
= 'theoperaplatform' 
 431     _VALID_URL 
= r
'https?://(?:www\.)?theoperaplatform\.eu/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)' 
 434         'url': 'http://www.theoperaplatform.eu/de/opera/verdi-otello', 
 435         'md5': '970655901fa2e82e04c00b955e9afe7b', 
 437             'id': '060338-009-A', 
 439             'title': 'Verdi - OTELLO', 
 440             'upload_date': '20160927', 
 445 class ArteTVPlaylistIE(ArteTVBaseIE
): 
 446     IE_NAME 
= 'arte.tv:playlist' 
 447     _VALID_URL 
= r
'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de|en|es)/[^#]*#collection/(?P<id>PL-\d+)' 
 450         'url': 'http://www.arte.tv/guide/de/plus7/?country=DE#collection/PL-013263/ARTETV', 
 453             'title': 'Areva & Uramin', 
 454             'description': 'md5:a1dc0312ce357c262259139cfd48c9bf', 
 456         'playlist_mincount': 6, 
 458         'url': 'http://www.arte.tv/guide/de/playlists?country=DE#collection/PL-013190/ARTETV', 
 459         'only_matching': True, 
 462     def _real_extract(self
, url
): 
 463         playlist_id
, lang 
= self
._extract
_url
_info
(url
) 
 464         collection 
= self
._download
_json
( 
 465             'https://api.arte.tv/api/player/v1/collectionData/%s/%s?source=videos' 
 466             % (lang
, playlist_id
), playlist_id
) 
 467         title 
= collection
.get('title') 
 468         description 
= collection
.get('shortDescription') or collection
.get('teaserText') 
 470             self
._extract
_from
_json
_url
( 
 471                 video
['jsonUrl'], video
.get('programId') or playlist_id
, lang
) 
 472             for video 
in collection
['videos'] if video
.get('jsonUrl')] 
 473         return self
.playlist_result(entries
, playlist_id
, title
, description
)