viceland.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import time
  4. import hashlib
  5. import json
  6. from .adobepass import AdobePass
  7. from ..compat import compat_HTTPError
  8. from ..utils import (
  9. int_or_none,
  10. parse_age_limit,
  11. str_or_none,
  12. parse_duration,
  13. ExtractorError,
  14. extract_attributes,
  15. )
  16. class VicelandIE(AdobePass):
  17. _VALID_URL = r'https?://(?:www\.)?viceland\.com/[^/]+/video/[^/]+/(?P<id>[a-f0-9]+)'
  18. _TEST = {
  19. # FIXME: fill the test after fixing delegation problem
  20. 'url': 'https://www.viceland.com/en_us/video/cyberwar-trailer/57608447973ee7705f6fbd4e',
  21. 'info_dict': {
  22. 'id': '57608447973ee7705f6fbd4e',
  23. 'ext': 'mp4',
  24. },
  25. 'params': {
  26. # m3u8 download
  27. 'skip_download': True,
  28. },
  29. 'add_ie': ['UplynkPreplay', 'Uplynk'],
  30. }
  31. def _real_extract(self, url):
  32. video_id = self._match_id(url)
  33. webpage = self._download_webpage(url, video_id)
  34. watch_hub_data = extract_attributes(self._search_regex(
  35. r'(?s)(<watch-hub\s*.+?</watch-hub>)', webpage, 'watch hub'))
  36. video_id = watch_hub_data['vms-id']
  37. title = watch_hub_data['video-title']
  38. query = {}
  39. if watch_hub_data.get('video-locked') == '1':
  40. resource = self._get_mvpd_resource(
  41. 'VICELAND', title, video_id,
  42. watch_hub_data.get('video-rating'))
  43. query['tvetoken'] = self._extract_mvpd_auth(url, video_id, 'VICELAND', resource)
  44. # signature generation algorithm is reverse engineered from signatureGenerator in
  45. # webpack:///../shared/~/vice-player/dist/js/vice-player.js in
  46. # https://www.viceland.com/assets/common/js/web.vendor.bundle.js
  47. exp = int(time.time()) + 14400
  48. query.update({
  49. 'exp': exp,
  50. 'sign': hashlib.sha512(('%s:GET:%d' % (video_id, exp)).encode()).hexdigest(),
  51. })
  52. try:
  53. preplay = self._download_json('https://www.viceland.com/en_us/preplay/%s' % video_id, video_id, query=query)
  54. except ExtractorError as e:
  55. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
  56. error = json.loads(e.cause.read().decode())
  57. raise ExtractorError('%s said: %s' % (self.IE_NAME, error['details']), expected=True)
  58. video_data = preplay['video']
  59. base = video_data['base']
  60. uplynk_preplay_url = preplay['preplayURL']
  61. episode = video_data.get('episode', {})
  62. channel = video_data.get('channel', {})
  63. subtitles = {}
  64. cc_url = preplay.get('ccURL')
  65. if cc_url:
  66. subtitles['en'] = [{
  67. 'url': cc_url,
  68. }]
  69. return {
  70. '_type': 'url_transparent',
  71. 'url': uplynk_preplay_url,
  72. 'id': video_id,
  73. 'title': title,
  74. 'description': base.get('body'),
  75. 'thumbnail': watch_hub_data.get('cover-image') or watch_hub_data.get('thumbnail'),
  76. 'duration': parse_duration(video_data.get('video_duration') or watch_hub_data.get('video-duration')),
  77. 'timestamp': int_or_none(video_data.get('created_at')),
  78. 'age_limit': parse_age_limit(video_data.get('video_rating')),
  79. 'series': video_data.get('show_title') or watch_hub_data.get('show-title'),
  80. 'episode_number': int_or_none(episode.get('episode_number') or watch_hub_data.get('episode')),
  81. 'episode_id': str_or_none(episode.get('id') or video_data.get('episode_id')),
  82. 'season_number': int_or_none(watch_hub_data.get('season')),
  83. 'season_id': str_or_none(episode.get('season_id')),
  84. 'uploader': channel.get('base', {}).get('title') or watch_hub_data.get('channel-title'),
  85. 'uploader_id': str_or_none(channel.get('id')),
  86. 'subtitles': subtitles,
  87. 'ie_key': 'UplynkPreplay',
  88. }