livestream.py 14 KB

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