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.

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