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.

283 lines
10 KiB

11 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. import itertools
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_str,
  8. compat_urllib_parse_urlparse,
  9. compat_urlparse,
  10. )
  11. from ..utils import (
  12. ExtractorError,
  13. find_xpath_attr,
  14. int_or_none,
  15. orderedSet,
  16. xpath_with_ns,
  17. )
  18. class LivestreamIE(InfoExtractor):
  19. IE_NAME = 'livestream'
  20. _VALID_URL = r'https?://(?:new\.)?livestream\.com/.*?/(?P<event_name>.*?)(/videos/(?P<id>[0-9]+)(?:/player)?)?/?(?:$|[?#])'
  21. _TESTS = [{
  22. 'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370',
  23. 'md5': '53274c76ba7754fb0e8d072716f2292b',
  24. 'info_dict': {
  25. 'id': '4719370',
  26. 'ext': 'mp4',
  27. 'title': 'Live from Webster Hall NYC',
  28. 'upload_date': '20121012',
  29. 'like_count': int,
  30. 'view_count': int,
  31. 'thumbnail': 're:^http://.*\.jpg$'
  32. }
  33. }, {
  34. 'url': 'http://new.livestream.com/tedx/cityenglish',
  35. 'info_dict': {
  36. 'title': 'TEDCity2.0 (English)',
  37. 'id': '2245590',
  38. },
  39. 'playlist_mincount': 4,
  40. }, {
  41. 'url': 'http://new.livestream.com/chess24/tatasteelchess',
  42. 'info_dict': {
  43. 'title': 'Tata Steel Chess',
  44. 'id': '3705884',
  45. },
  46. 'playlist_mincount': 60,
  47. }, {
  48. 'url': 'https://new.livestream.com/accounts/362/events/3557232/videos/67864563/player?autoPlay=false&height=360&mute=false&width=640',
  49. 'only_matching': True,
  50. }, {
  51. 'url': 'http://livestream.com/bsww/concacafbeachsoccercampeonato2015',
  52. 'only_matching': True,
  53. }]
  54. def _parse_smil(self, video_id, smil_url):
  55. formats = []
  56. _SWITCH_XPATH = (
  57. './/{http://www.w3.org/2001/SMIL20/Language}body/'
  58. '{http://www.w3.org/2001/SMIL20/Language}switch')
  59. smil_doc = self._download_xml(
  60. smil_url, video_id,
  61. note='Downloading SMIL information',
  62. errnote='Unable to download SMIL information',
  63. fatal=False)
  64. if smil_doc is False: # Download failed
  65. return formats
  66. title_node = find_xpath_attr(
  67. smil_doc, './/{http://www.w3.org/2001/SMIL20/Language}meta',
  68. 'name', 'title')
  69. if title_node is None:
  70. self.report_warning('Cannot find SMIL id')
  71. switch_node = smil_doc.find(_SWITCH_XPATH)
  72. else:
  73. title_id = title_node.attrib['content']
  74. switch_node = find_xpath_attr(
  75. smil_doc, _SWITCH_XPATH, 'id', title_id)
  76. if switch_node is None:
  77. raise ExtractorError('Cannot find switch node')
  78. video_nodes = switch_node.findall(
  79. '{http://www.w3.org/2001/SMIL20/Language}video')
  80. for vn in video_nodes:
  81. tbr = int_or_none(vn.attrib.get('system-bitrate'))
  82. furl = (
  83. 'http://livestream-f.akamaihd.net/%s?v=3.0.3&fp=WIN%%2014,0,0,145' %
  84. (vn.attrib['src']))
  85. if 'clipBegin' in vn.attrib:
  86. furl += '&ssek=' + vn.attrib['clipBegin']
  87. formats.append({
  88. 'url': furl,
  89. 'format_id': 'smil_%d' % tbr,
  90. 'ext': 'flv',
  91. 'tbr': tbr,
  92. 'preference': -1000,
  93. })
  94. return formats
  95. def _extract_video_info(self, video_data):
  96. video_id = compat_str(video_data['id'])
  97. FORMAT_KEYS = (
  98. ('sd', 'progressive_url'),
  99. ('hd', 'progressive_url_hd'),
  100. )
  101. formats = [{
  102. 'format_id': format_id,
  103. 'url': video_data[key],
  104. 'quality': i + 1,
  105. } for i, (format_id, key) in enumerate(FORMAT_KEYS)
  106. if video_data.get(key)]
  107. smil_url = video_data.get('smil_url')
  108. if smil_url:
  109. formats.extend(self._parse_smil(video_id, smil_url))
  110. self._sort_formats(formats)
  111. return {
  112. 'id': video_id,
  113. 'formats': formats,
  114. 'title': video_data['caption'],
  115. 'thumbnail': video_data.get('thumbnail_url'),
  116. 'upload_date': video_data['updated_at'].replace('-', '')[:8],
  117. 'like_count': video_data.get('likes', {}).get('total'),
  118. 'view_count': video_data.get('views'),
  119. }
  120. def _extract_event(self, info):
  121. event_id = compat_str(info['id'])
  122. account = compat_str(info['owner_account_id'])
  123. root_url = (
  124. 'https://new.livestream.com/api/accounts/{account}/events/{event}/'
  125. 'feed.json'.format(account=account, event=event_id))
  126. def _extract_videos():
  127. last_video = None
  128. for i in itertools.count(1):
  129. if last_video is None:
  130. info_url = root_url
  131. else:
  132. info_url = '{root}?&id={id}&newer=-1&type=video'.format(
  133. root=root_url, id=last_video)
  134. videos_info = self._download_json(info_url, event_id, 'Downloading page {0}'.format(i))['data']
  135. videos_info = [v['data'] for v in videos_info if v['type'] == 'video']
  136. if not videos_info:
  137. break
  138. for v in videos_info:
  139. yield self._extract_video_info(v)
  140. last_video = videos_info[-1]['id']
  141. return self.playlist_result(_extract_videos(), event_id, info['full_name'])
  142. def _real_extract(self, url):
  143. mobj = re.match(self._VALID_URL, url)
  144. video_id = mobj.group('id')
  145. event_name = mobj.group('event_name')
  146. webpage = self._download_webpage(url, video_id or event_name)
  147. og_video = self._og_search_video_url(
  148. webpage, 'player url', fatal=False, default=None)
  149. if og_video is not None:
  150. query_str = compat_urllib_parse_urlparse(og_video).query
  151. query = compat_urlparse.parse_qs(query_str)
  152. if 'play_url' in query:
  153. api_url = query['play_url'][0].replace('.smil', '')
  154. info = json.loads(self._download_webpage(
  155. api_url, video_id, 'Downloading video info'))
  156. return self._extract_video_info(info)
  157. config_json = self._search_regex(
  158. r'window.config = ({.*?});', webpage, 'window config')
  159. info = json.loads(config_json)['event']
  160. def is_relevant(vdata, vid):
  161. result = vdata['type'] == 'video'
  162. if video_id is not None:
  163. result = result and compat_str(vdata['data']['id']) == vid
  164. return result
  165. if video_id is None:
  166. # This is an event page:
  167. return self._extract_event(info)
  168. else:
  169. videos = [self._extract_video_info(video_data['data'])
  170. for video_data in info['feed']['data']
  171. if is_relevant(video_data, video_id)]
  172. if not videos:
  173. raise ExtractorError('Cannot find video %s' % video_id)
  174. return videos[0]
  175. # The original version of Livestream uses a different system
  176. class LivestreamOriginalIE(InfoExtractor):
  177. IE_NAME = 'livestream:original'
  178. _VALID_URL = r'''(?x)https?://www\.livestream\.com/
  179. (?P<user>[^/]+)/(?P<type>video|folder)
  180. (?:\?.*?Id=|/)(?P<id>.*?)(&|$)
  181. '''
  182. _TESTS = [{
  183. 'url': 'http://www.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  184. 'info_dict': {
  185. 'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  186. 'ext': 'flv',
  187. 'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
  188. },
  189. 'params': {
  190. # rtmp
  191. 'skip_download': True,
  192. },
  193. }, {
  194. 'url': 'https://www.livestream.com/newplay/folder?dirId=a07bf706-d0e4-4e75-a747-b021d84f2fd3',
  195. 'info_dict': {
  196. 'id': 'a07bf706-d0e4-4e75-a747-b021d84f2fd3',
  197. },
  198. 'playlist_mincount': 4,
  199. }]
  200. def _extract_video(self, user, video_id):
  201. api_url = 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user, video_id)
  202. info = self._download_xml(api_url, video_id)
  203. item = info.find('channel').find('item')
  204. ns = {'media': 'http://search.yahoo.com/mrss'}
  205. thumbnail_url = item.find(xpath_with_ns('media:thumbnail', ns)).attrib['url']
  206. # Remove the extension and number from the path (like 1.jpg)
  207. path = self._search_regex(r'(user-files/.+)_.*?\.jpg$', thumbnail_url, 'path')
  208. return {
  209. 'id': video_id,
  210. 'title': item.find('title').text,
  211. 'url': 'rtmp://extondemand.livestream.com/ondemand',
  212. 'play_path': 'trans/dv15/mogulus-{0}'.format(path),
  213. 'player_url': 'http://static.livestream.com/chromelessPlayer/v21/playerapi.swf?hash=5uetk&v=0803&classid=D27CDB6E-AE6D-11cf-96B8-444553540000&jsEnabled=false&wmode=opaque',
  214. 'ext': 'flv',
  215. 'thumbnail': thumbnail_url,
  216. }
  217. def _extract_folder(self, url, folder_id):
  218. webpage = self._download_webpage(url, folder_id)
  219. paths = orderedSet(re.findall(
  220. r'''(?x)(?:
  221. <li\s+class="folder">\s*<a\s+href="|
  222. <a\s+href="(?=https?://livestre\.am/)
  223. )([^"]+)"''', webpage))
  224. return {
  225. '_type': 'playlist',
  226. 'id': folder_id,
  227. 'entries': [{
  228. '_type': 'url',
  229. 'url': compat_urlparse.urljoin(url, p),
  230. } for p in paths],
  231. }
  232. def _real_extract(self, url):
  233. mobj = re.match(self._VALID_URL, url)
  234. id = mobj.group('id')
  235. user = mobj.group('user')
  236. url_type = mobj.group('type')
  237. if url_type == 'folder':
  238. return self._extract_folder(url, id)
  239. else:
  240. return self._extract_video(user, id)
  241. # The server doesn't support HEAD request, the generic extractor can't detect
  242. # the redirection
  243. class LivestreamShortenerIE(InfoExtractor):
  244. IE_NAME = 'livestream:shortener'
  245. IE_DESC = False # Do not list
  246. _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
  247. def _real_extract(self, url):
  248. mobj = re.match(self._VALID_URL, url)
  249. id = mobj.group('id')
  250. webpage = self._download_webpage(url, id)
  251. return {
  252. '_type': 'url',
  253. 'url': self._og_search_url(webpage),
  254. }