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.

211 lines
7.5 KiB

10 years ago
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. if video_id is None:
  103. # This is an event page:
  104. config_json = self._search_regex(
  105. r'window.config = ({.*?});', webpage, 'window config')
  106. info = json.loads(config_json)['event']
  107. videos = [self._extract_video_info(video_data['data'])
  108. for video_data in info['feed']['data']
  109. if video_data['type'] == 'video']
  110. return self.playlist_result(videos, info['id'], info['full_name'])
  111. else:
  112. og_video = self._og_search_video_url(webpage, 'player url')
  113. query_str = compat_urllib_parse_urlparse(og_video).query
  114. query = compat_urlparse.parse_qs(query_str)
  115. api_url = query['play_url'][0].replace('.smil', '')
  116. info = json.loads(self._download_webpage(
  117. api_url, video_id, 'Downloading video info'))
  118. return self._extract_video_info(info)
  119. # The original version of Livestream uses a different system
  120. class LivestreamOriginalIE(InfoExtractor):
  121. IE_NAME = 'livestream:original'
  122. _VALID_URL = r'''(?x)https?://www\.livestream\.com/
  123. (?P<user>[^/]+)/(?P<type>video|folder)
  124. (?:\?.*?Id=|/)(?P<id>.*?)(&|$)
  125. '''
  126. _TEST = {
  127. 'url': 'http://www.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  128. 'info_dict': {
  129. 'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  130. 'ext': 'flv',
  131. 'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
  132. },
  133. 'params': {
  134. # rtmp
  135. 'skip_download': True,
  136. },
  137. }
  138. def _extract_video(self, user, video_id):
  139. api_url = 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user, video_id)
  140. info = self._download_xml(api_url, video_id)
  141. item = info.find('channel').find('item')
  142. ns = {'media': 'http://search.yahoo.com/mrss'}
  143. thumbnail_url = item.find(xpath_with_ns('media:thumbnail', ns)).attrib['url']
  144. # Remove the extension and number from the path (like 1.jpg)
  145. path = self._search_regex(r'(user-files/.+)_.*?\.jpg$', thumbnail_url, 'path')
  146. return {
  147. 'id': video_id,
  148. 'title': item.find('title').text,
  149. 'url': 'rtmp://extondemand.livestream.com/ondemand',
  150. 'play_path': 'mp4:trans/dv15/mogulus-{0}.mp4'.format(path),
  151. 'ext': 'flv',
  152. 'thumbnail': thumbnail_url,
  153. }
  154. def _extract_folder(self, url, folder_id):
  155. webpage = self._download_webpage(url, folder_id)
  156. urls = orderedSet(re.findall(r'<a href="(https?://livestre\.am/.*?)"', webpage))
  157. return {
  158. '_type': 'playlist',
  159. 'id': folder_id,
  160. 'entries': [{
  161. '_type': 'url',
  162. 'url': video_url,
  163. } for video_url in urls],
  164. }
  165. def _real_extract(self, url):
  166. mobj = re.match(self._VALID_URL, url)
  167. id = mobj.group('id')
  168. user = mobj.group('user')
  169. url_type = mobj.group('type')
  170. if url_type == 'folder':
  171. return self._extract_folder(url, id)
  172. else:
  173. return self._extract_video(user, id)
  174. # The server doesn't support HEAD request, the generic extractor can't detect
  175. # the redirection
  176. class LivestreamShortenerIE(InfoExtractor):
  177. IE_NAME = 'livestream:shortener'
  178. IE_DESC = False # Do not list
  179. _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
  180. def _real_extract(self, url):
  181. mobj = re.match(self._VALID_URL, url)
  182. id = mobj.group('id')
  183. webpage = self._download_webpage(url, id)
  184. return {
  185. '_type': 'url',
  186. 'url': self._og_search_url(webpage),
  187. }