ustream.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_urlparse,
  7. get_meta_content,
  8. )
  9. class UstreamIE(InfoExtractor):
  10. _VALID_URL = r'https?://www\.ustream\.tv/(?P<type>recorded|embed|embed/recorded)/(?P<videoID>\d+)'
  11. IE_NAME = 'ustream'
  12. _TEST = {
  13. 'url': 'http://www.ustream.tv/recorded/20274954',
  14. 'file': '20274954.flv',
  15. 'md5': '088f151799e8f572f84eb62f17d73e5c',
  16. 'info_dict': {
  17. "uploader": "Young Americans for Liberty",
  18. "title": "Young Americans for Liberty February 7, 2012 2:28 AM",
  19. },
  20. }
  21. def _real_extract(self, url):
  22. m = re.match(self._VALID_URL, url)
  23. video_id = m.group('videoID')
  24. if m.group('type') == 'embed/recorded': # some sites use this embed format (see: http://github.com/rg3/youtube-dl/issues/2990)
  25. video_id = m.group('videoID')
  26. webpage = self._download_webpage(url, video_id, note="Downloading embedded Ustream page")
  27. desktop_url = 'http://www.ustream.tv/recorded/' + video_id
  28. return self.url_result(desktop_url, 'Ustream')
  29. if m.group('type') == 'embed':
  30. video_id = m.group('videoID')
  31. webpage = self._download_webpage(url, video_id)
  32. desktop_video_id = self._html_search_regex(r'ContentVideoIds=\["([^"]*?)"\]', webpage, 'desktop_video_id')
  33. desktop_url = 'http://www.ustream.tv/recorded/' + desktop_video_id
  34. return self.url_result(desktop_url, 'Ustream')
  35. video_url = 'http://tcdn.ustream.tv/video/%s' % video_id
  36. webpage = self._download_webpage(url, video_id)
  37. self.report_extraction(video_id)
  38. video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
  39. webpage, 'title')
  40. uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
  41. webpage, 'uploader', fatal=False, flags=re.DOTALL)
  42. thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
  43. webpage, 'thumbnail', fatal=False)
  44. return {
  45. 'id': video_id,
  46. 'url': video_url,
  47. 'ext': 'flv',
  48. 'title': video_title,
  49. 'uploader': uploader,
  50. 'thumbnail': thumbnail,
  51. }
  52. class UstreamChannelIE(InfoExtractor):
  53. _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
  54. IE_NAME = 'ustream:channel'
  55. def _real_extract(self, url):
  56. m = re.match(self._VALID_URL, url)
  57. slug = m.group('slug')
  58. webpage = self._download_webpage(url, slug)
  59. channel_id = get_meta_content('ustream:channel_id', webpage)
  60. BASE = 'http://www.ustream.tv'
  61. next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
  62. video_ids = []
  63. while next_url:
  64. reply = json.loads(self._download_webpage(compat_urlparse.urljoin(BASE, next_url), channel_id))
  65. video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
  66. next_url = reply['nextUrl']
  67. urls = ['http://www.ustream.tv/recorded/' + vid for vid in video_ids]
  68. url_entries = [self.url_result(eurl, 'Ustream') for eurl in urls]
  69. return self.playlist_result(url_entries, channel_id)