slideshare.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import json
  2. import urllib.parse
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. get_element_by_id,
  7. )
  8. class SlideshareIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:www\.)?slideshare\.net/[^/]+?/(?P<title>.+?)($|\?)'
  10. _TEST = {
  11. 'url': 'http://www.slideshare.net/Dataversity/keynote-presentation-managing-scale-and-complexity',
  12. 'info_dict': {
  13. 'id': '25665706',
  14. 'ext': 'mp4',
  15. 'title': 'Managing Scale and Complexity',
  16. 'description': 'This was a keynote presentation at the NoSQL Now! 2013 Conference & Expo (http://www.nosqlnow.com). This presentation was given by Adrian Cockcroft from Netflix.',
  17. },
  18. }
  19. def _real_extract(self, url):
  20. mobj = self._match_valid_url(url)
  21. page_title = mobj.group('title')
  22. webpage = self._download_webpage(url, page_title)
  23. slideshare_obj = self._search_regex(
  24. r'\$\.extend\(.*?slideshare_object,\s*(\{.*?\})\);',
  25. webpage, 'slideshare object')
  26. info = json.loads(slideshare_obj)
  27. if info['slideshow']['type'] != 'video':
  28. raise ExtractorError('Webpage type is "{}": only video extraction is supported for Slideshare'.format(info['slideshow']['type']), expected=True)
  29. doc = info['doc']
  30. bucket = info['jsplayer']['video_bucket']
  31. ext = info['jsplayer']['video_extension']
  32. video_url = urllib.parse.urljoin(bucket, doc + '-SD.' + ext)
  33. description = get_element_by_id('slideshow-description-paragraph', webpage) or self._html_search_regex(
  34. r'(?s)<p[^>]+itemprop="description"[^>]*>(.+?)</p>', webpage,
  35. 'description', fatal=False)
  36. return {
  37. '_type': 'video',
  38. 'id': info['slideshow']['id'],
  39. 'title': info['slideshow']['title'],
  40. 'ext': ext,
  41. 'url': video_url,
  42. 'thumbnail': info['slideshow']['pin_image_url'],
  43. 'description': description.strip() if description else None,
  44. }