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.

363 lines
13 KiB

10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. import itertools
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_str,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. find_xpath_attr,
  11. xpath_attr,
  12. xpath_with_ns,
  13. xpath_text,
  14. orderedSet,
  15. int_or_none,
  16. float_or_none,
  17. parse_iso8601,
  18. determine_ext,
  19. )
  20. class LivestreamIE(InfoExtractor):
  21. IE_NAME = 'livestream'
  22. _VALID_URL = r'https?://(?:new\.)?livestream\.com/(?:accounts/(?P<account_id>\d+)|(?P<account_name>[^/]+))/(?:events/(?P<event_id>\d+)|(?P<event_name>[^/]+))(?:/videos/(?P<id>\d+))?'
  23. _TESTS = [{
  24. 'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370',
  25. 'md5': '53274c76ba7754fb0e8d072716f2292b',
  26. 'info_dict': {
  27. 'id': '4719370',
  28. 'ext': 'mp4',
  29. 'title': 'Live from Webster Hall NYC',
  30. 'timestamp': 1350008072,
  31. 'upload_date': '20121012',
  32. 'duration': 5968.0,
  33. 'like_count': int,
  34. 'view_count': int,
  35. 'thumbnail': 're:^http://.*\.jpg$'
  36. }
  37. }, {
  38. 'url': 'http://new.livestream.com/tedx/cityenglish',
  39. 'info_dict': {
  40. 'title': 'TEDCity2.0 (English)',
  41. 'id': '2245590',
  42. },
  43. 'playlist_mincount': 4,
  44. }, {
  45. 'url': 'http://new.livestream.com/chess24/tatasteelchess',
  46. 'info_dict': {
  47. 'title': 'Tata Steel Chess',
  48. 'id': '3705884',
  49. },
  50. 'playlist_mincount': 60,
  51. }, {
  52. 'url': 'https://new.livestream.com/accounts/362/events/3557232/videos/67864563/player?autoPlay=false&height=360&mute=false&width=640',
  53. 'only_matching': True,
  54. }, {
  55. 'url': 'http://livestream.com/bsww/concacafbeachsoccercampeonato2015',
  56. 'only_matching': True,
  57. }]
  58. _API_URL_TEMPLATE = 'http://livestream.com/api/accounts/%s/events/%s'
  59. def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
  60. base_ele = find_xpath_attr(
  61. smil, self._xpath_ns('.//meta', namespace), 'name', 'httpBase')
  62. base = base_ele.get('content') if base_ele else 'http://livestreamvod-f.akamaihd.net/'
  63. formats = []
  64. video_nodes = smil.findall(self._xpath_ns('.//video', namespace))
  65. for vn in video_nodes:
  66. tbr = int_or_none(vn.attrib.get('system-bitrate'), 1000)
  67. furl = (
  68. '%s%s?v=3.0.3&fp=WIN%%2014,0,0,145' % (base, vn.attrib['src']))
  69. if 'clipBegin' in vn.attrib:
  70. furl += '&ssek=' + vn.attrib['clipBegin']
  71. formats.append({
  72. 'url': furl,
  73. 'format_id': 'smil_%d' % tbr,
  74. 'ext': 'flv',
  75. 'tbr': tbr,
  76. 'preference': -1000,
  77. })
  78. return formats
  79. def _extract_video_info(self, video_data):
  80. video_id = compat_str(video_data['id'])
  81. FORMAT_KEYS = (
  82. ('sd', 'progressive_url'),
  83. ('hd', 'progressive_url_hd'),
  84. )
  85. formats = []
  86. for format_id, key in FORMAT_KEYS:
  87. video_url = video_data.get(key)
  88. if video_url:
  89. ext = determine_ext(video_url)
  90. if ext == 'm3u8':
  91. continue
  92. bitrate = int_or_none(self._search_regex(
  93. r'(\d+)\.%s' % ext, video_url, 'bitrate', default=None))
  94. formats.append({
  95. 'url': video_url,
  96. 'format_id': format_id,
  97. 'tbr': bitrate,
  98. 'ext': ext,
  99. })
  100. smil_url = video_data.get('smil_url')
  101. if smil_url:
  102. formats.extend(self._extract_smil_formats(smil_url, video_id))
  103. m3u8_url = video_data.get('m3u8_url')
  104. if m3u8_url:
  105. formats.extend(self._extract_m3u8_formats(
  106. m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  107. f4m_url = video_data.get('f4m_url')
  108. if f4m_url:
  109. formats.extend(self._extract_f4m_formats(
  110. f4m_url, video_id, f4m_id='hds', fatal=False))
  111. self._sort_formats(formats)
  112. comments = [{
  113. 'author_id': comment.get('author_id'),
  114. 'author': comment.get('author', {}).get('full_name'),
  115. 'id': comment.get('id'),
  116. 'text': comment['text'],
  117. 'timestamp': parse_iso8601(comment.get('created_at')),
  118. } for comment in video_data.get('comments', {}).get('data', [])]
  119. return {
  120. 'id': video_id,
  121. 'formats': formats,
  122. 'title': video_data['caption'],
  123. 'description': video_data.get('description'),
  124. 'thumbnail': video_data.get('thumbnail_url'),
  125. 'duration': float_or_none(video_data.get('duration'), 1000),
  126. 'timestamp': parse_iso8601(video_data.get('publish_at')),
  127. 'like_count': video_data.get('likes', {}).get('total'),
  128. 'comment_count': video_data.get('comments', {}).get('total'),
  129. 'view_count': video_data.get('views'),
  130. 'comments': comments,
  131. }
  132. def _extract_stream_info(self, stream_info):
  133. broadcast_id = stream_info['broadcast_id']
  134. is_live = stream_info.get('is_live')
  135. formats = []
  136. smil_url = stream_info.get('play_url')
  137. if smil_url:
  138. formats.extend(self._extract_smil_formats(smil_url, broadcast_id))
  139. entry_protocol = 'm3u8' if is_live else 'm3u8_native'
  140. m3u8_url = stream_info.get('m3u8_url')
  141. if m3u8_url:
  142. formats.extend(self._extract_m3u8_formats(
  143. m3u8_url, broadcast_id, 'mp4', entry_protocol, m3u8_id='hls', fatal=False))
  144. rtsp_url = stream_info.get('rtsp_url')
  145. if rtsp_url:
  146. formats.append({
  147. 'url': rtsp_url,
  148. 'format_id': 'rtsp',
  149. })
  150. self._sort_formats(formats)
  151. return {
  152. 'id': broadcast_id,
  153. 'formats': formats,
  154. 'title': self._live_title(stream_info['stream_title']) if is_live else stream_info['stream_title'],
  155. 'thumbnail': stream_info.get('thumbnail_url'),
  156. 'is_live': is_live,
  157. }
  158. def _extract_event(self, event_data):
  159. event_id = compat_str(event_data['id'])
  160. account_id = compat_str(event_data['owner_account_id'])
  161. feed_root_url = self._API_URL_TEMPLATE % (account_id, event_id) + '/feed.json'
  162. stream_info = event_data.get('stream_info')
  163. if stream_info:
  164. return self._extract_stream_info(stream_info)
  165. last_video = None
  166. entries = []
  167. for i in itertools.count(1):
  168. if last_video is None:
  169. info_url = feed_root_url
  170. else:
  171. info_url = '{root}?&id={id}&newer=-1&type=video'.format(
  172. root=feed_root_url, id=last_video)
  173. videos_info = self._download_json(
  174. info_url, event_id, 'Downloading page {0}'.format(i))['data']
  175. videos_info = [v['data'] for v in videos_info if v['type'] == 'video']
  176. if not videos_info:
  177. break
  178. for v in videos_info:
  179. entries.append(self.url_result(
  180. 'http://livestream.com/accounts/%s/events/%s/videos/%s' % (account_id, event_id, v['id']),
  181. 'Livestream', v['id'], v['caption']))
  182. last_video = videos_info[-1]['id']
  183. return self.playlist_result(entries, event_id, event_data['full_name'])
  184. def _real_extract(self, url):
  185. mobj = re.match(self._VALID_URL, url)
  186. video_id = mobj.group('id')
  187. event = mobj.group('event_id') or mobj.group('event_name')
  188. account = mobj.group('account_id') or mobj.group('account_name')
  189. api_url = self._API_URL_TEMPLATE % (account, event)
  190. if video_id:
  191. video_data = self._download_json(
  192. api_url + '/videos/%s' % video_id, video_id)
  193. return self._extract_video_info(video_data)
  194. else:
  195. event_data = self._download_json(api_url, video_id)
  196. return self._extract_event(event_data)
  197. # The original version of Livestream uses a different system
  198. class LivestreamOriginalIE(InfoExtractor):
  199. IE_NAME = 'livestream:original'
  200. _VALID_URL = r'''(?x)https?://original\.livestream\.com/
  201. (?P<user>[^/\?#]+)(?:/(?P<type>video|folder)
  202. (?:(?:\?.*?Id=|/)(?P<id>.*?)(&|$))?)?
  203. '''
  204. _TESTS = [{
  205. 'url': 'http://original.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  206. 'info_dict': {
  207. 'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  208. 'ext': 'mp4',
  209. 'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
  210. 'duration': 771.301,
  211. 'view_count': int,
  212. },
  213. }, {
  214. 'url': 'https://original.livestream.com/newplay/folder?dirId=a07bf706-d0e4-4e75-a747-b021d84f2fd3',
  215. 'info_dict': {
  216. 'id': 'a07bf706-d0e4-4e75-a747-b021d84f2fd3',
  217. },
  218. 'playlist_mincount': 4,
  219. }, {
  220. # live stream
  221. 'url': 'http://original.livestream.com/znsbahamas',
  222. 'only_matching': True,
  223. }]
  224. def _extract_video_info(self, user, video_id):
  225. api_url = 'http://x%sx.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id=%s' % (user, video_id)
  226. info = self._download_xml(api_url, video_id)
  227. item = info.find('channel').find('item')
  228. title = xpath_text(item, 'title')
  229. media_ns = {'media': 'http://search.yahoo.com/mrss'}
  230. thumbnail_url = xpath_attr(
  231. item, xpath_with_ns('media:thumbnail', media_ns), 'url')
  232. duration = float_or_none(xpath_attr(
  233. item, xpath_with_ns('media:content', media_ns), 'duration'))
  234. ls_ns = {'ls': 'http://api.channel.livestream.com/2.0'}
  235. view_count = int_or_none(xpath_text(
  236. item, xpath_with_ns('ls:viewsCount', ls_ns)))
  237. return {
  238. 'id': video_id,
  239. 'title': title,
  240. 'thumbnail': thumbnail_url,
  241. 'duration': duration,
  242. 'view_count': view_count,
  243. }
  244. def _extract_video_formats(self, video_data, video_id, entry_protocol):
  245. formats = []
  246. progressive_url = video_data.get('progressiveUrl')
  247. if progressive_url:
  248. formats.append({
  249. 'url': progressive_url,
  250. 'format_id': 'http',
  251. })
  252. m3u8_url = video_data.get('httpUrl')
  253. if m3u8_url:
  254. formats.extend(self._extract_m3u8_formats(
  255. m3u8_url, video_id, 'mp4', entry_protocol, m3u8_id='hls', fatal=False))
  256. rtsp_url = video_data.get('rtspUrl')
  257. if rtsp_url:
  258. formats.append({
  259. 'url': rtsp_url,
  260. 'format_id': 'rtsp',
  261. })
  262. self._sort_formats(formats)
  263. return formats
  264. def _extract_folder(self, url, folder_id):
  265. webpage = self._download_webpage(url, folder_id)
  266. paths = orderedSet(re.findall(
  267. r'''(?x)(?:
  268. <li\s+class="folder">\s*<a\s+href="|
  269. <a\s+href="(?=https?://livestre\.am/)
  270. )([^"]+)"''', webpage))
  271. entries = [{
  272. '_type': 'url',
  273. 'url': compat_urlparse.urljoin(url, p),
  274. } for p in paths]
  275. return self.playlist_result(entries, folder_id)
  276. def _real_extract(self, url):
  277. mobj = re.match(self._VALID_URL, url)
  278. user = mobj.group('user')
  279. url_type = mobj.group('type')
  280. content_id = mobj.group('id')
  281. if url_type == 'folder':
  282. return self._extract_folder(url, content_id)
  283. else:
  284. # this url is used on mobile devices
  285. stream_url = 'http://x%sx.api.channel.livestream.com/3.0/getstream.json' % user
  286. info = {}
  287. if content_id:
  288. stream_url += '?id=%s' % content_id
  289. info = self._extract_video_info(user, content_id)
  290. else:
  291. content_id = user
  292. webpage = self._download_webpage(url, content_id)
  293. info = {
  294. 'title': self._og_search_title(webpage),
  295. 'description': self._og_search_description(webpage),
  296. 'thumbnail': self._search_regex(r'channelLogo.src\s*=\s*"([^"]+)"', webpage, 'thumbnail', None),
  297. }
  298. video_data = self._download_json(stream_url, content_id)
  299. is_live = video_data.get('isLive')
  300. entry_protocol = 'm3u8' if is_live else 'm3u8_native'
  301. info.update({
  302. 'id': content_id,
  303. 'title': self._live_title(info['title']) if is_live else info['title'],
  304. 'formats': self._extract_video_formats(video_data, content_id, entry_protocol),
  305. 'is_live': is_live,
  306. })
  307. return info
  308. # The server doesn't support HEAD request, the generic extractor can't detect
  309. # the redirection
  310. class LivestreamShortenerIE(InfoExtractor):
  311. IE_NAME = 'livestream:shortener'
  312. IE_DESC = False # Do not list
  313. _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
  314. def _real_extract(self, url):
  315. mobj = re.match(self._VALID_URL, url)
  316. id = mobj.group('id')
  317. webpage = self._download_webpage(url, id)
  318. return {
  319. '_type': 'url',
  320. 'url': self._og_search_url(webpage),
  321. }