]>
Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/dplay.py
a08dace4318a05dfacd9c6e9ad94508934b615ca
   2 from __future__ 
import unicode_literals
 
   8 from .common 
import InfoExtractor
 
  28 class DPlayIE(InfoExtractor
): 
  29     _VALID_URL 
= r
'https?://(?P<domain>www\.(?P<host>dplay\.(?P<country>dk|se|no)))/(?:videoer/)?(?P<id>[^/]+/[^/?#]+)' 
  32         # non geo restricted, via secure api, unsigned download hls URL 
  33         'url': 'http://www.dplay.se/nugammalt-77-handelser-som-format-sverige/season-1-svensken-lar-sig-njuta-av-livet/', 
  36             'display_id': 'nugammalt-77-handelser-som-format-sverige/season-1-svensken-lar-sig-njuta-av-livet', 
  38             'title': 'Svensken lär sig njuta av livet', 
  39             'description': 'md5:d3819c9bccffd0fe458ca42451dd50d8', 
  41             'timestamp': 1365454320, 
  42             'upload_date': '20130408', 
  43             'creator': 'Kanal 5 (Home)', 
  44             'series': 'Nugammalt - 77 händelser som format Sverige', 
  50         # geo restricted, via secure api, unsigned download hls URL 
  51         'url': 'http://www.dplay.dk/mig-og-min-mor/season-6-episode-12/', 
  54             'display_id': 'mig-og-min-mor/season-6-episode-12', 
  56             'title': 'Episode 12', 
  57             'description': 'md5:9c86e51a93f8a4401fc9641ef9894c90', 
  59             'timestamp': 1429696800, 
  60             'upload_date': '20150422', 
  61             'creator': 'Kanal 4 (Home)', 
  62             'series': 'Mig og min mor', 
  68         # geo restricted, via direct unsigned hls URL 
  69         'url': 'http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/', 
  70         'only_matching': True, 
  73         'url': 'https://www.dplay.no/videoer/i-kongens-klr/sesong-1-episode-7', 
  76             'display_id': 'i-kongens-klr/sesong-1-episode-7', 
  79             'description': 'md5:e3e1411b2b9aebeea36a6ec5d50c60cf', 
  81             'timestamp': 1516726800, 
  82             'upload_date': '20180123', 
  83             'series': 'I kongens klær', 
  88             'format': 'bestvideo', 
  89             'skip_download': True, 
  92         # geo restricted, bypassable via X-Forwarded-For 
  93         'url': 'https://www.dplay.dk/videoer/singleliv/season-5-episode-3', 
  94         'only_matching': True, 
  97     def _real_extract(self
, url
): 
  98         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  99         display_id 
= mobj
.group('id') 
 100         domain 
= mobj
.group('domain') 
 102         self
._initialize
_geo
_bypass
([mobj
.group('country').upper()]) 
 104         webpage 
= self
._download
_webpage
(url
, display_id
) 
 106         video_id 
