1 from __future__ 
import unicode_literals
 
   7 from .common 
import InfoExtractor
 
  10     compat_urllib_parse_urlparse
, 
  22 class LivestreamIE(InfoExtractor
): 
  23     IE_NAME 
= 'livestream' 
  24     _VALID_URL 
= r
'https?://(?:new\.)?livestream\.com/.*?/(?P<event_name>.*?)(/videos/(?P<id>[0-9]+)(?:/player)?)?/?(?:$|[?#])' 
  26         'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370', 
  27         'md5': '53274c76ba7754fb0e8d072716f2292b', 
  31             'title': 'Live from Webster Hall NYC', 
  32             'upload_date': '20121012', 
  35             'thumbnail': 're:^http://.*\.jpg$' 
  38         'url': 'http://new.livestream.com/tedx/cityenglish', 
  40             'title': 'TEDCity2.0 (English)', 
  43         'playlist_mincount': 4, 
  45         'url': 'http://new.livestream.com/chess24/tatasteelchess', 
  47             'title': 'Tata Steel Chess', 
  50         'playlist_mincount': 60, 
  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, 
  55         'url': 'http://livestream.com/bsww/concacafbeachsoccercampeonato2015', 
  56         'only_matching': True, 
  59     def _parse_smil(self
, video_id
, smil_url
): 
  62             './/{http://www.w3.org/2001/SMIL20/Language}body/' 
  63             '{http://www.w3.org/2001/SMIL20/Language}switch') 
  64         smil_doc 
= self
._download
_xml
( 
  66             note
='Downloading SMIL information', 
  67             errnote
='Unable to download SMIL information', 
  69         if smil_doc 
is False:  # Download failed 
  71         title_node 
= find_xpath_attr( 
  72             smil_doc
, './/{http://www.w3.org/2001/SMIL20/Language}meta', 
  74         if title_node 
is None: 
  75             self
.report_warning('Cannot find SMIL id') 
  76             switch_node 
= smil_doc
.find(_SWITCH_XPATH
) 
  78             title_id 
= title_node
.attrib
['content'] 
  79             switch_node 
= find_xpath_attr( 
  80                 smil_doc
, _SWITCH_XPATH
, 'id', title_id
) 
  81         if switch_node 
is None: 
  82             raise ExtractorError('Cannot find switch node') 
  83         video_nodes 
= switch_node
.findall( 
  84             '{http://www.w3.org/2001/SMIL20/Language}video') 
  86         for vn 
in video_nodes
: 
  87             tbr 
= int_or_none(vn
.attrib
.get('system-bitrate')) 
  89                 'http://livestream-f.akamaihd.net/%s?v=3.0.3&fp=WIN%%2014,0,0,145' % 
  91             if 'clipBegin' in vn
.attrib
: 
  92                 furl 
+= '&ssek=' + vn
.attrib
['clipBegin'] 
  95                 'format_id': 'smil_%d' % tbr
, 
 102     def _extract_video_info(self
, video_data
): 
 103         video_id 
= compat_str(video_data
['id']) 
 106             ('sd', 'progressive_url'), 
 107             ('hd', 'progressive_url_hd'), 
 110             'format_id': format_id
, 
 111             'url': video_data
[key
], 
 113         } for i
, (format_id
, key
) in enumerate(FORMAT_KEYS
) 
 114             if video_data
.get(key
)] 
 116         smil_url 
= video_data
.get('smil_url') 
 118             formats
.extend(self
._parse
_smil
(video_id
, smil_url
)) 
 119         self
._sort
_formats
(formats
) 
 124             'title': video_data
['caption'], 
 125             'thumbnail': video_data
.get('thumbnail_url'), 
 126             'upload_date': video_data
['updated_at'].replace('-', '')[:8], 
 127             'like_count': video_data
.get('likes', {}).get('total'), 
 128             'view_count': video_data
.get('views'), 
 131     def _extract_event(self
, info
): 
 132         event_id 
= compat_str(info
['id']) 
 133         account 
= compat_str(info
['owner_account_id']) 
 135             'https://new.livestream.com/api/accounts/{account}/events/{event}/' 
 136             'feed.json'.format(account
=account
, event
=event_id
)) 
 138         def _extract_videos(): 
 140             for i 
in itertools
.count(1): 
 141                 if last_video 
is None: 
 144                     info_url 
= '{root}?&id={id}&newer=-1&type=video'.format( 
 145                         root
=root_url
, id=last_video
) 
 146                 videos_info 
= self
._download
_json
(info_url
, event_id
, 'Downloading page {0}'.format(i
))['data'] 
 147                 videos_info 
= [v
['data'] for v 
in videos_info 
if v
['type'] == 'video'] 
 150                 for v 
in videos_info
: 
 151                     yield self
._extract
_video
_info
(v
) 
 152                 last_video 
