You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.9 KiB

  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. _TESTS = [{
  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. # From http://sportscanada.tv/canadagames/index.php/week2/figure-skating/444
  23. # Title and uploader available only from params JSON
  24. 'url': 'http://www.ustream.tv/embed/recorded/59307601?ub=ff0000&lc=ff0000&oc=ffffff&uc=ffffff&v=3&wmode=direct',
  25. 'md5': '5a2abf40babeac9812ed20ae12d34e10',
  26. 'info_dict': {
  27. 'id': '59307601',
  28. 'ext': 'flv',
  29. 'title': '-CG11- Canada Games Figure Skating',
  30. 'uploader': 'sportscanadatv',
  31. }
  32. }]
  33. def _real_extract(self, url):
  34. m = re.match(self._VALID_URL, url)
  35. video_id = m.group('videoID')
  36. # some sites use this embed format (see: http://github.com/rg3/youtube-dl/issues/2990)
  37. if m.group('type') == 'embed/recorded':
  38. video_id = m.group('videoID')
  39. desktop_url = 'http://www.ustream.tv/recorded/' + video_id
  40. return self.url_result(desktop_url, 'Ustream')
  41. if m.group('type') == 'embed':
  42. video_id = m.group('videoID')
  43. webpage = self._download_webpage(url, video_id)
  44. desktop_video_id = self._html_search_regex(
  45. r'ContentVideoIds=\["([^"]*?)"\]', webpage, 'desktop_video_id')
  46. desktop_url = 'http://www.ustream.tv/recorded/' + desktop_video_id
  47. return self.url_result(desktop_url, 'Ustream')
  48. params = self._download_json(
  49. 'http://cdngw.ustream.tv/rgwjson/Viewer.getVideo/' + json.dumps({
  50. 'brandId': 1,
  51. 'videoId': int(video_id),
  52. 'autoplay': False,
  53. }), video_id)
  54. if 'error' in params:
  55. raise ExtractorError(params['error']['message'], expected=True)
  56. video_url = params['flv']
  57. webpage = self._download_webpage(url, video_id)
  58. self.report_extraction(video_id)
  59. video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
  60. webpage, 'title', default=None)
  61. if not video_title:
  62. try:
  63. video_title = params['moduleConfig']['meta']['title']
  64. except KeyError:
  65. pass
  66. if not video_title:
  67. video_title = 'Ustream video ' + video_id
  68. uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
  69. webpage, 'uploader', fatal=False, flags=re.DOTALL, default=None)
  70. if not uploader:
  71. try:
  72. uploader = params['moduleConfig']['meta']['userName']
  73. except KeyError:
  74. uploader = None
  75. thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
  76. webpage, 'thumbnail', fatal=False)
  77. return {
  78. 'id': video_id,
  79. 'url': video_url,
  80. 'ext': 'flv',
  81. 'title': video_title,
  82. 'uploader': uploader,
  83. 'thumbnail': thumbnail,
  84. }
  85. class UstreamChannelIE(InfoExtractor):
  86. _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
  87. IE_NAME = 'ustream:channel'
  88. _TEST = {
  89. 'url': 'http://www.ustream.tv/channel/channeljapan',
  90. 'info_dict': {
  91. 'id': '10874166',
  92. },
  93. 'playlist_mincount': 17,
  94. }
  95. def _real_extract(self, url):
  96. m = re.match(self._VALID_URL, url)
  97. display_id = m.group('slug')
  98. webpage = self._download_webpage(url, display_id)
  99. channel_id = self._html_search_meta('ustream:channel_id', webpage)
  100. BASE = 'http://www.ustream.tv'
  101. next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
  102. video_ids = []
  103. while next_url:
  104. reply = self._download_json(
  105. compat_urlparse.urljoin(BASE, next_url), display_id,
  106. note='Downloading video information (next: %d)' % (len(video_ids) + 1))
  107. video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
  108. next_url = reply['nextUrl']
  109. entries = [
  110. self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream')
  111. for vid in video_ids]
  112. return {
  113. '_type': 'playlist',
  114. 'id': channel_id,
  115. 'display_id': display_id,
  116. 'entries': entries,
  117. }