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.

249 lines
8.9 KiB

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