viu.py 8.7 KB

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