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.

116 lines
4.8 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. compat_urllib_parse,
  8. orderedSet,
  9. compat_urllib_parse_urlparse,
  10. compat_urlparse,
  11. )
  12. class CondeNastIE(InfoExtractor):
  13. """
  14. Condé Nast is a media group, some of its sites use a custom HTML5 player
  15. that works the same in all of them.
  16. """
  17. # The keys are the supported sites and the values are the name to be shown
  18. # to the user and in the extractor description.
  19. _SITES = {
  20. 'wired': 'WIRED',
  21. 'gq': 'GQ',
  22. 'vogue': 'Vogue',
  23. 'glamour': 'Glamour',
  24. 'wmagazine': 'W Magazine',
  25. 'vanityfair': 'Vanity Fair',
  26. 'cnevids': 'Condé Nast',
  27. }
  28. _VALID_URL = r'http://(video|www|player)\.(?P<site>%s)\.com/(?P<type>watch|series|video|embed)/(?P<id>[^/?#]+)' % '|'.join(_SITES.keys())
  29. IE_DESC = 'Condé Nast media group: %s' % ', '.join(sorted(_SITES.values()))
  30. _TEST = {
  31. 'url': 'http://video.wired.com/watch/3d-printed-speakers-lit-with-led',
  32. 'md5': '1921f713ed48aabd715691f774c451f7',
  33. 'info_dict': {
  34. 'id': '5171b343c2b4c00dd0c1ccb3',
  35. 'ext': 'mp4',
  36. 'title': '3D Printed Speakers Lit With LED',
  37. '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.',
  38. }
  39. }
  40. def _extract_series(self, url, webpage):
  41. title = self._html_search_regex(r'<div class="cne-series-info">.*?<h1>(.+?)</h1>',
  42. webpage, 'series title', flags=re.DOTALL)
  43. url_object = compat_urllib_parse_urlparse(url)
  44. base_url = '%s://%s' % (url_object.scheme, url_object.netloc)
  45. m_paths = re.finditer(r'<p class="cne-thumb-title">.*?<a href="(/watch/.+?)["\?]',
  46. webpage, flags=re.DOTALL)
  47. paths = orderedSet(m.group(1) for m in m_paths)
  48. build_url = lambda path: compat_urlparse.urljoin(base_url, path)
  49. entries = [self.url_result(build_url(path), 'CondeNast') for path in paths]
  50. return self.playlist_result(entries, playlist_title=title)
  51. def _extract_video(self, webpage, url_type):
  52. if url_type != 'embed':
  53. description = self._html_search_regex(
  54. [
  55. r'<div class="cne-video-description">(.+?)</div>',
  56. r'<div class="video-post-content">(.+?)</div>',
  57. ],
  58. webpage, 'description', fatal=False, flags=re.DOTALL)
  59. else:
  60. description = None
  61. params = self._search_regex(r'var params = {(.+?)}[;,]', webpage,
  62. 'player params', flags=re.DOTALL)
  63. video_id = self._search_regex(r'videoId: [\'"](.+?)[\'"]', params, 'video id')
  64. player_id = self._search_regex(r'playerId: [\'"](.+?)[\'"]', params, 'player id')
  65. target = self._search_regex(r'target: [\'"](.+?)[\'"]', params, 'target')
  66. data = compat_urllib_parse.urlencode({'videoId': video_id,
  67. 'playerId': player_id,
  68. 'target': target,
  69. })
  70. base_info_url = self._search_regex(r'url = [\'"](.+?)[\'"][,;]',
  71. webpage, 'base info url',
  72. default='http://player.cnevids.com/player/loader.js?')
  73. info_url = base_info_url + data
  74. info_page = self._download_webpage(info_url, video_id,
  75. 'Downloading video info')
  76. video_info = self._search_regex(r'var video = ({.+?});', info_page, 'video info')
  77. video_info = json.loads(video_info)
  78. formats = [{
  79. 'format_id': '%s-%s' % (fdata['type'].split('/')[-1], fdata['quality']),
  80. 'url': fdata['src'],
  81. 'ext': fdata['type'].split('/')[-1],
  82. 'quality': 1 if fdata['quality'] == 'high' else 0,
  83. } for fdata in video_info['sources'][0]]
  84. self._sort_formats(formats)
  85. return {
  86. 'id': video_id,
  87. 'formats': formats,
  88. 'title': video_info['title'],
  89. 'thumbnail': video_info['poster_frame'],
  90. 'description': description,
  91. }
  92. def _real_extract(self, url):
  93. mobj = re.match(self._VALID_URL, url)
  94. site = mobj.group('site')
  95. url_type = mobj.group('type')
  96. item_id = mobj.group('id')
  97. self.to_screen('Extracting from %s with the Condé Nast extractor' % self._SITES[site])
  98. webpage = self._download_webpage(url, item_id)
  99. if url_type == 'series':
  100. return self._extract_series(url, webpage)
  101. else:
  102. return self._extract_video(webpage, url_type)