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.

148 lines
6.1 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_urlencode,
  7. compat_urllib_parse_urlparse,
  8. compat_urlparse,
  9. )
  10. from ..utils import (
  11. orderedSet,
  12. remove_end,
  13. )
  14. class CondeNastIE(InfoExtractor):
  15. """
  16. Condé Nast is a media group, some of its sites use a custom HTML5 player
  17. that works the same in all of them.
  18. """
  19. # The keys are the supported sites and the values are the name to be shown
  20. # to the user and in the extractor description.
  21. _SITES = {
  22. 'allure': 'Allure',
  23. 'architecturaldigest': 'Architectural Digest',
  24. 'arstechnica': 'Ars Technica',
  25. 'bonappetit': 'Bon Appétit',
  26. 'brides': 'Brides',
  27. 'cnevids': 'Condé Nast',
  28. 'cntraveler': 'Condé Nast Traveler',
  29. 'details': 'Details',
  30. 'epicurious': 'Epicurious',
  31. 'glamour': 'Glamour',
  32. 'golfdigest': 'Golf Digest',
  33. 'gq': 'GQ',
  34. 'newyorker': 'The New Yorker',
  35. 'self': 'SELF',
  36. 'teenvogue': 'Teen Vogue',
  37. 'vanityfair': 'Vanity Fair',
  38. 'vogue': 'Vogue',
  39. 'wired': 'WIRED',
  40. 'wmagazine': 'W Magazine',
  41. }
  42. _VALID_URL = r'https?://(?:video|www|player)\.(?P<site>%s)\.com/(?P<type>watch|series|video|embed(?:js)?)/(?P<id>[^/?#]+)' % '|'.join(_SITES.keys())
  43. IE_DESC = 'Condé Nast media group: %s' % ', '.join(sorted(_SITES.values()))
  44. EMBED_URL = r'(?:https?:)?//player\.(?P<site>%s)\.com/(?P<type>embed(?:js)?)/.+?' % '|'.join(_SITES.keys())
  45. _TESTS = [{
  46. 'url': 'http://video.wired.com/watch/3d-printed-speakers-lit-with-led',
  47. 'md5': '1921f713ed48aabd715691f774c451f7',
  48. 'info_dict': {
  49. 'id': '5171b343c2b4c00dd0c1ccb3',
  50. 'ext': 'mp4',
  51. 'title': '3D Printed Speakers Lit With LED',
  52. '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.',
  53. }
  54. }, {
  55. # JS embed
  56. 'url': 'http://player.cnevids.com/embedjs/55f9cf8b61646d1acf00000c/5511d76261646d5566020000.js',
  57. 'md5': 'f1a6f9cafb7083bab74a710f65d08999',
  58. 'info_dict': {
  59. 'id': '55f9cf8b61646d1acf00000c',
  60. 'ext': 'mp4',
  61. 'title': '3D printed TSA Travel Sentry keys really do open TSA locks',
  62. }
  63. }]
  64. def _extract_series(self, url, webpage):
  65. title = self._html_search_regex(r'<div class="cne-series-info">.*?<h1>(.+?)</h1>',
  66. webpage, 'series title', flags=re.DOTALL)
  67. url_object = compat_urllib_parse_urlparse(url)
  68. base_url = '%s://%s' % (url_object.scheme, url_object.netloc)
  69. m_paths = re.finditer(r'<p class="cne-thumb-title">.*?<a href="(/watch/.+?)["\?]',
  70. webpage, flags=re.DOTALL)
  71. paths = orderedSet(m.group(1) for m in m_paths)
  72. build_url = lambda path: compat_urlparse.urljoin(base_url, path)
  73. entries = [self.url_result(build_url(path), 'CondeNast') for path in paths]
  74. return self.playlist_result(entries, playlist_title=title)
  75. def _extract_video(self, webpage, url_type):
  76. if url_type != 'embed':
  77. description = self._html_search_regex(
  78. [
  79. r'<div class="cne-video-description">(.+?)</div>',
  80. r'<div class="video-post-content">(.+?)</div>',
  81. ],
  82. webpage, 'description', fatal=False, flags=re.DOTALL)
  83. else:
  84. description = None
  85. params = self._search_regex(r'var params = {(.+?)}[;,]', webpage,
  86. 'player params', flags=re.DOTALL)
  87. video_id = self._search_regex(r'videoId: [\'"](.+?)[\'"]', params, 'video id')
  88. player_id = self._search_regex(r'playerId: [\'"](.+?)[\'"]', params, 'player id')
  89. target = self._search_regex(r'target: [\'"](.+?)[\'"]', params, 'target')
  90. data = compat_urllib_parse_urlencode({'videoId': video_id,
  91. 'playerId': player_id,
  92. 'target': target,
  93. })
  94. base_info_url = self._search_regex(r'url = [\'"](.+?)[\'"][,;]',
  95. webpage, 'base info url',
  96. default='http://player.cnevids.com/player/loader.js?')
  97. info_url = base_info_url + data
  98. info_page = self._download_webpage(info_url, video_id,
  99. 'Downloading video info')
  100. video_info = self._search_regex(r'var\s+video\s*=\s*({.+?});', info_page, 'video info')
  101. video_info = self._parse_json(video_info, video_id)
  102. formats = [{
  103. 'format_id': '%s-%s' % (fdata['type'].split('/')[-1], fdata['quality']),
  104. 'url': fdata['src'],
  105. 'ext': fdata['type'].split('/')[-1],
  106. 'quality': 1 if fdata['quality'] == 'high' else 0,
  107. } for fdata in video_info['sources'][0]]
  108. self._sort_formats(formats)
  109. return {
  110. 'id': video_id,
  111. 'formats': formats,
  112. 'title': video_info['title'],
  113. 'thumbnail': video_info['poster_frame'],
  114. 'description': description,
  115. }
  116. def _real_extract(self, url):
  117. mobj = re.match(self._VALID_URL, url)
  118. site = mobj.group('site')
  119. url_type = mobj.group('type')
  120. item_id = mobj.group('id')
  121. # Convert JS embed to regular embed
  122. if url_type == 'embedjs':
  123. parsed_url = compat_urlparse.urlparse(url)
  124. url = compat_urlparse.urlunparse(parsed_url._replace(
  125. path=remove_end(parsed_url.path, '.js').replace('/embedjs/', '/embed/')))
  126. url_type = 'embed'
  127. self.to_screen('Extracting from %s with the Condé Nast extractor' % self._SITES[site])
  128. webpage = self._download_webpage(url, item_id)
  129. if url_type == 'series':
  130. return self._extract_series(url, webpage)
  131. else:
  132. return self._extract_video(webpage, url_type)