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.

245 lines
8.8 KiB

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