]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/viki.py
   2 from __future__ 
import unicode_literals
 
  11 from .common 
import InfoExtractor
 
  21 class VikiBaseIE(InfoExtractor
): 
  22     _VALID_URL_BASE 
= r
'https?://(?:www\.)?viki\.(?:com|net|mx|jp|fr)/' 
  23     _API_QUERY_TEMPLATE 
= '/v4/%sapp=%s&t=%s&site=www.viki.com' 
  24     _API_URL_TEMPLATE 
= 'http://api.viki.io%s&sig=%s' 
  27     _APP_VERSION 
= '2.2.5.1428709186' 
  28     _APP_SECRET 
= 'MM_d*yP@`&1@]@!AVrXf_o-HVEnoTnm$O-ti4[G~$JDI/Dc-&piU&z&5.;:}95=Iad' 
  31     _NETRC_MACHINE 
= 'viki' 
  36         'geo': 'Sorry, this content is not available in your region.', 
  37         'upcoming': 'Sorry, this content is not yet available.', 
  38         # 'paywall': 'paywall', 
  41     def _prepare_call(self
, path
, timestamp
=None, post_data
=None): 
  42         path 
+= '?' if '?' not in path 
else '&' 
  44             timestamp 
= int(time
.time()) 
  45         query 
= self
._API
_QUERY
_TEMPLATE 
% (path
, self
._APP
, timestamp
) 
  47             query 
+= '&token=%s' % self
._token
 
  49             self
._APP
_SECRET
.encode('ascii'), 
  50             query
.encode('ascii'), 
  53         url 
= self
._API
_URL
_TEMPLATE 
% (query
, sig
) 
  54         return sanitized_Request( 
  55             url
, json
.dumps(post_data
).encode('utf-8')) if post_data 
else url
 
  57     def _call_api(self
, path
, video_id
, note
, timestamp
=None, post_data
=None): 
  58         resp 
= self
._download
_json
( 
  59             self
._prepare
_call
(path
, timestamp
, post_data
), video_id
, note
) 
  61         error 
= resp
.get('error') 
  63             if error 
== 'invalid timestamp': 
  64                 resp 
= self
._download
_json
( 
  65                     self
._prepare
_call
(path
, int(resp
['current_timestamp']), post_data
), 
  66                     video_id
, '%s (retry)' % note
) 
  67                 error 
= resp
.get('error') 
  69                 self
._raise
_error
(resp
['error']) 
  73     def _raise_error(self
, error
): 
  75             '%s returned error: %s' % (self
.IE_NAME
, error
), 
  78     def _check_errors(self
, data
): 
  79         for reason
, status 
in data
.get('blocking', {}).items(): 
  80             if status 
and reason 
in self
._ERRORS
: 
  81                 message 
= self
._ERRORS
[reason
] 
  83                     self
.raise_geo_restricted(msg
=message
) 
  84                 raise ExtractorError('%s said: %s' % ( 
  85                     self
.IE_NAME
, message
), expected
=True) 
  87     def _real_initialize(self
): 
  91         (username
, password
) = self
._get
_login
_info
() 
 100         login 
= self
._call
_api
( 
 101             'sessions.json', None, 
 102             'Logging in', post_data
=login_form
) 
 104         self
._token 
= login
.get('token') 
 106             self
.report_warning('Unable to get session token, login has probably failed') 
 109     def dict_selection(dict_obj
, preferred_key
, allow_fallback
=True): 
 110         if preferred_key 
in dict_obj
: 
 111             return dict_obj
.get(preferred_key
) 
 113         if not allow_fallback
: 
 116         filtered_dict 
= list(filter(None, [dict_obj
.get(k
) for k 
in dict_obj
.keys()])) 
 117         return filtered_dict
[0] if filtered_dict 
else None 
 120 class VikiIE(VikiBaseIE
): 
 122     _VALID_URL 
