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.

249 lines
8.6 KiB

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