vice.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. from __future__ import unicode_literals
  2. import re
  3. import time
  4. import hashlib
  5. import json
  6. from .adobepass import AdobePassIE
  7. from .common import InfoExtractor
  8. from ..compat import compat_HTTPError
  9. from ..utils import (
  10. int_or_none,
  11. parse_age_limit,
  12. str_or_none,
  13. parse_duration,
  14. ExtractorError,
  15. extract_attributes,
  16. )
  17. class ViceBaseIE(AdobePassIE):
  18. def _extract_preplay_video(self, url, webpage):
  19. watch_hub_data = extract_attributes(self._search_regex(
  20. r'(?s)(<watch-hub\s*.+?</watch-hub>)', webpage, 'watch hub'))
  21. video_id = watch_hub_data['vms-id']
  22. title = watch_hub_data['video-title']
  23. query = {}
  24. is_locked = watch_hub_data.get('video-locked') == '1'
  25. if is_locked:
  26. resource = self._get_mvpd_resource(
  27. 'VICELAND', title, video_id,
  28. watch_hub_data.get('video-rating'))
  29. query['tvetoken'] = self._extract_mvpd_auth(url, video_id, 'VICELAND', resource)
  30. # signature generation algorithm is reverse engineered from signatureGenerator in
  31. # webpack:///../shared/~/vice-player/dist/js/vice-player.js in
  32. # https://www.viceland.com/assets/common/js/web.vendor.bundle.js
  33. exp = int(time.time()) + 14400
  34. query.update({
  35. 'exp': exp,
  36. 'sign': hashlib.sha512(('%s:GET:%d' % (video_id, exp)).encode()).hexdigest(),
  37. })
  38. try:
  39. host = 'www.viceland' if is_locked else self._PREPLAY_HOST
  40. preplay = self._download_json('https://%s.com/en_us/preplay/%s' % (host, video_id), video_id, query=query)
  41. except ExtractorError as e:
  42. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
  43. error = json.loads(e.cause.read().decode())
  44. raise ExtractorError('%s said: %s' % (self.IE_NAME, error['details']), expected=True)
  45. raise
  46. video_data = preplay['video']
  47. base = video_data['base']
  48. uplynk_preplay_url = preplay['preplayURL']
  49. episode = video_data.get('episode', {})
  50. channel = video_data.get('channel', {})
  51. subtitles = {}
  52. cc_url = preplay.get('ccURL')
  53. if cc_url:
  54. subtitles['en'] = [{
  55. 'url': cc_url,
  56. }]
  57. return {
  58. '_type': 'url_transparent',
  59. 'url': uplynk_preplay_url,
  60. 'id': video_id,
  61. 'title': title,
  62. 'description': base.get('body'),
  63. 'thumbnail': watch_hub_data.get('cover-image') or watch_hub_data.get('thumbnail'),
  64. 'duration': parse_duration(video_data.get('video_duration') or watch_hub_data.get('video-duration')),
  65. 'timestamp': int_or_none(video_data.get('created_at')),
  66. 'age_limit': parse_age_limit(video_data.get('video_rating')),
  67. 'series': video_data.get('show_title') or watch_hub_data.get('show-title'),
  68. 'episode_number': int_or_none(episode.get('episode_number') or watch_hub_data.get('episode')),
  69. 'episode_id': str_or_none(episode.get('id') or video_data.get('episode_id')),
  70. 'season_number': int_or_none(watch_hub_data.get('season')),
  71. 'season_id': str_or_none(episode.get('season_id')),
  72. 'uploader': channel.get('base', {}).get('title') or watch_hub_data.get('channel-title'),
  73. 'uploader_id': str_or_none(channel.get('id')),
  74. 'subtitles': subtitles,
  75. 'ie_key': 'UplynkPreplay',
  76. }
  77. class ViceIE(ViceBaseIE):
  78. _VALID_URL = r'https?://(?:.+?\.)?vice\.com/(?:[^/]+/)?videos?/(?P<id>[^/?#&]+)'
  79. _TESTS = [{
  80. 'url': 'http://www.vice.com/video/cowboy-capitalists-part-1',
  81. 'md5': 'e9d77741f9e42ba583e683cd170660f7',
  82. 'info_dict': {
  83. 'id': '43cW1mYzpia9IlestBjVpd23Yu3afAfp',
  84. 'ext': 'flv',
  85. 'title': 'VICE_COWBOYCAPITALISTS_PART01_v1_VICE_WM_1080p.mov',
  86. 'duration': 725.983,
  87. },
  88. 'add_ie': ['Ooyala'],
  89. }, {
  90. 'url': 'http://www.vice.com/video/how-to-hack-a-car',
  91. 'md5': 'a7ecf64ee4fa19b916c16f4b56184ae2',
  92. 'info_dict': {
  93. 'id': '3jstaBeXgAs',
  94. 'ext': 'mp4',
  95. 'title': 'How to Hack a Car: Phreaked Out (Episode 2)',
  96. 'description': 'md5:ee95453f7ff495db8efe14ae8bf56f30',
  97. 'uploader_id': 'MotherboardTV',
  98. 'uploader': 'Motherboard',
  99. 'upload_date': '20140529',
  100. },
  101. 'add_ie': ['Youtube'],
  102. }, {
  103. 'url': 'https://video.vice.com/en_us/video/the-signal-from-tolva/5816510690b70e6c5fd39a56',
  104. 'md5': '',
  105. 'info_dict': {
  106. 'id': '5816510690b70e6c5fd39a56',
  107. 'ext': 'mp4',
  108. 'uploader': 'Waypoint',
  109. 'title': 'The Signal From Tölva',
  110. 'uploader_id': '57f7d621e05ca860fa9ccaf9',
  111. 'timestamp': 1477941983938,
  112. },
  113. 'params': {
  114. # m3u8 download
  115. 'skip_download': True,
  116. },
  117. 'add_ie': ['UplynkPreplay'],
  118. }, {
  119. 'url': 'https://news.vice.com/video/experimenting-on-animals-inside-the-monkey-lab',
  120. 'only_matching': True,
  121. }, {
  122. 'url': 'http://www.vice.com/ru/video/big-night-out-ibiza-clive-martin-229',
  123. 'only_matching': True,
  124. }, {
  125. 'url': 'https://munchies.vice.com/en/videos/watch-the-trailer-for-our-new-series-the-pizza-show',
  126. 'only_matching': True,
  127. }]
  128. _PREPLAY_HOST = 'video.vice'
  129. def _real_extract(self, url):
  130. video_id = self._match_id(url)
  131. webpage, urlh = self._download_webpage_handle(url, video_id)
  132. embed_code = self._search_regex(
  133. r'embedCode=([^&\'"]+)', webpage,
  134. 'ooyala embed code', default=None)
  135. if embed_code:
  136. return self.url_result('ooyala:%s' % embed_code, 'Ooyala')
  137. youtube_id = self._search_regex(
  138. r'data-youtube-id="([^"]+)"', webpage, 'youtube id', default=None)
  139. if youtube_id:
  140. return self.url_result(youtube_id, 'Youtube')
  141. return self._extract_preplay_video(urlh.geturl(), webpage)
  142. class ViceShowIE(InfoExtractor):
  143. _VALID_URL = r'https?://(?:.+?\.)?vice\.com/(?:[^/]+/)?show/(?P<id>[^/?#&]+)'
  144. _TEST = {
  145. 'url': 'https://munchies.vice.com/en/show/fuck-thats-delicious-2',
  146. 'info_dict': {
  147. 'id': 'fuck-thats-delicious-2',
  148. 'title': "Fuck, That's Delicious",
  149. 'description': 'Follow the culinary adventures of rapper Action Bronson during his ongoing world tour.',
  150. },
  151. 'playlist_count': 17,
  152. }
  153. def _real_extract(self, url):
  154. show_id = self._match_id(url)
  155. webpage = self._download_webpage(url, show_id)
  156. entries = [
  157. self.url_result(video_url, ViceIE.ie_key())
  158. for video_url, _ in re.findall(
  159. r'<h2[^>]+class="article-title"[^>]+data-id="\d+"[^>]*>\s*<a[^>]+href="(%s.*?)"'
  160. % ViceIE._VALID_URL, webpage)]
  161. title = self._search_regex(
  162. r'<title>(.+?)</title>', webpage, 'title', default=None)
  163. if title:
  164. title = re.sub(r'(.+)\s*\|\s*.+$', r'\1', title).strip()
  165. description = self._html_search_meta('description', webpage, 'description')
  166. return self.playlist_result(entries, show_id, title, description)