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.

375 lines
14 KiB

11 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. smil_formats = self._extract_smil_formats(smil_url, video_id)
  103. if smil_formats:
  104. formats.extend(smil_formats)
  105. m3u8_url = video_data.get('m3u8_url')
  106. if m3u8_url:
  107. m3u8_formats = self._extract_m3u8_formats(
  108. m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
  109. if m3u8_formats:
  110. formats.extend(m3u8_formats)
  111. f4m_url = video_data.get('f4m_url')
  112. if f4m_url:
  113. f4m_formats = self._extract_f4m_formats(
  114. f4m_url, video_id, f4m_id='hds', fatal=False)
  115. if f4m_formats:
  116. formats.extend(f4m_formats)
  117. self._sort_formats(formats)
  118. comments = [{
  119. 'author_id': comment.get('author_id'),
  120. 'author': comment.get('author', {}).get('full_name'),
  121. 'id': comment.get('id'),
  122. 'text': comment['text'],
  123. 'timestamp': parse_iso8601(comment.get('created_at')),
  124. } for comment in video_data.get('comments', {}).get('data', [])]
  125. return {
  126. 'id': video_id,
  127. 'formats': formats,
  128. 'title': video_data['caption'],
  129. 'description': video_data.get('description'),
  130. 'thumbnail': video_data.get('thumbnail_url'),
  131. 'duration': float_or_none(video_data.get('duration'), 1000),
  132. 'timestamp': parse_iso8601(video_data.get('publish_at')),
  133. 'like_count': video_data.get('likes', {}).get('total'),
  134. 'comment_count': video_data.get('comments', {}).get('total'),
  135. 'view_count': video_data.get('views'),
  136. 'comments': comments,
  137. }
  138. def _extract_stream_info(self, stream_info):
  139. broadcast_id = stream_info['broadcast_id']
  140. is_live = stream_info.get('is_live')
  141. formats = []
  142. smil_url = stream_info.get('play_url')
  143. if smil_url:
  144. smil_formats = self._extract_smil_formats(smil_url, broadcast_id)
  145. if smil_formats:
  146. formats.extend(smil_formats)
  147. entry_protocol = 'm3u8' if is_live else 'm3u8_native'
  148. m3u8_url = stream_info.get('m3u8_url')
  149. if m3u8_url:
  150. m3u8_formats = self._extract_m3u8_formats(
  151. m3u8_url, broadcast_id, 'mp4', entry_protocol, m3u8_id='hls', fatal=False)
  152. if m3u8_formats:
  153. formats.extend(m3u8_formats)
  154. rtsp_url = stream_info.get('rtsp_url')
  155. if rtsp_url:
  156. formats.append({
  157. 'url': rtsp_url,
  158. 'format_id': 'rtsp',
  159. })
  160. self._sort_formats(formats)
  161. return {
  162. 'id': broadcast_id,
  163. 'formats': formats,
  164. 'title': self._live_title(stream_info['stream_title']) if is_live else stream_info['stream_title'],
  165. 'thumbnail': stream_info.get('thumbnail_url'),
  166. 'is_live': is_live,
  167. }
  168. def _extract_event(self, event_data):
  169. event_id = compat_str(event_data['id'])
  170. account_id = compat_str(event_data['owner_account_id'])
  171. feed_root_url = self._API_URL_TEMPLATE % (account_id, event_id) + '/feed.json'
  172. stream_info = event_data.get('stream_info')
  173. if stream_info:
  174. return self._extract_stream_info(stream_info)
  175. last_video = None
  176. entries = []
  177. for i in itertools.count(1):
  178. if last_video is None:
  179. info_url = feed_root_url
  180. else:
  181. info_url = '{root}?&id={id}&newer=-1&type=video'.format(
  182. root=feed_root_url, id=last_video)
  183. videos_info = self._download_json(
  184. info_url, event_id, 'Downloading page {0}'.format(i))['data']
  185. videos_info = [v['data'] for v in videos_info if v['type'] == 'video']
  186. if not videos_info:
  187. break
  188. for v in videos_info:
  189. entries.append(self.url_result(
  190. 'http://livestream.com/accounts/%s/events/%s/videos/%s' % (account_id, event_id, v['id']),
  191. 'Livestream', v['id'], v['caption']))
  192. last_video = videos_info[-1]['id']
  193. return self.playlist_result(entries, event_id, event_data['full_name'])
  194. def _real_extract(self, url):
  195. mobj = re.match(self._VALID_URL, url)
  196. video_id = mobj.group('id')
  197. event = mobj.group('event_id') or mobj.group('event_name')
  198. account = mobj.group('account_id') or mobj.group('account_name')
  199. api_url = self._API_URL_TEMPLATE % (account, event)
  200. if video_id:
  201. video_data = self._download_json(
  202. api_url + '/videos/%s' % video_id, video_id)
  203. return self._extract_video_info(video_data)
  204. else:
  205. event_data = self._download_json(api_url, video_id)
  206. return self._extract_event(event_data)
  207. # The original version of Livestream uses a different system
  208. class LivestreamOriginalIE(InfoExtractor):
  209. IE_NAME = 'livestream:original'
  210. _VALID_URL = r'''(?x)https?://original\.livestream\.com/
  211. (?P<user>[^/\?#]+)(?:/(?P<type>video|folder)
  212. (?:(?:\?.*?Id=|/)(?P<id>.*?)(&|$))?)?
  213. '''
  214. _TESTS = [{
  215. 'url': 'http://original.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  216. 'info_dict': {
  217. 'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  218. 'ext': 'mp4',
  219. 'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
  220. 'duration': 771.301,
  221. 'view_count': int,
  222. },
  223. }, {
  224. 'url': 'https://original.livestream.com/newplay/folder?dirId=a07bf706-d0e4-4e75-a747-b021d84f2fd3',
  225. 'info_dict': {
  226. 'id': 'a07bf706-d0e4-4e75-a747-b021d84f2fd3',
  227. },
  228. 'playlist_mincount': 4,
  229. }, {
  230. # live stream
  231. 'url': 'http://original.livestream.com/znsbahamas',
  232. 'only_matching': True,
  233. }]
  234. def _extract_video_info(self, user, video_id):
  235. api_url = 'http://x%sx.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id=%s' % (user, video_id)
  236. info = self._download_xml(api_url, video_id)
  237. item = info.find('channel').find('item')
  238. title = xpath_text(item, 'title')
  239. media_ns = {'media': 'http://search.yahoo.com/mrss'}
  240. thumbnail_url = xpath_attr(
  241. item, xpath_with_ns('media:thumbnail', media_ns), 'url')
  242. duration = float_or_none(xpath_attr(
  243. item, xpath_with_ns('media:content', media_ns), 'duration'))
  244. ls_ns = {'ls': 'http://api.channel.livestream.com/2.0'}
  245. view_count = int_or_none(xpath_text(
  246. item, xpath_with_ns('ls:viewsCount', ls_ns)))
  247. return {
  248. 'id': video_id,
  249. 'title': title,
  250. 'thumbnail': thumbnail_url,
  251. 'duration': duration,
  252. 'view_count': view_count,
  253. }
  254. def _extract_video_formats(self, video_data, video_id, entry_protocol):
  255. formats = []
  256. progressive_url = video_data.get('progressiveUrl')
  257. if progressive_url:
  258. formats.append({
  259. 'url': progressive_url,
  260. 'format_id': 'http',
  261. })
  262. m3u8_url = video_data.get('httpUrl')
  263. if m3u8_url:
  264. m3u8_formats = self._extract_m3u8_formats(
  265. m3u8_url, video_id, 'mp4', entry_protocol, m3u8_id='hls', fatal=False)
  266. if m3u8_formats:
  267. formats.extend(m3u8_formats)
  268. rtsp_url = video_data.get('rtspUrl')
  269. if rtsp_url:
  270. formats.append({
  271. 'url': rtsp_url,
  272. 'format_id': 'rtsp',
  273. })
  274. self._sort_formats(formats)
  275. return formats
  276. def _extract_folder(self, url, folder_id):
  277. webpage = self._download_webpage(url, folder_id)
  278. paths = orderedSet(re.findall(
  279. r'''(?x)(?:
  280. <li\s+class="folder">\s*<a\s+href="|
  281. <a\s+href="(?=https?://livestre\.am/)
  282. )([^"]+)"''', webpage))
  283. entries = [{
  284. '_type': 'url',
  285. 'url': compat_urlparse.urljoin(url, p),
  286. } for p in paths]
  287. return self.playlist_result(entries, folder_id)
  288. def _real_extract(self, url):
  289. mobj = re.match(self._VALID_URL, url)
  290. user = mobj.group('user')
  291. url_type = mobj.group('type')
  292. content_id = mobj.group('id')
  293. if url_type == 'folder':
  294. return self._extract_folder(url, content_id)
  295. else:
  296. # this url is used on mobile devices
  297. stream_url = 'http://x%sx.api.channel.livestream.com/3.0/getstream.json' % user
  298. info = {}
  299. if content_id:
  300. stream_url += '?id=%s' % content_id
  301. info = self._extract_video_info(user, content_id)
  302. else:
  303. content_id = user
  304. webpage = self._download_webpage(url, content_id)
  305. info = {
  306. 'title': self._og_search_title(webpage),
  307. 'description': self._og_search_description(webpage),
  308. 'thumbnail': self._search_regex(r'channelLogo.src\s*=\s*"([^"]+)"', webpage, 'thumbnail', None),
  309. }
  310. video_data = self._download_json(stream_url, content_id)
  311. is_live = video_data.get('isLive')
  312. entry_protocol = 'm3u8' if is_live else 'm3u8_native'
  313. info.update({
  314. 'id': content_id,
  315. 'title': self._live_title(info['title']) if is_live else info['title'],
  316. 'formats': self._extract_video_formats(video_data, content_id, entry_protocol),
  317. 'is_live': is_live,
  318. })
  319. return info
  320. # The server doesn't support HEAD request, the generic extractor can't detect
  321. # the redirection
  322. class LivestreamShortenerIE(InfoExtractor):
  323. IE_NAME = 'livestream:shortener'
  324. IE_DESC = False # Do not list
  325. _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
  326. def _real_extract(self, url):
  327. mobj = re.match(self._VALID_URL, url)
  328. id = mobj.group('id')
  329. webpage = self._download_webpage(url, id)
  330. return {
  331. '_type': 'url',
  332. 'url': self._og_search_url(webpage),
  333. }