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.

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