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.

146 lines
5.1 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_urlparse,
  6. )
  7. from ..utils import (
  8. ExtractorError,
  9. int_or_none,
  10. float_or_none,
  11. )
  12. class UstreamIE(InfoExtractor):
  13. _VALID_URL = r'https?://www\.ustream\.tv/(?P<type>recorded|embed|embed/recorded)/(?P<id>\d+)'
  14. IE_NAME = 'ustream'
  15. _TESTS = [{
  16. 'url': 'http://www.ustream.tv/recorded/20274954',
  17. 'md5': '088f151799e8f572f84eb62f17d73e5c',
  18. 'info_dict': {
  19. 'id': '20274954',
  20. 'ext': 'flv',
  21. 'title': 'Young Americans for Liberty February 7, 2012 2:28 AM',
  22. 'description': 'Young Americans for Liberty February 7, 2012 2:28 AM',
  23. 'timestamp': 1328577035,
  24. 'upload_date': '20120207',
  25. 'uploader': 'yaliberty',
  26. 'uploader_id': '6780869',
  27. },
  28. }, {
  29. # From http://sportscanada.tv/canadagames/index.php/week2/figure-skating/444
  30. # Title and uploader available only from params JSON
  31. 'url': 'http://www.ustream.tv/embed/recorded/59307601?ub=ff0000&lc=ff0000&oc=ffffff&uc=ffffff&v=3&wmode=direct',
  32. 'md5': '5a2abf40babeac9812ed20ae12d34e10',
  33. 'info_dict': {
  34. 'id': '59307601',
  35. 'ext': 'flv',
  36. 'title': '-CG11- Canada Games Figure Skating',
  37. 'uploader': 'sportscanadatv',
  38. },
  39. 'skip': 'This Pro Broadcaster has chosen to remove this video from the ustream.tv site.',
  40. }]
  41. def _real_extract(self, url):
  42. m = re.match(self._VALID_URL, url)
  43. video_id = m.group('id')
  44. # some sites use this embed format (see: http://github.com/rg3/youtube-dl/issues/2990)
  45. if m.group('type') == 'embed/recorded':
  46. video_id = m.group('id')
  47. desktop_url = 'http://www.ustream.tv/recorded/' + video_id
  48. return self.url_result(desktop_url, 'Ustream')
  49. if m.group('type') == 'embed':
  50. video_id = m.group('id')
  51. webpage = self._download_webpage(url, video_id)
  52. desktop_video_id = self._html_search_regex(
  53. r'ContentVideoIds=\["([^"]*?)"\]', webpage, 'desktop_video_id')
  54. desktop_url = 'http://www.ustream.tv/recorded/' + desktop_video_id
  55. return self.url_result(desktop_url, 'Ustream')
  56. params = self._download_json(
  57. 'https://api.ustream.tv/videos/%s.json' % video_id, video_id)
  58. error = params.get('error')
  59. if error:
  60. raise ExtractorError(
  61. '%s returned error: %s' % (self.IE_NAME, error), expected=True)
  62. video = params['video']
  63. title = video['title']
  64. filesize = float_or_none(video.get('file_size'))
  65. formats = [{
  66. 'id': video_id,
  67. 'url': video_url,
  68. 'ext': format_id,
  69. 'filesize': filesize,
  70. } for format_id, video_url in video['media_urls'].items()]
  71. self._sort_formats(formats)
  72. description = video.get('description')
  73. timestamp = int_or_none(video.get('created_at'))
  74. duration = float_or_none(video.get('length'))
  75. view_count = int_or_none(video.get('views'))
  76. uploader = video.get('owner', {}).get('username')
  77. uploader_id = video.get('owner', {}).get('id')
  78. thumbnails = [{
  79. 'id': thumbnail_id,
  80. 'url': thumbnail_url,
  81. } for thumbnail_id, thumbnail_url in video.get('thumbnail', {}).items()]
  82. return {
  83. 'id': video_id,
  84. 'title': title,
  85. 'description': description,
  86. 'thumbnails': thumbnails,
  87. 'timestamp': timestamp,
  88. 'duration': duration,
  89. 'view_count': view_count,
  90. 'uploader': uploader,
  91. 'uploader_id': uploader_id,
  92. 'formats': formats,
  93. }
  94. class UstreamChannelIE(InfoExtractor):
  95. _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
  96. IE_NAME = 'ustream:channel'
  97. _TEST = {
  98. 'url': 'http://www.ustream.tv/channel/channeljapan',
  99. 'info_dict': {
  100. 'id': '10874166',
  101. },
  102. 'playlist_mincount': 17,
  103. }
  104. def _real_extract(self, url):
  105. m = re.match(self._VALID_URL, url)
  106. display_id = m.group('slug')
  107. webpage = self._download_webpage(url, display_id)
  108. channel_id = self._html_search_meta('ustream:channel_id', webpage)
  109. BASE = 'http://www.ustream.tv'
  110. next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
  111. video_ids = []
  112. while next_url:
  113. reply = self._download_json(
  114. compat_urlparse.urljoin(BASE, next_url), display_id,
  115. note='Downloading video information (next: %d)' % (len(video_ids) + 1))
  116. video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
  117. next_url = reply['nextUrl']
  118. entries = [
  119. self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream')
  120. for vid in video_ids]
  121. return {
  122. '_type': 'playlist',
  123. 'id': channel_id,
  124. 'display_id': display_id,
  125. 'entries': entries,
  126. }