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.

106 lines
4.8 KiB

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