logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

viu.py (9267B)


  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. _AREA_ID = {
  178. 'HK': 1,
  179. 'SG': 2,
  180. 'TH': 4,
  181. 'PH': 5,
  182. }
  183. def _real_extract(self, url):
  184. country_code, video_id = re.match(self._VALID_URL, url).groups()
  185. query = {
  186. 'r': 'vod/ajax-detail',
  187. 'platform_flag_label': 'web',
  188. 'product_id': video_id,
  189. }
  190. area_id = self._AREA_ID.get(country_code.upper())
  191. if area_id:
  192. query['area_id'] = area_id
  193. product_data = self._download_json(
  194. 'http://www.viu.com/ott/%s/index.php' % country_code, video_id,
  195. 'Downloading video info', query=query)['data']
  196. video_data = product_data.get('current_product')
  197. if not video_data:
  198. raise ExtractorError('This video is not available in your region.', expected=True)
  199. stream_data = self._download_json(
  200. 'https://d1k2us671qcoau.cloudfront.net/distribute_web_%s.php' % country_code,
  201. video_id, 'Downloading stream info', query={
  202. 'ccs_product_id': video_data['ccs_product_id'],
  203. }, headers={
  204. 'Referer': url,
  205. 'Origin': re.search(r'https?://[^/]+', url).group(0),
  206. })['data']['stream']
  207. stream_sizes = stream_data.get('size', {})
  208. formats = []
  209. for vid_format, stream_url in stream_data.get('url', {}).items():
  210. height = int_or_none(self._search_regex(
  211. r's(\d+)p', vid_format, 'height', default=None))
  212. formats.append({
  213. 'format_id': vid_format,
  214. 'url': stream_url,
  215. 'height': height,
  216. 'ext': 'mp4',
  217. 'filesize': int_or_none(stream_sizes.get(vid_format))
  218. })
  219. self._sort_formats(formats)
  220. subtitles = {}
  221. for sub in video_data.get('subtitle', []):
  222. sub_url = sub.get('url')
  223. if not sub_url:
  224. continue
  225. subtitles.setdefault(sub.get('name'), []).append({
  226. 'url': sub_url,
  227. 'ext': 'srt',
  228. })
  229. title = video_data['synopsis'].strip()
  230. return {
  231. 'id': video_id,
  232. 'title': title,
  233. 'description': video_data.get('description'),
  234. 'series': product_data.get('series', {}).get('name'),
  235. 'episode': title,
  236. 'episode_number': int_or_none(video_data.get('number')),
  237. 'duration': int_or_none(stream_data.get('duration')),
  238. 'thumbnail': video_data.get('cover_image_url'),
  239. 'formats': formats,
  240. 'subtitles': subtitles,
  241. }