ustream.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urlparse,
  7. )
  8. from ..utils import ExtractorError
  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. 'md5': '088f151799e8f572f84eb62f17d73e5c',
  15. 'info_dict': {
  16. 'id': '20274954',
  17. 'ext': 'flv',
  18. 'uploader': 'Young Americans for Liberty',
  19. 'title': 'Young Americans for Liberty February 7, 2012 2:28 AM',
  20. },
  21. }
  22. def _real_extract(self, url):
  23. m = re.match(self._VALID_URL, url)
  24. video_id = m.group('videoID')
  25. # some sites use this embed format (see: http://github.com/rg3/youtube-dl/issues/2990)
  26. if m.group('type') == 'embed/recorded':
  27. video_id = m.group('videoID')
  28. desktop_url = 'http://www.ustream.tv/recorded/' + video_id
  29. return self.url_result(desktop_url, 'Ustream')
  30. if m.group('type') == 'embed':
  31. video_id = m.group('videoID')
  32. webpage = self._download_webpage(url, video_id)
  33. desktop_video_id = self._html_search_regex(
  34. r'ContentVideoIds=\["([^"]*?)"\]', webpage, 'desktop_video_id')
  35. desktop_url = 'http://www.ustream.tv/recorded/' + desktop_video_id
  36. return self.url_result(desktop_url, 'Ustream')
  37. params = self._download_json(
  38. 'http://cdngw.ustream.tv/rgwjson/Viewer.getVideo/' + json.dumps({
  39. 'brandId': 1,
  40. 'videoId': int(video_id),
  41. 'autoplay': False,
  42. }), video_id)
  43. if 'error' in params:
  44. raise ExtractorError(params['error']['message'], expected=True)
  45. video_url = params['flv']
  46. webpage = self._download_webpage(url, video_id)
  47. self.report_extraction(video_id)
  48. video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
  49. webpage, 'title')
  50. uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
  51. webpage, 'uploader', fatal=False, flags=re.DOTALL)
  52. thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
  53. webpage, 'thumbnail', fatal=False)
  54. return {
  55. 'id': video_id,
  56. 'url': video_url,
  57. 'ext': 'flv',
  58. 'title': video_title,
  59. 'uploader': uploader,
  60. 'thumbnail': thumbnail,
  61. }
  62. class UstreamChannelIE(InfoExtractor):
  63. _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
  64. IE_NAME = 'ustream:channel'
  65. _TEST = {
  66. 'url': 'http://www.ustream.tv/channel/channeljapan',
  67. 'info_dict': {
  68. 'id': '10874166',
  69. },
  70. 'playlist_mincount': 17,
  71. }
  72. def _real_extract(self, url):
  73. m = re.match(self._VALID_URL, url)
  74. display_id = m.group('slug')
  75. webpage = self._download_webpage(url, display_id)
  76. channel_id = self._html_search_meta('ustream:channel_id', webpage)
  77. BASE = 'http://www.ustream.tv'
  78. next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
  79. video_ids = []
  80. while next_url:
  81. reply = self._download_json(
  82. compat_urlparse.urljoin(BASE, next_url), display_id,
  83. note='Downloading video information (next: %d)' % (len(video_ids) + 1))
  84. video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
  85. next_url = reply['nextUrl']
  86. entries = [
  87. self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream')
  88. for vid in video_ids]
  89. return {
  90. '_type': 'playlist',
  91. 'id': channel_id,
  92. 'display_id': display_id,
  93. 'entries': entries,
  94. }