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.

256 lines
8.7 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_kwargs,
  7. compat_str,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. int_or_none,
  12. )
  13. class ViuBaseIE(InfoExtractor):
  14. def _real_initialize(self):
  15. viu_auth_res = self._request_webpage(
  16. 'https://www.viu.com/api/apps/v2/authenticate', None,
  17. 'Requesting Viu auth', query={
  18. 'acct': 'test',
  19. 'appid': 'viu_desktop',
  20. 'fmt': 'json',
  21. 'iid': 'guest',
  22. 'languageid': 'default',
  23. 'platform': 'desktop',
  24. 'userid': 'guest',
  25. 'useridtype': 'guest',
  26. 'ver': '1.0'
  27. }, headers=self.geo_verification_headers())
  28. self._auth_token = viu_auth_res.info()['X-VIU-AUTH']
  29. def _call_api(self, path, *args, **kwargs):
  30. headers = self.geo_verification_headers()
  31. headers.update({
  32. 'X-VIU-AUTH': self._auth_token
  33. })
  34. headers.update(kwargs.get('headers', {}))
  35. kwargs['headers'] = headers
  36. response = self._download_json(
  37. 'https://www.viu.com/api/' + path, *args,
  38. **compat_kwargs(kwargs))['response']
  39. if response.get('status') != 'success':
  40. raise ExtractorError('%s said: %s' % (
  41. self.IE_NAME, response['message']), expected=True)
  42. return response
  43. class ViuIE(ViuBaseIE):
  44. _VALID_URL = r'(?:viu:|https?://[^/]+\.viu\.com/[a-z]{2}/media/)(?P<id>\d+)'
  45. _TESTS = [{
  46. 'url': 'https://www.viu.com/en/media/1116705532?containerId=playlist-22168059',
  47. 'info_dict': {
  48. 'id': '1116705532',
  49. 'ext': 'mp4',
  50. 'title': 'Citizen Khan - Ep 1',
  51. 'description': 'md5:d7ea1604f49e5ba79c212c551ce2110e',
  52. },
  53. 'params': {
  54. 'skip_download': 'm3u8 download',
  55. },
  56. 'skip': 'Geo-restricted to India',
  57. }, {
  58. 'url': 'https://www.viu.com/en/media/1130599965',
  59. 'info_dict': {
  60. 'id': '1130599965',
  61. 'ext': 'mp4',
  62. 'title': 'Jealousy Incarnate - Episode 1',
  63. 'description': 'md5:d3d82375cab969415d2720b6894361e9',
  64. },
  65. 'params': {
  66. 'skip_download': 'm3u8 download',
  67. },
  68. 'skip': 'Geo-restricted to Indonesia',
  69. }, {
  70. 'url': 'https://india.viu.com/en/media/1126286865',
  71. 'only_matching': True,
  72. }]
  73. def _real_extract(self, url):
  74. video_id = self._match_id(url)
  75. video_data = self._call_api(
  76. 'clip/load', video_id, 'Downloading video data', query={
  77. 'appid': 'viu_desktop',
  78. 'fmt': 'json',
  79. 'id': video_id
  80. })['item'][0]
  81. title = video_data['title']
  82. m3u8_url = None
  83. url_path = video_data.get('urlpathd') or video_data.get('urlpath')
  84. tdirforwhole = video_data.get('tdirforwhole')
  85. # #EXT-X-BYTERANGE is not supported by native hls downloader
  86. # and ffmpeg (#10955)
  87. # hls_file = video_data.get('hlsfile')
  88. hls_file = video_data.get('jwhlsfile')
  89. if url_path and tdirforwhole and hls_file:
  90. m3u8_url = '%s/%s/%s' % (url_path, tdirforwhole, hls_file)
  91. else:
  92. # m3u8_url = re.sub(
  93. # r'(/hlsc_)[a-z]+(\d+\.m3u8)',
  94. # r'\1whe\2', video_data['href'])
  95. m3u8_url = video_data['href']
  96. formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
  97. self._sort_formats(formats)
  98. subtitles = {}
  99. for key, value in video_data.items():
  100. mobj = re.match(r'^subtitle_(?P<lang>[^_]+)_(?P<ext>(vtt|srt))', key)
  101. if not mobj:
  102. continue
  103. subtitles.setdefault(mobj.group('lang'), []).append({
  104. 'url': value,
  105. 'ext': mobj.group('ext')
  106. })
  107. return {
  108. 'id': video_id,
  109. 'title': title,
  110. 'description': video_data.get('description'),
  111. 'series': video_data.get('moviealbumshowname'),
  112. 'episode': title,
  113. 'episode_number': int_or_none(video_data.get('episodeno')),
  114. 'duration': int_or_none(video_data.get('duration')),
  115. 'formats': formats,
  116. 'subtitles': subtitles,
  117. }
  118. class ViuPlaylistIE(ViuBaseIE):
  119. IE_NAME = 'viu:playlist'
  120. _VALID_URL = r'https?://www\.viu\.com/[^/]+/listing/playlist-(?P<id>\d+)'
  121. _TEST = {
  122. 'url': 'https://www.viu.com/en/listing/playlist-22461380',
  123. 'info_dict': {
  124. 'id': '22461380',
  125. 'title': 'The Good Wife',
  126. },
  127. 'playlist_count': 16,
  128. 'skip': 'Geo-restricted to Indonesia',
  129. }
  130. def _real_extract(self, url):
  131. playlist_id = self._match_id(url)
  132. playlist_data = self._call_api(
  133. 'container/load', playlist_id,
  134. 'Downloading playlist info', query={
  135. 'appid': 'viu_desktop',
  136. 'fmt': 'json',
  137. 'id': 'playlist-' + playlist_id
  138. })['container']
  139. entries = []
  140. for item in playlist_data.get('item', []):
  141. item_id = item.get('id')
  142. if not item_id:
  143. continue
  144. item_id = compat_str(item_id)
  145. entries.append(self.url_result(
  146. 'viu:' + item_id, 'Viu', item_id))
  147. return self.playlist_result(
  148. entries, playlist_id, playlist_data.get('title'))
  149. class ViuOTTIE(InfoExtractor):
  150. IE_NAME = 'viu:ott'
  151. _VALID_URL = r'https?://(?:www\.)?viu\.com/ott/(?P<country_code>[a-z]{2})/[a-z]{2}-[a-z]{2}/vod/(?P<id>\d+)'
  152. _TESTS = [{
  153. 'url': 'http://www.viu.com/ott/sg/en-us/vod/3421/The%20Prime%20Minister%20and%20I',
  154. 'info_dict': {
  155. 'id': '3421',
  156. 'ext': 'mp4',
  157. 'title': 'A New Beginning',
  158. 'description': 'md5:1e7486a619b6399b25ba6a41c0fe5b2c',
  159. },
  160. 'params': {
  161. 'skip_download': 'm3u8 download',
  162. },
  163. 'skip': 'Geo-restricted to Singapore',
  164. }, {
  165. 'url': 'http://www.viu.com/ott/hk/zh-hk/vod/7123/%E5%A4%A7%E4%BA%BA%E5%A5%B3%E5%AD%90',
  166. 'info_dict': {
  167. 'id': '7123',
  168. 'ext': 'mp4',
  169. 'title': '這就是我的生活之道',
  170. 'description': 'md5:4eb0d8b08cf04fcdc6bbbeb16043434f',
  171. },
  172. 'params': {
  173. 'skip_download': 'm3u8 download',
  174. },
  175. 'skip': 'Geo-restricted to Hong Kong',
  176. }]
  177. def _real_extract(self, url):
  178. country_code, video_id = re.match(self._VALID_URL, url).groups()
  179. product_data = self._download_json(
  180. 'http://www.viu.com/ott/%s/index.php' % country_code, video_id,
  181. 'Downloading video info', query={
  182. 'r': 'vod/ajax-detail',
  183. 'platform_flag_label': 'web',
  184. 'product_id': video_id,
  185. })['data']
  186. video_data = product_data.get('current_product')
  187. if not video_data:
  188. raise ExtractorError('This video is not available in your region.', expected=True)
  189. stream_data = self._download_json(
  190. 'https://d1k2us671qcoau.cloudfront.net/distribute_web_%s.php' % country_code,
  191. video_id, 'Downloading stream info', query={
  192. 'ccs_product_id': video_data['ccs_product_id'],
  193. })['data']['stream']
  194. stream_sizes = stream_data.get('size', {})
  195. formats = []
  196. for vid_format, stream_url in stream_data.get('url', {}).items():
  197. height = int_or_none(self._search_regex(
  198. r's(\d+)p', vid_format, 'height', default=None))
  199. formats.append({
  200. 'format_id': vid_format,
  201. 'url': stream_url,
  202. 'height': height,
  203. 'ext': 'mp4',
  204. 'filesize': int_or_none(stream_sizes.get(vid_format))
  205. })
  206. self._sort_formats(formats)
  207. subtitles = {}
  208. for sub in video_data.get('subtitle', []):
  209. sub_url = sub.get('url')
  210. if not sub_url:
  211. continue
  212. subtitles.setdefault(sub.get('name'), []).append({
  213. 'url': sub_url,
  214. 'ext': 'srt',
  215. })
  216. title = video_data['synopsis'].strip()
  217. return {
  218. 'id': video_id,
  219. 'title': title,
  220. 'description': video_data.get('description'),
  221. 'series': product_data.get('series', {}).get('name'),
  222. 'episode': title,
  223. 'episode_number': int_or_none(video_data.get('number')),
  224. 'duration': int_or_none(stream_data.get('duration')),
  225. 'thumbnail': video_data.get('cover_image_url'),
  226. 'formats': formats,
  227. 'subtitles': subtitles,
  228. }