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.

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