livestream.py 7.2 KB

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