= r
'%s(?:videos|player)/(?P<id>[0-9]+v)' % VikiBaseIE
._VALID
_URL
_BASE
 
 124         'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14', 
 128             'title': 'Heirs Episode 14', 
 130             'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e', 
 131             'upload_date': '20131121', 
 134         'skip': 'Blocked in the US', 
 137         'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference', 
 138         'md5': '86c0b5dbd4d83a6611a79987cc7a1989', 
 142             'title': "'The Avengers: Age of Ultron' Press Conference", 
 143             'description': 'md5:d70b2f9428f5488321bfe1db10d612ea', 
 145             'timestamp': 1430380829, 
 146             'upload_date': '20150430', 
 147             'uploader': 'Arirang TV', 
 152         'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi', 
 156             'title': 'Ankhon Dekhi', 
 158             'timestamp': 1408532356, 
 159             'upload_date': '20140820', 
 164         'skip': 'Blocked in the US', 
 167         'url': 'http://www.viki.com/videos/44699v-boys-over-flowers-episode-1', 
 168         'md5': '5fa476a902e902783ac7a4d615cdbc7a', 
 172             'title': 'Boys Over Flowers - Episode 1', 
 173             'description': 'md5:b89cf50038b480b88b5b3c93589a9076', 
 175             'timestamp': 1270496524, 
 176             'upload_date': '20100405', 
 177             'uploader': 'group8', 
 183         'url': 'http://www.viki.com/videos/50562v-poor-nastya-complete-episode-1', 
 184         'md5': '63f8600c1da6f01b7640eee7eca4f1da', 
 188             'title': 'Poor Nastya [COMPLETE] - Episode 1', 
 191             'timestamp': 1274949505, 
 192             'upload_date': '20101213', 
 193             'uploader': 'ad14065n', 
 194             'uploader_id': 'ad14065n', 
 199         'url': 'http://www.viki.com/player/44699v', 
 200         'only_matching': True, 
 202         # non-English description 
 203         'url': 'http://www.viki.com/videos/158036v-love-in-magic', 
 204         'md5': '1713ae35df5a521b31f6dc40730e7c9c', 
 208             'uploader': 'I Planet Entertainment', 
 209             'upload_date': '20111122', 
 210             'timestamp': 1321985454, 
 211             'description': 'md5:44b1e46619df3a072294645c770cef36', 
 212             'title': 'Love In Magic', 
 217     def _real_extract(self
, url
): 
 218         video_id 
= self
._match
_id
(url
) 
 220         video 
= self
._call
_api
( 
 221             'videos/%s.json' % video_id
, video_id
, 'Downloading video JSON') 
 223         self
._check
_errors
(video
) 
 225         title 
= self
.dict_selection(video
.get('titles', {}), 'en', allow_fallback
=False) 
 227             title 
= 'Episode %d' % video
.get('number') if video
.get('type') == 'episode' else video
.get('id') or video_id
 
 228             container_titles 
= video
.get('container', {}).get('titles', {}) 
 229             container_title 
= self
.dict_selection(container_titles
, 'en') 
 230             title 
= '%s - %s' % (container_title
, title
) 
 232         description 
= self
.dict_selection(video
.get('descriptions', {}), 'en') 
 234         duration 
= int_or_none(video
.get('duration')) 
 235         timestamp 
= parse_iso8601(video
.get('created_at')) 
 236         uploader 
= video
.get('author') 
 237         like_count 
= int_or_none(video
.get('likes', {}).get('count')) 
 238         age_limit 
= parse_age_limit(video
.get('rating')) 
 241         for thumbnail_id
, thumbnail 
in video
.get('images', {}).items(): 
 244                 'url': thumbnail
.get('url'), 
 248         for subtitle_lang
, _ 
in video
.get('subtitle_completions', {}).items(): 
 249             subtitles
[subtitle_lang
] = [{ 
 250                 'ext': subtitles_format
, 
 251                 'url': self
._prepare
_call
( 
 252                     'videos/%s/subtitles/%s.%s' % (video_id
, subtitle_lang
, subtitles_format
)), 
 253             } for subtitles_format 
in ('srt', 'vtt')] 
 258             'description': description
, 
 259             'duration': duration
, 
 260             'timestamp': timestamp
, 
 261             'uploader': uploader
, 
 262             'like_count': like_count
, 
 263             'age_limit': age_limit
, 
 264             'thumbnails': thumbnails
, 
 265             'subtitles': subtitles
, 
 268         streams 
= self
._call
_api
( 
 269             'videos/%s/streams.json' % video_id
, video_id
, 
 270             'Downloading video streams JSON') 
 272         if 'external' in streams
