viceland.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. 'url': 'https://www.viceland.com/en_us/video/cyberwar-trailer/57608447973ee7705f6fbd4e',
  20. 'info_dict': {
  21. 'id': '57608447973ee7705f6fbd4e',
  22. 'ext': 'mp4',
  23. 'title': 'CYBERWAR (Trailer)',
  24. 'description': 'Tapping into the geopolitics of hacking and surveillance, Ben Makuch travels the world to meet with hackers, government officials, and dissidents to investigate the ecosystem of cyberwarfare.',
  25. 'age_limit': 14,
  26. 'timestamp': 1466008539,
  27. 'upload_date': '20160615',
  28. 'uploader_id': '11',
  29. 'uploader': 'Viceland',
  30. },
  31. 'params': {
  32. # m3u8 download
  33. 'skip_download': True,
  34. },
  35. 'add_ie': ['UplynkPreplay', 'Uplynk'],
  36. }
  37. def _real_extract(self, url):
  38. video_id = self._match_id(url)
  39. webpage = self._download_webpage(url, video_id)
  40. watch_hub_data = extract_attributes(self._search_regex(
  41. r'(?s)(<watch-hub\s*.+?</watch-hub>)', webpage, 'watch hub'))
  42. video_id = watch_hub_data['vms-id']
  43. title = watch_hub_data['video-title']
  44. query = {}
  45. if watch_hub_data.get('video-locked') == '1':
  46. resource = self._get_mvpd_resource(
  47. 'VICELAND', title, video_id,
  48. watch_hub_data.get('video-rating'))
  49. query['tvetoken'] = self._extract_mvpd_auth(url, video_id, 'VICELAND', resource)
  50. # signature generation algorithm is reverse engineered from signatureGenerator in
  51. # webpack:///../shared/~/vice-player/dist/js/vice-player.js in
  52. # https://www.viceland.com/assets/common/js/web.vendor.bundle.js
  53. exp = int(time.time()) + 14400
  54. query.update({
  55. 'exp': exp,
  56. 'sign': hashlib.sha512(('%s:GET:%d' % (video_id, exp)).encode()).hexdigest(),
  57. })
  58. try:
  59. preplay = self._download_json('https://www.viceland.com/en_us/preplay/%s' % video_id, video_id, query=query)
  60. except ExtractorError as e:
  61. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
  62. error = json.loads(e.cause.read().decode())
  63. raise ExtractorError('%s said: %s' % (self.IE_NAME, error['details']), expected=True)
  64. video_data = preplay['video']
  65. base = video_data['base']
  66. uplynk_preplay_url = preplay['preplayURL']
  67. episode = video_data.get('episode', {})
  68. channel = video_data.get('channel', {})
  69. subtitles = {}
  70. cc_url = preplay.get('ccURL')
  71. if cc_url:
  72. subtitles['en'] = [{
  73. 'url': cc_url,
  74. }]
  75. return {
  76. '_type': 'url_transparent',
  77. 'url': uplynk_preplay_url,
  78. 'id': video_id,
  79. 'title': title,
  80. 'description': base.get('body'),
  81. 'thumbnail': watch_hub_data.get('cover-image') or watch_hub_data.get('thumbnail'),
  82. 'duration': parse_duration(video_data.get('video_duration') or watch_hub_data.get('video-duration')),
  83. 'timestamp': int_or_none(video_data.get('created_at')),
  84. 'age_limit': parse_age_limit(video_data.get('video_rating')),
  85. 'series': video_data.get('show_title') or watch_hub_data.get('show-title'),
  86. 'episode_number': int_or_none(episode.get('episode_number') or watch_hub_data.get('episode')),
  87. 'episode_id': str_or_none(episode.get('id') or video_data.get('episode_id')),
  88. 'season_number': int_or_none(watch_hub_data.get('season')),
  89. 'season_id': str_or_none(episode.get('season_id')),
  90. 'uploader': channel.get('base', {}).get('title') or watch_hub_data.get('channel-title'),
  91. 'uploader_id': str_or_none(channel.get('id')),
  92. 'subtitles': subtitles,
  93. 'ie_key': 'UplynkPreplay',
  94. }