You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
10 KiB

  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(
  31. url, video_id, 'VICELAND', resource)
  32. # signature generation algorithm is reverse engineered from signatureGenerator in
  33. # webpack:///../shared/~/vice-player/dist/js/vice-player.js in
  34. # https://www.viceland.com/assets/common/js/web.vendor.bundle.js
  35. exp = int(time.time()) + 14400
  36. query.update({
  37. 'exp': exp,
  38. 'sign': hashlib.sha512(('%s:GET:%d' % (video_id, exp)).encode()).hexdigest(),
  39. })
  40. try:
  41. host = 'www.viceland' if is_locked else self._PREPLAY_HOST
  42. preplay = self._download_json(
  43. 'https://%s.com/%s/preplay/%s' % (host, locale, video_id),
  44. video_id, query=query)
  45. except ExtractorError as e:
  46. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
  47. error = json.loads(e.cause.read().decode())
  48. raise ExtractorError('%s said: %s' % (
  49. self.IE_NAME, error['details']), expected=True)
  50. raise
  51. video_data = preplay['video']
  52. base = video_data['base']
  53. uplynk_preplay_url = preplay['preplayURL']
  54. episode = video_data.get('episode', {})
  55. channel = video_data.get('channel', {})
  56. subtitles = {}
  57. cc_url = preplay.get('ccURL')
  58. if cc_url:
  59. subtitles['en'] = [{
  60. 'url': cc_url,
  61. }]
  62. return {
  63. '_type': 'url_transparent',
  64. 'url': uplynk_preplay_url,
  65. 'id': video_id,
  66. 'title': title,
  67. 'description': base.get('body') or base.get('display_body'),
  68. 'thumbnail': watch_hub_data.get('cover-image') or watch_hub_data.get('thumbnail'),
  69. 'duration': int_or_none(video_data.get('video_duration')) or parse_duration(watch_hub_data.get('video-duration')),
  70. 'timestamp': int_or_none(video_data.get('created_at'), 1000),
  71. 'age_limit': parse_age_limit(video_data.get('video_rating')),
  72. 'series': video_data.get('show_title') or watch_hub_data.get('show-title'),
  73. 'episode_number': int_or_none(episode.get('episode_number') or watch_hub_data.get('episode')),
  74. 'episode_id': str_or_none(episode.get('id') or video_data.get('episode_id')),
  75. 'season_number': int_or_none(watch_hub_data.get('season')),
  76. 'season_id': str_or_none(episode.get('season_id')),
  77. 'uploader': channel.get('base', {}).get('title') or watch_hub_data.get('channel-title'),
  78. 'uploader_id': str_or_none(channel.get('id')),
  79. 'subtitles': subtitles,
  80. 'ie_key': 'UplynkPreplay',
  81. }
  82. class ViceIE(ViceBaseIE):
  83. IE_NAME = 'vice'
  84. _VALID_URL = r'https?://(?:.+?\.)?vice\.com/(?:(?P<locale>[^/]+)/)?videos?/(?P<id>[^/?#&]+)'
  85. _TESTS = [{
  86. 'url': 'https://news.vice.com/video/experimenting-on-animals-inside-the-monkey-lab',
  87. 'md5': '7d3ae2f9ba5f196cdd9f9efd43657ac2',
  88. 'info_dict': {
  89. 'id': 'N2bzkydjraWDGwnt8jAttCF6Y0PDv4Zj',
  90. 'ext': 'flv',
  91. 'title': 'Monkey Labs of Holland',
  92. 'description': 'md5:92b3c7dcbfe477f772dd4afa496c9149',
  93. },
  94. 'add_ie': ['Ooyala'],
  95. }, {
  96. 'url': 'https://video.vice.com/en_us/video/the-signal-from-tolva/5816510690b70e6c5fd39a56',
  97. 'info_dict': {
  98. 'id': '5816510690b70e6c5fd39a56',
  99. 'ext': 'mp4',
  100. 'uploader': 'Waypoint',
  101. 'title': 'The Signal From Tölva',
  102. 'description': 'md5:3927e3c79f9e8094606a2b3c5b5e55d5',
  103. 'uploader_id': '57f7d621e05ca860fa9ccaf9',
  104. 'timestamp': 1477941983,
  105. 'upload_date': '20161031',
  106. },
  107. 'params': {
  108. # m3u8 download
  109. 'skip_download': True,
  110. },
  111. 'add_ie': ['UplynkPreplay'],
  112. }, {
  113. 'url': 'https://video.vice.com/alps/video/ulfs-wien-beruchtigste-grafitti-crew-part-1/581b12b60a0e1f4c0fb6ea2f',
  114. 'info_dict': {
  115. 'id': '581b12b60a0e1f4c0fb6ea2f',
  116. 'ext': 'mp4',
  117. 'title': 'ULFs - Wien berüchtigste Grafitti Crew - Part 1',
  118. '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>',
  119. 'uploader': 'VICE',
  120. 'uploader_id': '57a204088cb727dec794c67b',
  121. 'timestamp': 1485368119,
  122. 'upload_date': '20170125',
  123. 'age_limit': 14,
  124. },
  125. 'params': {
  126. # AES-encrypted m3u8
  127. 'skip_download': True,
  128. },
  129. 'add_ie': ['UplynkPreplay'],
  130. }, {
  131. 'url': 'https://video.vice.com/en_us/video/pizza-show-trailer/56d8c9a54d286ed92f7f30e4',
  132. 'only_matching': True,
  133. }]
  134. _PREPLAY_HOST = 'video.vice'
  135. def _real_extract(self, url):
  136. locale, video_id = re.match(self._VALID_URL, url).groups()
  137. webpage, urlh = self._download_webpage_handle(url, video_id)
  138. embed_code = self._search_regex(
  139. r'embedCode=([^&\'"]+)', webpage,
  140. 'ooyala embed code', default=None)
  141. if embed_code:
  142. return self.url_result('ooyala:%s' % embed_code, 'Ooyala')
  143. youtube_id = self._search_regex(
  144. r'data-youtube-id="([^"]+)"', webpage, 'youtube id', default=None)
  145. if youtube_id:
  146. return self.url_result(youtube_id, 'Youtube')
  147. return self._extract_preplay_video(urlh.geturl(), locale, webpage)
  148. class ViceShowIE(InfoExtractor):
  149. IE_NAME = 'vice:show'
  150. _VALID_URL = r'https?://(?:.+?\.)?vice\.com/(?:[^/]+/)?show/(?P<id>[^/?#&]+)'
  151. _TEST = {
  152. 'url': 'https://munchies.vice.com/en/show/fuck-thats-delicious-2',
  153. 'info_dict': {
  154. 'id': 'fuck-thats-delicious-2',
  155. 'title': "Fuck, That's Delicious",
  156. 'description': 'Follow the culinary adventures of rapper Action Bronson during his ongoing world tour.',
  157. },
  158. 'playlist_count': 17,
  159. }
  160. def _real_extract(self, url):
  161. show_id = self._match_id(url)
  162. webpage = self._download_webpage(url, show_id)
  163. entries = [
  164. self.url_result(video_url, ViceIE.ie_key())
  165. for video_url, _ in re.findall(
  166. r'<h2[^>]+class="article-title"[^>]+data-id="\d+"[^>]*>\s*<a[^>]+href="(%s.*?)"'
  167. % ViceIE._VALID_URL, webpage)]
  168. title = self._search_regex(
  169. r'<title>(.+?)</title>', webpage, 'title', default=None)
  170. if title:
  171. title = re.sub(r'(.+)\s*\|\s*.+$', r'\1', title).strip()
  172. description = self._html_search_meta(
  173. 'description', webpage, 'description')
  174. return self.playlist_result(entries, show_id, title, description)
  175. class ViceArticleIE(InfoExtractor):
  176. IE_NAME = 'vice:article'
  177. _VALID_URL = r'https://www.vice.com/[^/]+/article/(?P<id>[^?#]+)'
  178. _TESTS = [{
  179. 'url': 'https://www.vice.com/en_us/article/on-set-with-the-woman-making-mormon-porn-in-utah',
  180. 'info_dict': {
  181. 'id': '58dc0a3dee202d2a0ccfcbd8',
  182. 'ext': 'mp4',
  183. 'title': 'Mormon War on Porn ',
  184. 'description': 'md5:ad396a2481e7f8afb5ed486878421090',
  185. 'uploader': 'VICE',
  186. 'uploader_id': '57a204088cb727dec794c693',
  187. 'timestamp': 1489160690,
  188. 'upload_date': '20170310',
  189. },
  190. 'params': {
  191. # AES-encrypted m3u8
  192. 'skip_download': True,
  193. },
  194. 'add_ie': ['UplynkPreplay'],
  195. }, {
  196. 'url': 'https://www.vice.com/en_us/article/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. 'url': 'https://www.vice.com/en_us/article/cowboy-capitalists-part-1',
  210. 'only_matching': True,
  211. }, {
  212. 'url': 'https://www.vice.com/ru/article/big-night-out-ibiza-clive-martin-229',
  213. 'only_matching': True,
  214. }]
  215. def _real_extract(self, url):
  216. display_id = self._match_id(url)
  217. webpage = self._download_webpage(url, display_id)
  218. prefetch_data = self._parse_json(self._search_regex(
  219. r'window\.__PREFETCH_DATA\s*=\s*({.*});',
  220. webpage, 'prefetch data'), display_id)
  221. body = prefetch_data['body']
  222. def _url_res(video_url, ie_key):
  223. return {
  224. '_type': 'url_transparent',
  225. 'url': video_url,
  226. 'display_id': display_id,
  227. 'ie_key': ie_key,
  228. }
  229. embed_code = self._search_regex(
  230. r'embedCode=([^&\'"]+)', body,
  231. 'ooyala embed code', default=None)
  232. if embed_code:
  233. return _url_res('ooyala:%s' % embed_code, 'Ooyala')
  234. youtube_url = self._html_search_regex(
  235. r'<iframe[^>]+src="(.*youtube\.com/.*)"',
  236. body, 'YouTube URL', default=None)
  237. if youtube_url:
  238. return _url_res(youtube_url, 'Youtube')
  239. video_url = self._html_search_regex(
  240. r'data-video-url="([^"]+)"',
  241. prefetch_data['embed_code'], 'video URL')
  242. return _url_res(video_url, ViceIE.ie_key())