viu.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. })
  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. hls_file = video_data.get('hlsfile')
  79. if url_path and tdirforwhole and hls_file:
  80. m3u8_url = '%s/%s/%s' % (url_path, tdirforwhole, hls_file)
  81. else:
  82. m3u8_url = re.sub(
  83. r'(/hlsc_)[a-z]+(\d+\.m3u8)',
  84. r'\1whe\2', video_data['href'])
  85. formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
  86. self._sort_formats(formats)
  87. subtitles = {}
  88. for key, value in video_data.items():
  89. mobj = re.match(r'^subtitle_(?P<lang>[^_]+)_(?P<ext>(vtt|srt))', key)
  90. if not mobj:
  91. continue
  92. subtitles.setdefault(mobj.group('lang'), []).append({
  93. 'url': value,
  94. 'ext': mobj.group('ext')
  95. })
  96. return {
  97. 'id': video_id,
  98. 'title': title,
  99. 'description': video_data.get('description'),
  100. 'series': video_data.get('moviealbumshowname'),
  101. 'episode': title,
  102. 'episode_number': int_or_none(video_data.get('episodeno')),
  103. 'duration': int_or_none(video_data.get('duration')),
  104. 'formats': formats,
  105. 'subtitles': subtitles,
  106. }
  107. class ViuPlaylistIE(ViuBaseIE):
  108. IE_NAME = 'viu:playlist'
  109. _VALID_URL = r'https?://www\.viu\.com/[^/]+/listing/playlist-(?P<id>\d+)'
  110. _TEST = {
  111. 'url': 'https://www.viu.com/en/listing/playlist-22461380',
  112. 'info_dict': {
  113. 'id': '22461380',
  114. 'title': 'The Good Wife',
  115. },
  116. 'playlist_count': 16,
  117. 'skip': 'Geo-restricted to Indonesia',
  118. }
  119. def _real_extract(self, url):
  120. playlist_id = self._match_id(url)
  121. playlist_data = self._call_api(
  122. 'container/load', playlist_id,
  123. 'Downloading playlist info', query={
  124. 'appid': 'viu_desktop',
  125. 'fmt': 'json',
  126. 'id': 'playlist-' + playlist_id
  127. })['container']
  128. entries = []
  129. for item in playlist_data.get('item', []):
  130. item_id = item.get('id')
  131. if not item_id:
  132. continue
  133. item_id = compat_str(item_id)
  134. entries.append(self.url_result(
  135. 'viu:' + item_id, 'Viu', item_id))
  136. return self.playlist_result(
  137. entries, playlist_id, playlist_data.get('title'))
  138. class ViuOTTIE(InfoExtractor):
  139. IE_NAME = 'viu:ott'
  140. _VALID_URL = r'https?://(?:www\.)?viu\.com/ott/(?P<country_code>[a-z]{2})/[a-z]{2}-[a-z]{2}/vod/(?P<id>\d+)'
  141. _TESTS = [{
  142. 'url': 'http://www.viu.com/ott/sg/en-us/vod/3421/The%20Prime%20Minister%20and%20I',
  143. 'info_dict': {
  144. 'id': '3421',
  145. 'ext': 'mp4',
  146. 'title': 'A New Beginning',
  147. 'description': 'md5:1e7486a619b6399b25ba6a41c0fe5b2c',
  148. },
  149. 'params': {
  150. 'skip_download': 'm3u8 download',
  151. },
  152. 'skip': 'Geo-restricted to Singapore',
  153. }, {
  154. 'url': 'http://www.viu.com/ott/hk/zh-hk/vod/7123/%E5%A4%A7%E4%BA%BA%E5%A5%B3%E5%AD%90',
  155. 'info_dict': {
  156. 'id': '7123',
  157. 'ext': 'mp4',
  158. 'title': '這就是我的生活之道',
  159. 'description': 'md5:4eb0d8b08cf04fcdc6bbbeb16043434f',
  160. },
  161. 'params': {
  162. 'skip_download': 'm3u8 download',
  163. },
  164. 'skip': 'Geo-restricted to Hong Kong',
  165. }]
  166. def _real_extract(self, url):
  167. country_code, video_id = re.match(self._VALID_URL, url).groups()
  168. product_data = self._download_json(
  169. 'http://www.viu.com/ott/%s/index.php' % country_code, video_id,
  170. 'Downloading video info', query={
  171. 'r': 'vod/ajax-detail',
  172. 'platform_flag_label': 'web',
  173. 'product_id': video_id,
  174. })['data']
  175. video_data = product_data.get('current_product')
  176. if not video_data:
  177. raise ExtractorError('This video is not available in your region.', expected=True)
  178. stream_data = self._download_json(
  179. 'https://d1k2us671qcoau.cloudfront.net/distribute_web_%s.php' % country_code,
  180. video_id, 'Downloading stream info', query={
  181. 'ccs_product_id': video_data['ccs_product_id'],
  182. })['data']['stream']
  183. stream_sizes = stream_data.get('size', {})
  184. formats = []
  185. for vid_format, stream_url in stream_data.get('url', {}).items():
  186. height = int_or_none(self._search_regex(
  187. r's(\d+)p', vid_format, 'height', default=None))
  188. formats.append({
  189. 'format_id': vid_format,
  190. 'url': stream_url,
  191. 'height': height,
  192. 'ext': 'mp4',
  193. 'filesize': int_or_none(stream_sizes.get(vid_format))
  194. })
  195. self._sort_formats(formats)
  196. subtitles = {}
  197. for sub in video_data.get('subtitle', []):
  198. sub_url = sub.get('url')
  199. if not sub_url:
  200. continue
  201. subtitles.setdefault(sub.get('name'), []).append({
  202. 'url': sub_url,
  203. 'ext': 'srt',
  204. })
  205. title = video_data['synopsis'].strip()
  206. return {
  207. 'id': video_id,
  208. 'title': title,
  209. 'description': video_data.get('description'),
  210. 'series': product_data.get('series', {}).get('name'),
  211. 'episode': title,
  212. 'episode_number': int_or_none(video_data.get('number')),
  213. 'duration': int_or_none(stream_data.get('duration')),
  214. 'thumbnail': video_data.get('cover_image_url'),
  215. 'formats': formats,
  216. 'subtitles': subtitles,
  217. }