viki.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. from __future__ import unicode_literals
  2. import re
  3. import time
  4. import hmac
  5. import hashlib
  6. from ..utils import (
  7. ExtractorError,
  8. int_or_none,
  9. parse_age_limit,
  10. parse_iso8601,
  11. )
  12. from .common import InfoExtractor
  13. class VikiBaseIE(InfoExtractor):
  14. _API_QUERY_TEMPLATE = '/v4/%sapp=%s&t=%s&site=www.viki.com'
  15. _API_URL_TEMPLATE = 'http://api.viki.io%s&sig=%s'
  16. _APP = '65535a'
  17. _APP_VERSION = '2.2.5.1428709186'
  18. _APP_SECRET = '-$iJ}@p7!G@SyU/je1bEyWg}upLu-6V6-Lg9VD(]siH,r.,m-r|ulZ,U4LC/SeR)'
  19. def _prepare_call(self, path, timestamp=None):
  20. path += '?' if '?' not in path else '&'
  21. if not timestamp:
  22. timestamp = int(time.time())
  23. query = self._API_QUERY_TEMPLATE % (path, self._APP, timestamp)
  24. sig = hmac.new(
  25. self._APP_SECRET.encode('ascii'),
  26. query.encode('ascii'),
  27. hashlib.sha1
  28. ).hexdigest()
  29. return self._API_URL_TEMPLATE % (query, sig)
  30. def _call_api(self, path, video_id, note, timestamp=None):
  31. resp = self._download_json(
  32. self._prepare_call(path, timestamp), video_id, note)
  33. error = resp.get('error')
  34. if error:
  35. if error == 'invalid timestamp':
  36. resp = self._download_json(
  37. self._prepare_call(path, int(resp['current_timestamp'])),
  38. video_id, '%s (retry)' % note)
  39. error = resp.get('error')
  40. if error:
  41. self._raise_error(resp['error'])
  42. return resp
  43. def _raise_error(self, error):
  44. raise ExtractorError(
  45. '%s returned error: %s' % (self.IE_NAME, error),
  46. expected=True)
  47. class VikiIE(VikiBaseIE):
  48. IE_NAME = 'viki'
  49. _VALID_URL = r'https?://(?:www\.)?viki\.com/(?:videos|player)/(?P<id>[0-9]+v)'
  50. _TESTS = [{
  51. 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
  52. 'info_dict': {
  53. 'id': '1023585v',
  54. 'ext': 'mp4',
  55. 'title': 'Heirs Episode 14',
  56. 'uploader': 'SBS',
  57. 'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e',
  58. 'upload_date': '20131121',
  59. 'age_limit': 13,
  60. },
  61. 'skip': 'Blocked in the US',
  62. }, {
  63. # clip
  64. 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
  65. 'md5': '86c0b5dbd4d83a6611a79987cc7a1989',
  66. 'info_dict': {
  67. 'id': '1067139v',
  68. 'ext': 'mp4',
  69. 'title': "'The Avengers: Age of Ultron' Press Conference",
  70. 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
  71. 'duration': 352,
  72. 'timestamp': 1430380829,
  73. 'upload_date': '20150430',
  74. 'uploader': 'Arirang TV',
  75. 'like_count': int,
  76. 'age_limit': 0,
  77. }
  78. }, {
  79. 'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi',
  80. 'info_dict': {
  81. 'id': '1048879v',
  82. 'ext': 'mp4',
  83. 'title': 'Ankhon Dekhi',
  84. 'duration': 6512,
  85. 'timestamp': 1408532356,
  86. 'upload_date': '20140820',
  87. 'uploader': 'Spuul',
  88. 'like_count': int,
  89. 'age_limit': 13,
  90. },
  91. 'params': {
  92. # m3u8 download
  93. 'skip_download': True,
  94. }
  95. }, {
  96. # episode
  97. 'url': 'http://www.viki.com/videos/44699v-boys-over-flowers-episode-1',
  98. 'md5': '190f3ef426005ba3a080a63325955bc3',
  99. 'info_dict': {
  100. 'id': '44699v',
  101. 'ext': 'mp4',
  102. 'title': 'Boys Over Flowers - Episode 1',
  103. 'description': 'md5:52617e4f729c7d03bfd4bcbbb6e946f2',
  104. 'duration': 4155,
  105. 'timestamp': 1270496524,
  106. 'upload_date': '20100405',
  107. 'uploader': 'group8',
  108. 'like_count': int,
  109. 'age_limit': 13,
  110. }
  111. }, {
  112. 'url': 'http://www.viki.com/player/44699v',
  113. 'only_matching': True,
  114. }]
  115. def _real_extract(self, url):
  116. video_id = self._match_id(url)
  117. streams = self._call_api(
  118. 'videos/%s/streams.json' % video_id, video_id,
  119. 'Downloading video streams JSON')
  120. formats = []
  121. for format_id, stream_dict in streams.items():
  122. height = self._search_regex(
  123. r'^(\d+)[pP]$', format_id, 'height', default=None)
  124. for protocol, format_dict in stream_dict.items():
  125. if format_id == 'm3u8':
  126. formats = self._extract_m3u8_formats(
  127. format_dict['url'], video_id, 'mp4', m3u8_id='m3u8-%s' % protocol)
  128. else:
  129. formats.append({
  130. 'url': format_dict['url'],
  131. 'format_id': '%s-%s' % (format_id, protocol),
  132. 'height': height,
  133. })
  134. self._sort_formats(formats)
  135. video = self._call_api(
  136. 'videos/%s.json' % video_id, video_id, 'Downloading video JSON')
  137. title = None
  138. titles = video.get('titles')
  139. if titles:
  140. title = titles.get('en') or titles[titles.keys()[0]]
  141. if not title:
  142. title = 'Episode %d' % video.get('number') if video.get('type') == 'episode' else video.get('id') or video_id
  143. container_titles = video.get('container', {}).get('titles')
  144. if container_titles:
  145. container_title = container_titles.get('en') or container_titles[titles.keys()[0]]
  146. title = '%s - %s' % (container_title, title)
  147. descriptions = video.get('descriptions')
  148. description = descriptions.get('en') or descriptions[titles.keys()[0]] if descriptions else None
  149. duration = int_or_none(video.get('duration'))
  150. timestamp = parse_iso8601(video.get('created_at'))
  151. uploader = video.get('author')
  152. like_count = int_or_none(video.get('likes', {}).get('count'))
  153. age_limit = parse_age_limit(video.get('rating'))
  154. thumbnails = []
  155. for thumbnail_id, thumbnail in video.get('images', {}).items():
  156. thumbnails.append({
  157. 'id': thumbnail_id,
  158. 'url': thumbnail.get('url'),
  159. })
  160. subtitles = {}
  161. for subtitle_lang, _ in video.get('subtitle_completions', {}).items():
  162. subtitles[subtitle_lang] = [{
  163. 'ext': subtitles_format,
  164. 'url': self._prepare_call(
  165. 'videos/%s/subtitles/%s.%s' % (video_id, subtitle_lang, subtitles_format)),
  166. } for subtitles_format in ('srt', 'vtt')]
  167. return {
  168. 'id': video_id,
  169. 'title': title,
  170. 'description': description,
  171. 'duration': duration,
  172. 'timestamp': timestamp,
  173. 'uploader': uploader,
  174. 'like_count': like_count,
  175. 'age_limit': age_limit,
  176. 'thumbnails': thumbnails,
  177. 'formats': formats,
  178. 'subtitles': subtitles,
  179. }
  180. class VikiChannelIE(InfoExtractor):
  181. IE_NAME = 'viki:channel'
  182. _VALID_URL = r'https?://(?:www\.)?viki\.com/tv/(?P<id>[0-9]+c)'
  183. _TESTS = [{
  184. 'url': 'http://www.viki.com/tv/50c-boys-over-flowers',
  185. 'info_dict': {
  186. 'id': '50c',
  187. 'title': 'Boys Over Flowers',
  188. 'description': 'md5:ecd3cff47967fe193cff37c0bec52790',
  189. },
  190. 'playlist_count': 70,
  191. }, {
  192. 'url': 'http://www.viki.com/tv/1354c-poor-nastya-complete',
  193. 'info_dict': {
  194. 'id': '1354c',
  195. 'title': 'Poor Nastya [COMPLETE]',
  196. 'description': 'md5:05bf5471385aa8b21c18ad450e350525',
  197. },
  198. 'playlist_count': 127,
  199. }]
  200. _API_BASE = 'http://api.viki.io/v4/containers'
  201. _APP = '100000a'
  202. _PER_PAGE = 25
  203. def _real_extract(self, url):
  204. channel_id = self._match_id(url)
  205. channel = self._download_json(
  206. '%s/%s.json?app=%s' % (self._API_BASE, channel_id, self._APP),
  207. channel_id, 'Downloading channel JSON')
  208. titles = channel['titles']
  209. title = titles.get('en') or titles[titles.keys()[0]]
  210. descriptions = channel['descriptions']
  211. description = descriptions.get('en') or descriptions[descriptions.keys()[0]]
  212. entries = []
  213. for video_type in ('episodes', 'clips'):
  214. page_url = '%s/%s/%s.json?app=%s&per_page=%d&sort=number&direction=asc&with_paging=true&page=1' % (self._API_BASE, channel_id, video_type, self._APP, self._PER_PAGE)
  215. while page_url:
  216. page = self._download_json(
  217. page_url, channel_id,
  218. 'Downloading %s JSON page #%s'
  219. % (video_type, re.search(r'[?&]page=([0-9]+)', page_url).group(1)))
  220. for video in page['response']:
  221. video_id = video['id']
  222. entries.append(self.url_result(
  223. 'http://www.viki.com/videos/%s' % video_id, 'Viki', video_id))
  224. page_url = page['pagination']['next']
  225. return self.playlist_result(entries, channel_id, title, description)