You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
12 KiB

11 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
11 years ago
11 years ago
11 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import time
  5. import hmac
  6. import hashlib
  7. import itertools
  8. from .common import InfoExtractor
  9. from ..utils import (
  10. ExtractorError,
  11. int_or_none,
  12. parse_age_limit,
  13. parse_iso8601,
  14. sanitized_Request,
  15. )
  16. class VikiBaseIE(InfoExtractor):
  17. _VALID_URL_BASE = r'https?://(?:www\.)?viki\.(?:com|net|mx|jp|fr)/'
  18. _API_QUERY_TEMPLATE = '/v4/%sapp=%s&t=%s&site=www.viki.com'
  19. _API_URL_TEMPLATE = 'http://api.viki.io%s&sig=%s'
  20. _APP = '65535a'
  21. _APP_VERSION = '2.2.5.1428709186'
  22. _APP_SECRET = '-$iJ}@p7!G@SyU/je1bEyWg}upLu-6V6-Lg9VD(]siH,r.,m-r|ulZ,U4LC/SeR)'
  23. _NETRC_MACHINE = 'viki'
  24. _token = None
  25. _ERRORS = {
  26. 'geo': 'Sorry, this content is not available in your region.',
  27. 'upcoming': 'Sorry, this content is not yet available.',
  28. # 'paywall': 'paywall',
  29. }
  30. def _prepare_call(self, path, timestamp=None, post_data=None):
  31. path += '?' if '?' not in path else '&'
  32. if not timestamp:
  33. timestamp = int(time.time())
  34. query = self._API_QUERY_TEMPLATE % (path, self._APP, timestamp)
  35. if self._token:
  36. query += '&token=%s' % self._token
  37. sig = hmac.new(
  38. self._APP_SECRET.encode('ascii'),
  39. query.encode('ascii'),
  40. hashlib.sha1
  41. ).hexdigest()
  42. url = self._API_URL_TEMPLATE % (query, sig)
  43. return sanitized_Request(
  44. url, json.dumps(post_data).encode('utf-8')) if post_data else url
  45. def _call_api(self, path, video_id, note, timestamp=None, post_data=None):
  46. resp = self._download_json(
  47. self._prepare_call(path, timestamp, post_data), video_id, note)
  48. error = resp.get('error')
  49. if error:
  50. if error == 'invalid timestamp':
  51. resp = self._download_json(
  52. self._prepare_call(path, int(resp['current_timestamp']), post_data),
  53. video_id, '%s (retry)' % note)
  54. error = resp.get('error')
  55. if error:
  56. self._raise_error(resp['error'])
  57. return resp
  58. def _raise_error(self, error):
  59. raise ExtractorError(
  60. '%s returned error: %s' % (self.IE_NAME, error),
  61. expected=True)
  62. def _check_errors(self, data):
  63. for reason, status in data.get('blocking', {}).items():
  64. if status and reason in self._ERRORS:
  65. raise ExtractorError('%s said: %s' % (
  66. self.IE_NAME, self._ERRORS[reason]), expected=True)
  67. def _real_initialize(self):
  68. self._login()
  69. def _login(self):
  70. (username, password) = self._get_login_info()
  71. if username is None:
  72. return
  73. login_form = {
  74. 'login_id': username,
  75. 'password': password,
  76. }
  77. login = self._call_api(
  78. 'sessions.json', None,
  79. 'Logging in as %s' % username, post_data=login_form)
  80. self._token = login.get('token')
  81. if not self._token:
  82. self.report_warning('Unable to get session token, login has probably failed')
  83. @staticmethod
  84. def dict_selection(dict_obj, preferred_key, allow_fallback=True):
  85. if preferred_key in dict_obj:
  86. return dict_obj.get(preferred_key)
  87. if not allow_fallback:
  88. return
  89. filtered_dict = list(filter(None, [dict_obj.get(k) for k in dict_obj.keys()]))
  90. return filtered_dict[0] if filtered_dict else None
  91. class VikiIE(VikiBaseIE):
  92. IE_NAME = 'viki'
  93. _VALID_URL = r'%s(?:videos|player)/(?P<id>[0-9]+v)' % VikiBaseIE._VALID_URL_BASE
  94. _TESTS = [{
  95. 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
  96. 'info_dict': {
  97. 'id': '1023585v',
  98. 'ext': 'mp4',
  99. 'title': 'Heirs Episode 14',
  100. 'uploader': 'SBS',
  101. 'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e',
  102. 'upload_date': '20131121',
  103. 'age_limit': 13,
  104. },
  105. 'skip': 'Blocked in the US',
  106. }, {
  107. # clip
  108. 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
  109. 'md5': '86c0b5dbd4d83a6611a79987cc7a1989',
  110. 'info_dict': {
  111. 'id': '1067139v',
  112. 'ext': 'mp4',
  113. 'title': "'The Avengers: Age of Ultron' Press Conference",
  114. 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
  115. 'duration': 352,
  116. 'timestamp': 1430380829,
  117. 'upload_date': '20150430',
  118. 'uploader': 'Arirang TV',
  119. 'like_count': int,
  120. 'age_limit': 0,
  121. }
  122. }, {
  123. 'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi',
  124. 'info_dict': {
  125. 'id': '1048879v',
  126. 'ext': 'mp4',
  127. 'title': 'Ankhon Dekhi',
  128. 'duration': 6512,
  129. 'timestamp': 1408532356,
  130. 'upload_date': '20140820',
  131. 'uploader': 'Spuul',
  132. 'like_count': int,
  133. 'age_limit': 13,
  134. },
  135. 'skip': 'Blocked in the US',
  136. }, {
  137. # episode
  138. 'url': 'http://www.viki.com/videos/44699v-boys-over-flowers-episode-1',
  139. 'md5': '5fa476a902e902783ac7a4d615cdbc7a',
  140. 'info_dict': {
  141. 'id': '44699v',
  142. 'ext': 'mp4',
  143. 'title': 'Boys Over Flowers - Episode 1',
  144. 'description': 'md5:b89cf50038b480b88b5b3c93589a9076',
  145. 'duration': 4204,
  146. 'timestamp': 1270496524,
  147. 'upload_date': '20100405',
  148. 'uploader': 'group8',
  149. 'like_count': int,
  150. 'age_limit': 13,
  151. }
  152. }, {
  153. # youtube external
  154. 'url': 'http://www.viki.com/videos/50562v-poor-nastya-complete-episode-1',
  155. 'md5': '63f8600c1da6f01b7640eee7eca4f1da',
  156. 'info_dict': {
  157. 'id': '50562v',
  158. 'ext': 'webm',
  159. 'title': 'Poor Nastya [COMPLETE] - Episode 1',
  160. 'description': '',
  161. 'duration': 606,
  162. 'timestamp': 1274949505,
  163. 'upload_date': '20101213',
  164. 'uploader': 'ad14065n',
  165. 'uploader_id': 'ad14065n',
  166. 'like_count': int,
  167. 'age_limit': 13,
  168. }
  169. }, {
  170. 'url': 'http://www.viki.com/player/44699v',
  171. 'only_matching': True,
  172. }, {
  173. # non-English description
  174. 'url': 'http://www.viki.com/videos/158036v-love-in-magic',
  175. 'md5': '1713ae35df5a521b31f6dc40730e7c9c',
  176. 'info_dict': {
  177. 'id': '158036v',
  178. 'ext': 'mp4',
  179. 'uploader': 'I Planet Entertainment',
  180. 'upload_date': '20111122',
  181. 'timestamp': 1321985454,
  182. 'description': 'md5:44b1e46619df3a072294645c770cef36',
  183. 'title': 'Love In Magic',
  184. 'age_limit': 13,
  185. },
  186. }]
  187. def _real_extract(self, url):
  188. video_id = self._match_id(url)
  189. video = self._call_api(
  190. 'videos/%s.json' % video_id, video_id, 'Downloading video JSON')
  191. self._check_errors(video)
  192. title = self.dict_selection(video.get('titles', {}), 'en', allow_fallback=False)
  193. if not title:
  194. title = 'Episode %d' % video.get('number') if video.get('type') == 'episode' else video.get('id') or video_id
  195. container_titles = video.get('container', {}).get('titles', {})
  196. container_title = self.dict_selection(container_titles, 'en')
  197. title = '%s - %s' % (container_title, title)
  198. description = self.dict_selection(video.get('descriptions', {}), 'en')
  199. duration = int_or_none(video.get('duration'))
  200. timestamp = parse_iso8601(video.get('created_at'))
  201. uploader = video.get('author')
  202. like_count = int_or_none(video.get('likes', {}).get('count'))
  203. age_limit = parse_age_limit(video.get('rating'))
  204. thumbnails = []
  205. for thumbnail_id, thumbnail in video.get('images', {}).items():
  206. thumbnails.append({
  207. 'id': thumbnail_id,
  208. 'url': thumbnail.get('url'),
  209. })
  210. subtitles = {}
  211. for subtitle_lang, _ in video.get('subtitle_completions', {}).items():
  212. subtitles[subtitle_lang] = [{
  213. 'ext': subtitles_format,
  214. 'url': self._prepare_call(
  215. 'videos/%s/subtitles/%s.%s' % (video_id, subtitle_lang, subtitles_format)),
  216. } for subtitles_format in ('srt', 'vtt')]
  217. result = {
  218. 'id': video_id,
  219. 'title': title,
  220. 'description': description,
  221. 'duration': duration,
  222. 'timestamp': timestamp,
  223. 'uploader': uploader,
  224. 'like_count': like_count,
  225. 'age_limit': age_limit,
  226. 'thumbnails': thumbnails,
  227. 'subtitles': subtitles,
  228. }
  229. streams = self._call_api(
  230. 'videos/%s/streams.json' % video_id, video_id,
  231. 'Downloading video streams JSON')
  232. if 'external' in streams:
  233. result.update({
  234. '_type': 'url_transparent',
  235. 'url': streams['external']['url'],
  236. })
  237. return result
  238. formats = []
  239. for format_id, stream_dict in streams.items():
  240. height = int_or_none(self._search_regex(
  241. r'^(\d+)[pP]$', format_id, 'height', default=None))
  242. for protocol, format_dict in stream_dict.items():
  243. if format_id == 'm3u8':
  244. m3u8_formats = self._extract_m3u8_formats(
  245. format_dict['url'], video_id, 'mp4',
  246. entry_protocol='m3u8_native', preference=-1,
  247. m3u8_id='m3u8-%s' % protocol, fatal=False)
  248. # Despite CODECS metadata in m3u8 all video-only formats
  249. # are actually video+audio
  250. for f in m3u8_formats:
  251. if f.get('acodec') == 'none' and f.get('vcodec') != 'none':
  252. f['acodec'] = None
  253. formats.extend(m3u8_formats)
  254. else:
  255. formats.append({
  256. 'url': format_dict['url'],
  257. 'format_id': '%s-%s' % (format_id, protocol),
  258. 'height': height,
  259. })
  260. self._sort_formats(formats)
  261. result['formats'] = formats
  262. return result
  263. class VikiChannelIE(VikiBaseIE):
  264. IE_NAME = 'viki:channel'
  265. _VALID_URL = r'%s(?:tv|news|movies|artists)/(?P<id>[0-9]+c)' % VikiBaseIE._VALID_URL_BASE
  266. _TESTS = [{
  267. 'url': 'http://www.viki.com/tv/50c-boys-over-flowers',
  268. 'info_dict': {
  269. 'id': '50c',
  270. 'title': 'Boys Over Flowers',
  271. 'description': 'md5:ecd3cff47967fe193cff37c0bec52790',
  272. },
  273. 'playlist_mincount': 71,
  274. }, {
  275. 'url': 'http://www.viki.com/tv/1354c-poor-nastya-complete',
  276. 'info_dict': {
  277. 'id': '1354c',
  278. 'title': 'Poor Nastya [COMPLETE]',
  279. 'description': 'md5:05bf5471385aa8b21c18ad450e350525',
  280. },
  281. 'playlist_count': 127,
  282. }, {
  283. 'url': 'http://www.viki.com/news/24569c-showbiz-korea',
  284. 'only_matching': True,
  285. }, {
  286. 'url': 'http://www.viki.com/movies/22047c-pride-and-prejudice-2005',
  287. 'only_matching': True,
  288. }, {
  289. 'url': 'http://www.viki.com/artists/2141c-shinee',
  290. 'only_matching': True,
  291. }]
  292. _PER_PAGE = 25
  293. def _real_extract(self, url):
  294. channel_id = self._match_id(url)
  295. channel = self._call_api(
  296. 'containers/%s.json' % channel_id, channel_id,
  297. 'Downloading channel JSON')
  298. self._check_errors(channel)
  299. title = self.dict_selection(channel['titles'], 'en')
  300. description = self.dict_selection(channel['descriptions'], 'en')
  301. entries = []
  302. for video_type in ('episodes', 'clips', 'movies'):
  303. for page_num in itertools.count(1):
  304. page = self._call_api(
  305. 'containers/%s/%s.json?per_page=%d&sort=number&direction=asc&with_paging=true&page=%d'
  306. % (channel_id, video_type, self._PER_PAGE, page_num), channel_id,
  307. 'Downloading %s JSON page #%d' % (video_type, page_num))
  308. for video in page['response']:
  309. video_id = video['id']
  310. entries.append(self.url_result(
  311. 'http://www.viki.com/videos/%s' % video_id, 'Viki'))
  312. if not page['pagination']['next']:
  313. break
  314. return self.playlist_result(entries, channel_id, title, description)