vice.py 8.3 KB

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