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.

232 lines
8.7 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urllib_parse_urlparse,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. determine_ext,
  11. extract_attributes,
  12. int_or_none,
  13. js_to_json,
  14. mimetype2ext,
  15. orderedSet,
  16. parse_iso8601,
  17. )
  18. class CondeNastIE(InfoExtractor):
  19. """
  20. Condé Nast is a media group, some of its sites use a custom HTML5 player
  21. that works the same in all of them.
  22. """
  23. # The keys are the supported sites and the values are the name to be shown
  24. # to the user and in the extractor description.
  25. _SITES = {
  26. 'allure': 'Allure',
  27. 'architecturaldigest': 'Architectural Digest',
  28. 'arstechnica': 'Ars Technica',
  29. 'bonappetit': 'Bon Appétit',
  30. 'brides': 'Brides',
  31. 'cnevids': 'Condé Nast',
  32. 'cntraveler': 'Condé Nast Traveler',
  33. 'details': 'Details',
  34. 'epicurious': 'Epicurious',
  35. 'glamour': 'Glamour',
  36. 'golfdigest': 'Golf Digest',
  37. 'gq': 'GQ',
  38. 'newyorker': 'The New Yorker',
  39. 'self': 'SELF',
  40. 'teenvogue': 'Teen Vogue',
  41. 'vanityfair': 'Vanity Fair',
  42. 'vogue': 'Vogue',
  43. 'wired': 'WIRED',
  44. 'wmagazine': 'W Magazine',
  45. }
  46. _VALID_URL = r'''(?x)https?://(?:video|www|player(?:-backend)?)\.(?:%s)\.com/
  47. (?:
  48. (?:
  49. embed(?:js)?|
  50. (?:script|inline)/video
  51. )/(?P<id>[0-9a-f]{24})(?:/(?P<player_id>[0-9a-f]{24}))?(?:.+?\btarget=(?P<target>[^&]+))?|
  52. (?P<type>watch|series|video)/(?P<display_id>[^/?#]+)
  53. )''' % '|'.join(_SITES.keys())
  54. IE_DESC = 'Condé Nast media group: %s' % ', '.join(sorted(_SITES.values()))
  55. EMBED_URL = r'(?:https?:)?//player(?:-backend)?\.(?:%s)\.com/(?:embed(?:js)?|(?:script|inline)/video)/.+?' % '|'.join(_SITES.keys())
  56. _TESTS = [{
  57. 'url': 'http://video.wired.com/watch/3d-printed-speakers-lit-with-led',
  58. 'md5': '1921f713ed48aabd715691f774c451f7',
  59. 'info_dict': {
  60. 'id': '5171b343c2b4c00dd0c1ccb3',
  61. 'ext': 'mp4',
  62. 'title': '3D Printed Speakers Lit With LED',
  63. 'description': 'Check out these beautiful 3D printed LED speakers. You can\'t actually buy them, but LumiGeek is working on a board that will let you make you\'re own.',
  64. 'uploader': 'wired',
  65. 'upload_date': '20130314',
  66. 'timestamp': 1363219200,
  67. }
  68. }, {
  69. 'url': 'http://video.gq.com/watch/the-closer-with-keith-olbermann-the-only-true-surprise-trump-s-an-idiot?c=series',
  70. 'info_dict': {
  71. 'id': '58d1865bfd2e6126e2000015',
  72. 'ext': 'mp4',
  73. 'title': 'The Only True Surprise? Trump’s an Idiot',
  74. 'uploader': 'gq',
  75. 'upload_date': '20170321',
  76. 'timestamp': 1490126427,
  77. },
  78. }, {
  79. # JS embed
  80. 'url': 'http://player.cnevids.com/embedjs/55f9cf8b61646d1acf00000c/5511d76261646d5566020000.js',
  81. 'md5': 'f1a6f9cafb7083bab74a710f65d08999',
  82. 'info_dict': {
  83. 'id': '55f9cf8b61646d1acf00000c',
  84. 'ext': 'mp4',
  85. 'title': '3D printed TSA Travel Sentry keys really do open TSA locks',
  86. 'uploader': 'arstechnica',
  87. 'upload_date': '20150916',
  88. 'timestamp': 1442434955,
  89. }
  90. }, {
  91. 'url': 'https://player.cnevids.com/inline/video/59138decb57ac36b83000005.js?target=js-cne-player',
  92. 'only_matching': True,
  93. }, {
  94. 'url': 'http://player-backend.cnevids.com/script/video/59138decb57ac36b83000005.js',
  95. 'only_matching': True,
  96. }]
  97. def _extract_series(self, url, webpage):
  98. title = self._html_search_regex(
  99. r'(?s)<div class="cne-series-info">.*?<h1>(.+?)</h1>',
  100. webpage, 'series title')
  101. url_object = compat_urllib_parse_urlparse(url)
  102. base_url = '%s://%s' % (url_object.scheme, url_object.netloc)
  103. m_paths = re.finditer(
  104. r'(?s)<p class="cne-thumb-title">.*?<a href="(/watch/.+?)["\?]', webpage)
  105. paths = orderedSet(m.group(1) for m in m_paths)
  106. build_url = lambda path: compat_urlparse.urljoin(base_url, path)
  107. entries = [self.url_result(build_url(path), 'CondeNast') for path in paths]
  108. return self.playlist_result(entries, playlist_title=title)
  109. def _extract_video_params(self, webpage, display_id):
  110. query = self._parse_json(
  111. self._search_regex(
  112. r'(?s)var\s+params\s*=\s*({.+?})[;,]', webpage, 'player params',
  113. default='{}'),
  114. display_id, transform_source=js_to_json, fatal=False)
  115. if query:
  116. query['videoId'] = self._search_regex(
  117. r'(?:data-video-id=|currentVideoId\s*=\s*)["\']([\da-f]+)',
  118. webpage, 'video id', default=None)
  119. else:
  120. params = extract_attributes(self._search_regex(
  121. r'(<[^>]+data-js="video-player"[^>]+>)',
  122. webpage, 'player params element'))
  123. query.update({
  124. 'videoId': params['data-video'],
  125. 'playerId': params['data-player'],
  126. 'target': params['id'],
  127. })
  128. return query
  129. def _extract_video(self, params):
  130. video_id = params['videoId']
  131. video_info = None
  132. # New API path
  133. query = params.copy()
  134. query['embedType'] = 'inline'
  135. info_page = self._download_json(
  136. 'http://player.cnevids.com/embed-api.json', video_id,
  137. 'Downloading embed info', fatal=False, query=query)
  138. # Old fallbacks
  139. if not info_page:
  140. if params.get('playerId'):
  141. info_page = self._download_json(
  142. 'http://player.cnevids.com/player/video.js', video_id,
  143. 'Downloading video info', fatal=False, query=params)
  144. if info_page:
  145. video_info = info_page.get('video')
  146. if not video_info:
  147. info_page = self._download_webpage(
  148. 'http://player.cnevids.com/player/loader.js',
  149. video_id, 'Downloading loader info', query=params)
  150. if not video_info:
  151. info_page = self._download_webpage(
  152. 'https://player.cnevids.com/inline/video/%s.js' % video_id,
  153. video_id, 'Downloading inline info', query={
  154. 'target': params.get('target', 'embedplayer')
  155. })
  156. if not video_info:
  157. video_info = self._parse_json(
  158. self._search_regex(
  159. r'(?s)var\s+config\s*=\s*({.+?});', info_page, 'config'),
  160. video_id, transform_source=js_to_json)['video']
  161. title = video_info['title']
  162. formats = []
  163. for fdata in video_info['sources']:
  164. src = fdata.get('src')
  165. if not src:
  166. continue
  167. ext = mimetype2ext(fdata.get('type')) or determine_ext(src)
  168. if ext == 'm3u8':
  169. formats.extend(self._extract_m3u8_formats(
  170. src, video_id, 'mp4', entry_protocol='m3u8_native',
  171. m3u8_id='hls', fatal=False))
  172. continue
  173. quality = fdata.get('quality')
  174. formats.append({
  175. 'format_id': ext + ('-%s' % quality if quality else ''),
  176. 'url': src,
  177. 'ext': ext,
  178. 'quality': 1 if quality == 'high' else 0,
  179. })
  180. self._sort_formats(formats)
  181. return {
  182. 'id': video_id,
  183. 'formats': formats,
  184. 'title': title,
  185. 'thumbnail': video_info.get('poster_frame'),
  186. 'uploader': video_info.get('brand'),
  187. 'duration': int_or_none(video_info.get('duration')),
  188. 'tags': video_info.get('tags'),
  189. 'series': video_info.get('series_title'),
  190. 'season': video_info.get('season_title'),
  191. 'timestamp': parse_iso8601(video_info.get('premiere_date')),
  192. 'categories': video_info.get('categories'),
  193. }
  194. def _real_extract(self, url):
  195. video_id, player_id, target, url_type, display_id = re.match(self._VALID_URL, url).groups()
  196. if video_id:
  197. return self._extract_video({
  198. 'videoId': video_id,
  199. 'playerId': player_id,
  200. 'target': target,
  201. })
  202. webpage = self._download_webpage(url, display_id)
  203. if url_type == 'series':
  204. return self._extract_series(url, webpage)
  205. else:
  206. params = self._extract_video_params(webpage, display_id)
  207. info = self._search_json_ld(
  208. webpage, display_id, fatal=False)
  209. info.update(self._extract_video(params))
  210. return info