ustream.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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', default=None)
  50. if not video_title:
  51. try:
  52. video_title = params['moduleConfig']['meta']['title']
  53. except KeyError:
  54. pass
  55. if not video_title:
  56. video_title = 'Ustream video ' + video_id
  57. uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
  58. webpage, 'uploader', fatal=False, flags=re.DOTALL)
  59. thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
  60. webpage, 'thumbnail', fatal=False)
  61. return {
  62. 'id': video_id,
  63. 'url': video_url,
  64. 'ext': 'flv',
  65. 'title': video_title,
  66. 'uploader': uploader,
  67. 'thumbnail': thumbnail,
  68. }
  69. class UstreamChannelIE(InfoExtractor):
  70. _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
  71. IE_NAME = 'ustream:channel'
  72. _TEST = {
  73. 'url': 'http://www.ustream.tv/channel/channeljapan',
  74. 'info_dict': {
  75. 'id': '10874166',
  76. },
  77. 'playlist_mincount': 17,
  78. }
  79. def _real_extract(self, url):
  80. m = re.match(self._VALID_URL, url)
  81. display_id = m.group('slug')
  82. webpage = self._download_webpage(url, display_id)
  83. channel_id = self._html_search_meta('ustream:channel_id', webpage)
  84. BASE = 'http://www.ustream.tv'
  85. next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
  86. video_ids = []
  87. while next_url:
  88. reply = self._download_json(
  89. compat_urlparse.urljoin(BASE, next_url), display_id,
  90. note='Downloading video information (next: %d)' % (len(video_ids) + 1))
  91. video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
  92. next_url = reply['nextUrl']
  93. entries = [
  94. self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream')
  95. for vid in video_ids]
  96. return {
  97. '_type': 'playlist',
  98. 'id': channel_id,
  99. 'display_id': display_id,
  100. 'entries': entries,
  101. }