= videos_info
[-1]['id'] 
 153         return self
.playlist_result(_extract_videos(), event_id
, info
['full_name']) 
 155     def _real_extract(self
, url
): 
 156         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 157         video_id 
= mobj
.group('id') 
 158         event_name 
= mobj
.group('event_name') 
 159         webpage 
= self
._download
_webpage
(url
, video_id 
or event_name
) 
 161         og_video 
= self
._og
_search
_video
_url
( 
 162             webpage
, 'player url', fatal
=False, default
=None) 
 163         if og_video 
is not None: 
 164             query_str 
= compat_urllib_parse_urlparse(og_video
).query
 
 165             query 
= compat_urlparse
.parse_qs(query_str
) 
 166             if 'play_url' in query
: 
 167                 api_url 
= query
['play_url'][0].replace('.smil', '') 
 168                 info 
= json
.loads(self
._download
_webpage
( 
 169                     api_url
, video_id
, 'Downloading video info')) 
 170                 return self
._extract
_video
_info
(info
) 
 172         config_json 
= self
._search
_regex
( 
 173             r
'window.config = ({.*?});', webpage
, 'window config') 
 174         info 
= json
.loads(config_json
)['event'] 
 176         def is_relevant(vdata
, vid
): 
 177             result 
= vdata
['type'] == 'video' 
 178             if video_id 
is not None: 
 179                 result 
= result 
and compat_str(vdata
['data']['id']) == vid
 
 183             # This is an event page: 
 184             return self
._extract
_event
(info
) 
 186             videos 
= [self
._extract
_video
_info
(video_data
['data']) 
 187                       for video_data 
in info
['feed']['data'] 
 188                       if is_relevant(video_data
, video_id
)] 
 190                 raise ExtractorError('Cannot find video %s' % video_id
) 
 194 # The original version of Livestream uses a different system 
 195 class LivestreamOriginalIE(InfoExtractor
): 
 196     IE_NAME 
= 'livestream:original' 
 197     _VALID_URL 
= r
'''(?x)https?://original\.livestream\.com/ 
 198         (?P<user>[^/]+)/(?P<type>video|folder) 
 199         (?:\?.*?Id=|/)(?P<id>.*?)(&|$) 
 202         'url': 'http://original.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb', 
 204             'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb', 
 206             'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital', 
 209         'url': 'https://original.livestream.com/newplay/folder?dirId=a07bf706-d0e4-4e75-a747-b021d84f2fd3', 
 211             'id': 'a07bf706-d0e4-4e75-a747-b021d84f2fd3', 
 213         'playlist_mincount': 4, 
 216     def _extract_video(self
, user
, video_id
): 
 217         api_url 
= 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user
, video_id
) 
 219         info 
= self
._download
_xml
(api_url
, video_id
) 
 220         # this url is used on mobile devices 
 221         stream_url 
= 'http://x{0}x.api.channel.livestream.com/3.0/getstream.json?id={1}'.format(user
, video_id
) 
 222         stream_info 
= self
._download
_json
(stream_url
, video_id
) 
 223         item 
= info
.find('channel').find('item') 
 224         ns 
= {'media': 'http://search.yahoo.com/mrss'} 
 225         thumbnail_url 
= item
.find(xpath_with_ns('media:thumbnail', ns
)).attrib
['url'] 
 229             'title': item
.find('title').text
, 
 230             'url': stream_info
['progressiveUrl'], 
 231             'thumbnail': thumbnail_url
, 
 234     def _extract_folder(self
, url
, folder_id
): 
 235         webpage 
= self
._download
_webpage
(url
, folder_id
) 
 236         paths 
= orderedSet(re
.findall( 
 238                 <li\s+class="folder">\s*<a\s+href="| 
 239                 <a\s+href="(?=https?://livestre\.am/) 
 240             )([^"]+)"''', webpage
)) 
 247                 'url': compat_urlparse
.urljoin(url
, p
), 
 251     def _real_extract(self
, url
): 
 252         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 253         id = mobj
.group('id') 
 254         user 
= mobj
.group('user') 
 255         url_type 
= mobj
.group('type') 
 256         if url_type 
== 'folder': 
 257             return self
._extract
_folder
(url
, id) 
 259             return self
._extract
_video
(user
, id) 
 262 # The server doesn't support HEAD request, the generic extractor can't detect 
 264 class LivestreamShortenerIE(InfoExtractor
): 
 265     IE_NAME 
= 'livestream:shortener' 
 266     IE_DESC 
= False  # Do not list 
 267     _VALID_URL 
= r
'https?://livestre\.am/(?P<id>.+)' 
 269     def _real_extract(self
, url
): 
 270         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 271         id = mobj
.group('id') 
 272         webpage 
= self
._download
_webpage
(url
, id) 
 276             'url': self
._og
_search
_url
(webpage
),