: 
 274                 '_type': 'url_transparent', 
 275                 'url': streams
['external']['url'], 
 280         for format_id
, stream_dict 
in streams
.items(): 
 281             height 
= int_or_none(self
._search
_regex
( 
 282                 r
'^(\d+)[pP]$', format_id
, 'height', default
=None)) 
 283             for protocol
, format_dict 
in stream_dict
.items(): 
 284                 # rtmps URLs does not seem to work 
 285                 if protocol 
== 'rtmps': 
 287                 format_url 
= format_dict
['url'] 
 288                 if format_id 
== 'm3u8': 
 289                     m3u8_formats 
= self
._extract
_m
3u8_formats
( 
 290                         format_url
, video_id
, 'mp4', 
 291                         entry_protocol
='m3u8_native', 
 292                         m3u8_id
='m3u8-%s' % protocol
, fatal
=False) 
 293                     # Despite CODECS metadata in m3u8 all video-only formats 
 294                     # are actually video+audio 
 295                     for f 
in m3u8_formats
: 
 296                         if f
.get('acodec') == 'none' and f
.get('vcodec') != 'none': 
 298                     formats
.extend(m3u8_formats
) 
 299                 elif format_url
.startswith('rtmp'): 
 301                         r
'^(?P<url>rtmp://[^/]+/(?P<app>.+?))/(?P<playpath>mp4:.+)$', 
 306                         'format_id': 'rtmp-%s' % format_id
, 
 308                         'url': mobj
.group('url'), 
 309                         'play_path': mobj
.group('playpath'), 
 310                         'app': mobj
.group('app'), 
 316                         'format_id': '%s-%s' % (format_id
, protocol
), 
 319         self
._sort
_formats
(formats
) 
 321         result
['formats'] = formats
 
 325 class VikiChannelIE(VikiBaseIE
): 
 326     IE_NAME 
= 'viki:channel' 
 327     _VALID_URL 
= r
'%s(?:tv|news|movies|artists)/(?P<id>[0-9]+c)' % VikiBaseIE
._VALID
_URL
_BASE
 
 329         'url': 'http://www.viki.com/tv/50c-boys-over-flowers', 
 332             'title': 'Boys Over Flowers', 
 333             'description': 'md5:ecd3cff47967fe193cff37c0bec52790', 
 335         'playlist_mincount': 71, 
 337         'url': 'http://www.viki.com/tv/1354c-poor-nastya-complete', 
 340             'title': 'Poor Nastya [COMPLETE]', 
 341             'description': 'md5:05bf5471385aa8b21c18ad450e350525', 
 343         'playlist_count': 127, 
 345         'url': 'http://www.viki.com/news/24569c-showbiz-korea', 
 346         'only_matching': True, 
 348         'url': 'http://www.viki.com/movies/22047c-pride-and-prejudice-2005', 
 349         'only_matching': True, 
 351         'url': 'http://www.viki.com/artists/2141c-shinee', 
 352         'only_matching': True, 
 357     def _real_extract(self
, url
): 
 358         channel_id 
= self
._match
_id
(url
) 
 360         channel 
= self
._call
_api
( 
 361             'containers/%s.json' % channel_id
, channel_id
, 
 362             'Downloading channel JSON') 
 364         self
._check
_errors
(channel
) 
 366         title 
= self
.dict_selection(channel
['titles'], 'en') 
 368         description 
= self
.dict_selection(channel
['descriptions'], 'en') 
 371         for video_type 
in ('episodes', 'clips', 'movies'): 
 372             for page_num 
in itertools
.count(1): 
 373                 page 
= self
._call
_api
( 
 374                     'containers/%s/%s.json?per_page=%d&sort=number&direction=asc&with_paging=true&page=%d' 
 375                     % (channel_id
, video_type
, self
._PER
_PAGE
, page_num
), channel_id
, 
 376                     'Downloading %s JSON page #%d' % (video_type
, page_num
)) 
 377                 for video 
in page
['response']: 
 378                     video_id 
= video
['id'] 
 379                     entries
.append(self
.url_result( 
 380                         'http://www.viki.com/videos/%s' % video_id
, 'Viki')) 
 381                 if not page
['pagination']['next']: 
 384         return self
.playlist_result(entries
, channel_id
, title
, description
)