= self
._search
_regex
( 
 107             r
'data-video-id=["\'](\d
+)', webpage, 'video 
id', default=None) 
 110             host = mobj.group('host
') 
 111             disco_base = 'https
://disco
-api
.%s' % host 
 113                 '%s/token
' % disco_base, display_id, 'Downloading token
', 
 115                     'realm
': host.replace('.', ''), 
 117             video = self._download_json( 
 118                 '%s/content
/videos
/%s' % (disco_base, display_id), display_id, 
 121                     'x
-disco
-client
': 'WEB
:UNKNOWN
:dplay
-client
:0.0.1', 
 125             video_id = video['data
']['id'] 
 126             info = video['data
']['attributes
'] 
 129             for format_id, format_dict in self._download_json( 
 130                     '%s/playback
/videoPlaybackInfo
/%s' % (disco_base, video_id), 
 131                     display_id)['data
']['attributes
']['streaming
'].items(): 
 132                 if not isinstance(format_dict, dict): 
 134                 format_url = format_dict.get('url
') 
 137                 ext = determine_ext(format_url) 
 138                 if format_id == 'dash
' or ext == 'mpd
': 
 139                     formats.extend(self._extract_mpd_formats( 
 140                         format_url, display_id, mpd_id='dash
', fatal=False)) 
 141                 elif format_id == 'hls
' or ext == 'm3u8
': 
 142                     formats.extend(self._extract_m3u8_formats( 
 143                         format_url, display_id, 'mp4
', 
 144                         entry_protocol='m3u8_native
', m3u8_id='hls
', 
 149                         'format_id
': format_id, 
 151             self._sort_formats(formats) 
 155                 included = video.get('included
') 
 156                 if isinstance(included, list): 
 157                     show = next(e for e in included if e.get('type') == 'show
') 
 159                         show, lambda x: x['attributes
']['name
'], compat_str) 
 160             except StopIteration: 
 165                 'display_id
': display_id, 
 167                 'description
': info.get('description
'), 
 168                 'duration
': float_or_none( 
 169                     info.get('videoDuration
'), scale=1000), 
 170                 'timestamp
': unified_timestamp(info.get('publishStart
')), 
 172                 'season_number
': int_or_none(info.get('seasonNumber
')), 
 173                 'episode_number
': int_or_none(info.get('episodeNumber
')), 
 174                 'age_limit
': int_or_none(info.get('minimum_age
')), 
 178         info = self._download_json( 
 179             'http
://%s/api
/v2
/ajax
/videos?video_id
=%s' % (domain, video_id), 
 182         title = info['title
'] 
 184         PROTOCOLS = ('hls
', 'hds
') 
 187         def extract_formats(protocol, manifest_url): 
 188             if protocol == 'hls
': 
 189                 m3u8_formats = self._extract_m3u8_formats( 
 190                     manifest_url, video_id, ext='mp4
', 
 191                     entry_protocol='m3u8_native
', m3u8_id=protocol, fatal=False) 
 192                 # Sometimes final URLs inside m3u8 are unsigned, let's fix this
 
 193                 # ourselves. Also fragments' URLs are only served signed for 
 195                 query 
= compat_urlparse
.parse_qs(compat_urlparse
.urlparse(manifest_url
).query
) 
 196                 for m3u8_format 
in m3u8_formats
: 
 198                         'url': update_url_query(m3u8_format
['url'], query
), 
 200                             'User-Agent': USER_AGENTS
['Safari'], 
 203                 formats
.extend(m3u8_formats
) 
 204             elif protocol 
== 'hds': 
 205                 formats
.extend(self
._extract
_f
4m
_formats
( 
 206                     manifest_url 
+ '&hdcore=3.8.0&plugin=flowplayer-3.8.0.0', 
 207                     video_id
, f4m_id
=protocol
, fatal
=False)) 
 209         domain_tld 
= domain
.split('.')[-1] 
 210         if domain_tld 
in ('se', 'dk', 'no'): 
 211             for protocol 
in PROTOCOLS
: 
 212                 # Providing dsc-geo allows to bypass geo restriction in some cases 
 214                     'secure.dplay.%s' % domain_tld
, 'dsc-geo', 
 216                         'countryCode': domain_tld
.upper(), 
 217                         'expiry': (time
.time() + 20 * 60) * 1000, 
 219                 stream 
= self
._download
_json
( 
 220                     'https://secure.dplay.%s/secure/api/v2/user/authorization/stream/%s?stream_type=%s' 
 221                     % (domain_tld
, video_id
, protocol
), video_id
, 
 222                     'Downloading %s stream JSON' % protocol
, fatal
=False) 
 223                 if stream 
and stream
.get(protocol
): 
 224                     extract_formats(protocol
, stream
[protocol
]) 
 226         # The last resort is to try direct unsigned hls/hds URLs from info dictionary. 
 227         # Sometimes this does work even when secure API with dsc-geo has failed (e.g. 
 228         # http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/). 
 230             for protocol 
in PROTOCOLS
: 
 231                 if info
.get(protocol
): 
 232                     extract_formats(protocol
, info
[protocol
]) 
 234         self
._sort
_formats
(formats
) 
 237         for lang 
in ('se', 'sv', 'da', 'nl', 'no'): 
 238             for format_id 
in ('web_vtt', 'vtt', 'srt'): 
 239                 subtitle_url 
= info
.get('subtitles_%s_%s' % (lang
, format_id
)) 
 241                     subtitles
.setdefault(lang
, []).append({'url': subtitle_url
}) 
 245             'display_id': display_id
, 
 247             'description': info
.get('video_metadata_longDescription'), 
 248             'duration': int_or_none(info
.get('video_metadata_length'), scale
=1000), 
 249             'timestamp': int_or_none(info
.get('video_publish_date')), 
 250             'creator': info
.get('video_metadata_homeChannel'), 
 251             'series': info
.get('video_metadata_show'), 
 252             'season_number': int_or_none(info
.get('season')), 
 253             'episode_number': int_or_none(info
.get('episode')), 
 254             'age_limit': int_or_none(info
.get('minimum_age')), 
 256             'subtitles': subtitles
, 
 260 class DPlayItIE(InfoExtractor
): 
 261     _VALID_URL 
= r
'https?://it\.dplay\.com/[^/]+/[^/]+/(?P<id>[^/?#]+)' 
 262     _GEO_COUNTRIES 
= ['IT'] 
 264         'url': 'http://it.dplay.com/nove/biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij/', 
 265         'md5': '2b808ffb00fc47b884a172ca5d13053c', 
 268             'display_id': 'luigi-di-maio-la-psicosi-di-stanislawskij', 
 270             'title': 'Biografie imbarazzanti: Luigi Di Maio: la psicosi di Stanislawskij', 
 271             'description': 'md5:3c7a4303aef85868f867a26f5cc14813', 
 272             'thumbnail': r
're:^https?://.*\.jpe?g', 
 273             'upload_date': '20160524', 
 274             'series': 'Biografie imbarazzanti', 
 276             'episode': 'Luigi Di Maio: la psicosi di Stanislawskij', 
 281     def _real_extract(self
, url
): 
 282         display_id 
= self
._match
_id
(url
) 
 284         webpage 
= self
._download
_webpage
(url
, display_id
) 
 286         title 
= remove_end(self
._og
_search
_title
(webpage
), ' | Dplay') 
 290         info 
= self
._search
_regex
( 
 291             r
'playback_json\s*:\s*JSON\.parse\s*\(\s*("(?:\\.|[^"\\])+?")', 
 292             webpage
, 'playback JSON', default
=None) 
 295                 info 
= self
._parse
_json
(info
, display_id
, fatal
=False) 
 299                 video_id 
= try_get(info
, lambda x
: x
['data']['id']) 
 302             info_url 
= self
._search
_regex
( 
 303                 r
'url\s*[:=]\s*["\']((?
:https?
:)?
//[^
/]+/playback
/videoPlaybackInfo
/\d
+)', 
 306             video_id = info_url.rpartition('/')[-1] 
 309                 info = self._download_json( 
 310                     info_url, display_id, headers={ 
 311                         'Authorization
': 'Bearer 
%s' % self._get_cookies(url).get( 
 312                             'dplayit_token
').value, 
 315             except ExtractorError as e: 
 316                 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (400, 403): 
 317                     info = self._parse_json(e.cause.read().decode('utf
-8'), display_id) 
 318                     error = info['errors
'][0] 
 319                     if error.get('code
') == 'access
.denied
.geoblocked
': 
 320                         self.raise_geo_restricted( 
 321                             msg=error.get('detail
'), countries=self._GEO_COUNTRIES) 
 322                     raise ExtractorError(info['errors
'][0]['detail
'], expected=True) 
 325         hls_url = info['data
']['attributes
']['streaming
']['hls
']['url
'] 
 327         formats = self._extract_m3u8_formats( 
 328             hls_url, display_id, ext='mp4
', entry_protocol='m3u8_native
', 
 331         series = self._html_search_regex( 
 332             r'(?s
)<h1
[^
>]+class=["\'].*?\bshow_title\b.*?["\'][^
>]*>(.+?
)</h1
>', 
 333             webpage, 'series
', fatal=False) 
 334         episode = self._search_regex( 
 335             r'<p
[^
>]+class=["\'].*?\bdesc_ep\b.*?["\'][^
>]*>\s
*<br
/>\s
*<b
>([^
<]+)', 
 336             webpage, 'episode
', fatal=False) 
 339             r'(?s
)<span
[^
>]+class=["\']dates["\'][^
>]*>.+?
\bS\
.(?P
<season_number
>\d
+)\s
+E\
.(?P
<episode_number
>\d
+)\s
*-\s
*(?P
<upload_date
>\d{2}
/\d{2}
/\d{4}
)', 
 342             season_number = int(mobj.group('season_number
')) 
 343             episode_number = int(mobj.group('episode_number
')) 
 344             upload_date = unified_strdate(mobj.group('upload_date
')) 
 346             season_number = episode_number = upload_date = None 
 349             'id': compat_str(video_id or display_id), 
 350             'display_id
': display_id, 
 352             'description
': self._og_search_description(webpage), 
 353             'thumbnail
': self._og_search_thumbnail(webpage), 
 355             'season_number
': season_number, 
 357             'episode_number
': episode_number, 
 358             'upload_date
': upload_date,