ustream.py 4.8 KB

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