vice.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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': 'https://video.vice.com/en_us/video/the-signal-from-tolva/5816510690b70e6c5fd39a56',
  92. 'info_dict': {
  93. 'id': '5816510690b70e6c5fd39a56',
  94. 'ext': 'mp4',
  95. 'uploader': 'Waypoint',
  96. 'title': 'The Signal From Tölva',
  97. 'description': 'md5:3927e3c79f9e8094606a2b3c5b5e55d5',
  98. 'uploader_id': '57f7d621e05ca860fa9ccaf9',
  99. 'timestamp': 1477941983,
  100. 'upload_date': '20161031',
  101. },
  102. 'params': {
  103. # m3u8 download
  104. 'skip_download': True,
  105. },
  106. 'add_ie': ['UplynkPreplay'],
  107. }, {
  108. 'url': 'https://video.vice.com/alps/video/ulfs-wien-beruchtigste-grafitti-crew-part-1/581b12b60a0e1f4c0fb6ea2f',
  109. 'info_dict': {
  110. 'id': '581b12b60a0e1f4c0fb6ea2f',
  111. 'ext': 'mp4',
  112. 'title': 'ULFs - Wien berüchtigste Grafitti Crew - Part 1',
  113. '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>',
  114. 'uploader': 'VICE',
  115. 'uploader_id': '57a204088cb727dec794c67b',
  116. 'timestamp': 1485368119,
  117. 'upload_date': '20170125',
  118. 'age_limit': 14,
  119. },
  120. 'params': {
  121. # AES-encrypted m3u8
  122. 'skip_download': True,
  123. },
  124. 'add_ie': ['UplynkPreplay'],
  125. }, {
  126. 'url': 'https://news.vice.com/video/experimenting-on-animals-inside-the-monkey-lab',
  127. 'only_matching': True,
  128. }, {
  129. 'url': 'http://www.vice.com/ru/video/big-night-out-ibiza-clive-martin-229',
  130. 'only_matching': True,
  131. }, {
  132. 'url': 'https://munchies.vice.com/en/videos/watch-the-trailer-for-our-new-series-the-pizza-show',
  133. 'only_matching': True,
  134. }]
  135. _PREPLAY_HOST = 'video.vice'
  136. def _real_extract(self, url):
  137. mobj = re.match(self._VALID_URL, url)
  138. video_id = mobj.group('id')
  139. locale = mobj.group('locale')
  140. video_id = self._match_id(url)
  141. webpage, urlh = self._download_webpage_handle(url, video_id)
  142. embed_code = self._search_regex(
  143. r'embedCode=([^&\'"]+)', webpage,
  144. 'ooyala embed code', default=None)
  145. if embed_code:
  146. return self.url_result('ooyala:%s' % embed_code, 'Ooyala')
  147. youtube_id = self._search_regex(
  148. r'data-youtube-id="([^"]+)"', webpage, 'youtube id', default=None)
  149. if youtube_id:
  150. return self.url_result(youtube_id, 'Youtube')
  151. return self._extract_preplay_video(urlh.geturl(), locale, webpage)
  152. class ViceShowIE(InfoExtractor):
  153. _VALID_URL = r'https?://(?:.+?\.)?vice\.com/(?:[^/]+/)?show/(?P<id>[^/?#&]+)'
  154. _TEST = {
  155. 'url': 'https://munchies.vice.com/en/show/fuck-thats-delicious-2',
  156. 'info_dict': {
  157. 'id': 'fuck-thats-delicious-2',
  158. 'title': "Fuck, That's Delicious",
  159. 'description': 'Follow the culinary adventures of rapper Action Bronson during his ongoing world tour.',
  160. },
  161. 'playlist_count': 17,
  162. }
  163. def _real_extract(self, url):
  164. show_id = self._match_id(url)
  165. webpage = self._download_webpage(url, show_id)
  166. entries = [
  167. self.url_result(video_url, ViceIE.ie_key())
  168. for video_url, _ in re.findall(
  169. r'<h2[^>]+class="article-title"[^>]+data-id="\d+"[^>]*>\s*<a[^>]+href="(%s.*?)"'
  170. % ViceIE._VALID_URL, webpage)]
  171. title = self._search_regex(
  172. r'<title>(.+?)</title>', webpage, 'title', default=None)
  173. if title:
  174. title = re.sub(r'(.+)\s*\|\s*.+$', r'\1', title).strip()
  175. description = self._html_search_meta('description', webpage, 'description')
  176. return self.playlist_result(entries, show_id, title, description)
  177. class ViceArticleIE(InfoExtractor):
  178. _VALID_URL = r'https://www.vice.com/[^/]+/article/(?P<id>[^?#]+)'
  179. _TESTS = [{
  180. 'url': 'https://www.vice.com/en_us/article/on-set-with-the-woman-making-mormon-porn-in-utah',
  181. 'info_dict': {
  182. 'id': '58dc0a3dee202d2a0ccfcbd8',
  183. 'ext': 'mp4',
  184. 'title': 'Mormon War on Porn ',
  185. 'description': 'md5:ad396a2481e7f8afb5ed486878421090',
  186. 'uploader': 'VICE',
  187. 'uploader_id': '57a204088cb727dec794c693',
  188. 'timestamp': 1489160690,
  189. 'upload_date': '20170310',
  190. },
  191. 'params': {
  192. # AES-encrypted m3u8
  193. 'skip_download': True,
  194. },
  195. }, {
  196. 'url': 'http://www.vice.com/video/how-to-hack-a-car',
  197. 'md5': 'a7ecf64ee4fa19b916c16f4b56184ae2',
  198. 'info_dict': {
  199. 'id': '3jstaBeXgAs',
  200. 'ext': 'mp4',
  201. 'title': 'How to Hack a Car: Phreaked Out (Episode 2)',
  202. 'description': 'md5:ee95453f7ff495db8efe14ae8bf56f30',
  203. 'uploader_id': 'MotherboardTV',
  204. 'uploader': 'Motherboard',
  205. 'upload_date': '20140529',
  206. },
  207. 'add_ie': ['Youtube'],
  208. }]
  209. def _real_extract(self, url):
  210. display_id = self._match_id(url)
  211. webpage = self._download_webpage(url, display_id)
  212. prefetch_data = self._parse_json(self._search_regex(
  213. r'window\.__PREFETCH_DATA\s*=\s*({.*});',
  214. webpage, 'prefetch data'), display_id)
  215. body = prefetch_data['body']
  216. youtube_url = self._html_search_regex(
  217. r'<iframe[^>]+src="(.*youtube\.com/.*)"', body, 'YouTube URL', default=None)
  218. if youtube_url:
  219. return {
  220. '_type': 'url_transparent',
  221. 'url': youtube_url,
  222. 'display_id': display_id,
  223. 'ie_key': 'Youtube',
  224. }
  225. video_url = self._html_search_regex(
  226. r'data-video-url="([^"]+)"', prefetch_data['embed_code'], 'video URL')
  227. return {
  228. '_type': 'url_transparent',
  229. 'url': video_url,
  230. 'display_id': display_id,
  231. 'ie_key': ViceIE.ie_key(),
  232. }