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.

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