condenast.py 5.4 KB

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