livestream.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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(smil, self._xpath_ns('.//meta', namespace), 'name', 'httpBase')
  61. base = base_ele.get('content') if base_ele else 'http://livestreamvod-f.akamaihd.net/'
  62. formats = []
  63. video_nodes = smil.findall(self._xpath_ns('.//video', namespace))
  64. for vn in video_nodes:
  65. tbr = int_or_none(vn.attrib.get('system-bitrate'), 1000)
  66. furl = (
  67. '%s%s?v=3.0.3&fp=WIN%%2014,0,0,145' % (base, vn.attrib['src']))
  68. if 'clipBegin' in vn.attrib:
  69. furl += '&ssek=' + vn.attrib['clipBegin']
  70. formats.append({
  71. 'url': furl,
  72. 'format_id': 'smil_%d' % tbr,
  73. 'ext': 'flv',
  74. 'tbr': tbr,
  75. 'preference': -1000,
  76. })
  77. return formats
  78. def _extract_video_info(self, video_data):
  79. video_id = compat_str(video_data['id'])
  80. FORMAT_KEYS = (
  81. ('sd', 'progressive_url'),
  82. ('hd', 'progressive_url_hd'),
  83. )
  84. formats = []
  85. for format_id, key in FORMAT_KEYS:
  86. video_url = video_data.get(key)
  87. if video_url:
  88. ext = determine_ext(video_url)
  89. bitrate = int_or_none(self._search_regex(r'(\d+)\.%s' % ext, video_url, 'bitrate', default=None))
  90. formats.append({
  91. 'url': video_url,
  92. 'format_id': format_id,
  93. 'tbr': bitrate,
  94. 'ext': ext,
  95. })
  96. smil_url = video_data.get('smil_url')
  97. if smil_url:
  98. smil_formats = self._extract_smil_formats(smil_url, video_id)
  99. if smil_formats:
  100. formats.extend(smil_formats)
  101. m3u8_url = video_data.get('m3u8_url')
  102. if m3u8_url:
  103. m3u8_formats = self._extract_m3u8_formats(
  104. m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
  105. if m3u8_formats:
  106. formats.extend(m3u8_formats)
  107. f4m_url = video_data.get('f4m_url')
  108. if f4m_url:
  109. f4m_formats = self._extract_f4m_formats(f4m_url, video_id, f4m_id='hds', fatal=False)
  110. if f4m_formats:
  111. formats.extend(f4m_formats)
  112. self._sort_formats(formats)
  113. comments = [{
  114. 'author_id': comment.get('author_id'),
  115. 'author': comment.get('author', {}).get('full_name'),
  116. 'id': comment.get('id'),
  117. 'text': comment['text'],
  118. 'timestamp': parse_iso8601(comment.get('created_at')),
  119. } for comment in video_data.get('comments', {}).get('data', [])]
  120. return {
  121. 'id': video_id,
  122. 'formats': formats,
  123. 'title': video_data['caption'],
  124. 'description': video_data.get('description'),
  125. 'thumbnail': video_data.get('thumbnail_url'),
  126. 'duration': float_or_none(video_data.get('duration'), 1000),
  127. 'timestamp': parse_iso8601(video_data.get('publish_at')),
  128. 'like_count': video_data.get('likes', {}).get('total'),
  129. 'comment_count': video_data.get('comments', {}).get('total'),
  130. 'view_count': video_data.get('views'),
  131. 'comments': comments,
  132. }
  133. def _extract_stream_info(self, stream_info):
  134. broadcast_id = stream_info['broadcast_id']
  135. is_live = stream_info.get('is_live')
  136. formats = []
  137. smil_url = stream_info.get('play_url')
  138. if smil_url:
  139. smil_formats = self._extract_smil_formats(smil_url, broadcast_id)
  140. if smil_formats:
  141. formats.extend(smil_formats)
  142. m3u8_url = stream_info.get('m3u8_url')
  143. if m3u8_url:
  144. m3u8_formats = self._extract_m3u8_formats(
  145. m3u8_url, broadcast_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
  146. if m3u8_formats:
  147. formats.extend(m3u8_formats)
  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(info_url, event_id, 'Downloading page {0}'.format(i))['data']
  178. videos_info = [v['data'] for v in videos_info if v['type'] == 'video']
  179. if not videos_info:
  180. break
  181. for v in videos_info:
  182. entries.append(self.url_result(
  183. 'http://livestream.com/accounts/%s/events/%s/videos/%s' % (account_id, event_id, v['id']),
  184. 'Livestream', v['id'], v['caption']))
  185. last_video = videos_info[-1]['id']
  186. return self.playlist_result(entries, event_id, event_data['full_name'])
  187. def _real_extract(self, url):
  188. mobj = re.match(self._VALID_URL, url)
  189. video_id = mobj.group('id')
  190. event = mobj.group('event_id') or mobj.group('event_name')
  191. account = mobj.group('account_id') or mobj.group('account_name')
  192. api_url = self._API_URL_TEMPLATE % (account, event)
  193. if video_id:
  194. video_data = self._download_json(api_url + '/videos/%s' % video_id, video_id)
  195. return self._extract_video_info(video_data)
  196. else:
  197. event_data = self._download_json(api_url, video_id)
  198. return self._extract_event(event_data)
  199. # The original version of Livestream uses a different system
  200. class LivestreamOriginalIE(InfoExtractor):
  201. IE_NAME = 'livestream:original'
  202. _VALID_URL = r'''(?x)https?://original\.livestream\.com/
  203. (?P<user>[^/]+)/(?P<type>video|folder)
  204. (?:\?.*?Id=|/)(?P<id>.*?)(&|$)
  205. '''
  206. _TESTS = [{
  207. 'url': 'http://original.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  208. 'info_dict': {
  209. 'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
  210. 'ext': 'mp4',
  211. 'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
  212. 'duration': 771.301,
  213. 'view_count': int,
  214. },
  215. }, {
  216. 'url': 'https://original.livestream.com/newplay/folder?dirId=a07bf706-d0e4-4e75-a747-b021d84f2fd3',
  217. 'info_dict': {
  218. 'id': 'a07bf706-d0e4-4e75-a747-b021d84f2fd3',
  219. },
  220. 'playlist_mincount': 4,
  221. }]
  222. def _extract_video(self, user, video_id):
  223. api_url = 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user, video_id)
  224. info = self._download_xml(api_url, video_id)
  225. # this url is used on mobile devices
  226. stream_url = 'http://x{0}x.api.channel.livestream.com/3.0/getstream.json?id={1}'.format(user, video_id)
  227. stream_info = self._download_json(stream_url, video_id)
  228. is_live = stream_info.get('isLive')
  229. item = info.find('channel').find('item')
  230. media_ns = {'media': 'http://search.yahoo.com/mrss'}
  231. thumbnail_url = xpath_attr(item, xpath_with_ns('media:thumbnail', media_ns), 'url')
  232. duration = float_or_none(xpath_attr(item, xpath_with_ns('media:content', media_ns), 'duration'))
  233. ls_ns = {'ls': 'http://api.channel.livestream.com/2.0'}
  234. view_count = int_or_none(xpath_text(item, xpath_with_ns('ls:viewsCount', ls_ns)))
  235. formats = [{
  236. 'url': stream_info['progressiveUrl'],
  237. 'format_id': 'http',
  238. }]
  239. m3u8_url = stream_info.get('httpUrl')
  240. if m3u8_url:
  241. m3u8_formats = self._extract_m3u8_formats(
  242. m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
  243. if m3u8_formats:
  244. formats.extend(m3u8_formats)
  245. rtsp_url = stream_info.get('rtspUrl')
  246. if rtsp_url:
  247. formats.append({
  248. 'url': rtsp_url,
  249. 'format_id': 'rtsp',
  250. })
  251. self._sort_formats(formats)
  252. return {
  253. 'id': video_id,
  254. 'title': self._live_title(xpath_text(item, 'title')) if is_live else xpath_text(item, 'title'),
  255. 'formats': formats,
  256. 'thumbnail': thumbnail_url,
  257. 'duration': duration,
  258. 'view_count': view_count,
  259. 'is_live': is_live,
  260. }
  261. def _extract_folder(self, url, folder_id):
  262. webpage = self._download_webpage(url, folder_id)
  263. paths = orderedSet(re.findall(
  264. r'''(?x)(?:
  265. <li\s+class="folder">\s*<a\s+href="|
  266. <a\s+href="(?=https?://livestre\.am/)
  267. )([^"]+)"''', webpage))
  268. entries = [{
  269. '_type': 'url',
  270. 'url': compat_urlparse.urljoin(url, p),
  271. } for p in paths]
  272. return self.playlist_result(entries, folder_id)
  273. def _real_extract(self, url):
  274. mobj = re.match(self._VALID_URL, url)
  275. id = mobj.group('id')
  276. user = mobj.group('user')
  277. url_type = mobj.group('type')
  278. if url_type == 'folder':
  279. return self._extract_folder(url, id)
  280. else:
  281. return self._extract_video(user, id)
  282. # The server doesn't support HEAD request, the generic extractor can't detect
  283. # the redirection
  284. class LivestreamShortenerIE(InfoExtractor):
  285. IE_NAME = 'livestream:shortener'
  286. IE_DESC = False # Do not list
  287. _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
  288. def _real_extract(self, url):
  289. mobj = re.match(self._VALID_URL, url)
  290. id = mobj.group('id')
  291. webpage = self._download_webpage(url, id)
  292. return {
  293. '_type': 'url',
  294. 'url': self._og_search_url(webpage),
  295. }