Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[86c074]: plugin.video.si / default.py Maximize Restore History

Download this file

default.py    413 lines (336 with data), 15.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# -*- coding: utf-8 -*-
# Sports Illustrated
import sys
import httplib
import urllib, urllib2, cookielib, datetime, time, re, os
import xbmcplugin, xbmcgui, xbmcaddon, xbmcvfs
import cgi, gzip
from StringIO import StringIO
import pyamf
from pyamf import remoting
CONST = '84c401e577ddd24ca827eab0184302b8281e8b51'
USER_AGENT = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5'
GENRE_SPORTS = "Sports"
UTF8 = 'utf-8'
addon = xbmcaddon.Addon('plugin.video.si')
__addonname__ = addon.getAddonInfo('name')
__language__ = addon.getLocalizedString
home = addon.getAddonInfo('path').decode(UTF8)
icon = xbmc.translatePath(os.path.join(home, 'icon.png'))
fanart = xbmc.translatePath(os.path.join(home, 'fanart.jpg'))
def log(txt):
message = '%s: %s' % (__addonname__, txt.encode('ascii', 'ignore'))
xbmc.log(msg=message, level=xbmc.LOGDEBUG)
def _parse_argv():
global url,name,iconimage, mode, playlist,fchan,fres,fhost,fname,fepg,fanArt
params = {}
try:
params = dict( arg.split( "=" ) for arg in ((sys.argv[2][1:]).split( "&" )) )
except:
params = {}
url = demunge(params.get("url",None))
name = demunge(params.get("name",""))
iconimage = demunge(params.get("iconimage",""))
fanArt = demunge(params.get("fanart",""))
playlist = demunge(params.get("playlist",""))
fchan = demunge(params.get("fchan",""))
fres = demunge(params.get("fres",""))
fhost = demunge(params.get("fhost",""))
fname = demunge(params.get("fname",""))
fepg = demunge(params.get("fepg",None))
try:
playlist=eval(playlist.replace('|',','))
except:
pass
try:
mode = int(params.get( "mode", None ))
except:
mode = None
def demunge(munge):
try:
munge = urllib.unquote_plus(munge).decode(UTF8)
except:
pass
return munge
def getRequest(url):
log("getRequest URL:"+str(url))
headers = {'User-Agent':USER_AGENT, 'Accept':"text/html", 'Accept-Encoding':'gzip,deflate,sdch', 'Accept-Language':'en-US,en;q=0.8', 'Cookie':'hide_ce=true'}
req = urllib2.Request(url.encode(UTF8), None, headers)
try:
response = urllib2.urlopen(req)
if response.info().getheader('Content-Encoding') == 'gzip':
log("Content Encoding == gzip")
buf = StringIO( response.read())
f = gzip.GzipFile(fileobj=buf)
link1 = f.read()
else:
link1=response.read()
except:
link1 = ""
link1 = str(link1).replace('\n','')
return(link1)
def getSources():
Choices = [["Most Recent","","SI"],
["NFL","nfl",""],
["College Football","ncaaf_video",""],
["MLB","mlb",""],
["NBA","nba",""],
["College Basketball","ncaab_video",""],
["Golf","golf",""],
["NHL","nhl",""],
["NASCAR","nascar",""],
["Soccer","soccer",""],
["MMA & Boxing","boxing",""],
["Tennis","tennis",""],
["More Sports","video",""],
["Swim Daily","swimdaily",""],
["Game Room","gameroom",""],
["Fantasy","fantasy",""],
["High School","highschool",""],
[" SI Now","si_now_fullshow","SI"],
[" Pro Football Now","profootballnow_fullshow","SI"]]
for pname, pcode, pchoice in Choices:
addDir(pname,pchoice+'#'+pcode,21,icon,fanart,pname,GENRE_SPORTS,"",False)
'''
[" Moments of Glory","moments_of_glory","SI"],
[" Fresh Takes","ncaab.video_all_fresh_takes","SI"],
[" The Press","this_week_in_sports","SI"]]
'''
def getCategory(Category_url):
log("main page")
pchoice, pcode = Category_url.split('#')
if pchoice == "SI":
Category_url = "http://sportsillustrated.cnn.com/.element/auto/4.1/video/page/si_"+pcode+"_video_page.json?format=jsonp&callback=siVideoPage.load"
link = getRequest(Category_url)
match = re.compile('\{.+?"brightcoveId":"(.+?)".+?"createdDate":"(.+?)".+?"slug":"(.+?)".+?"headline":"(.+?)".+?"description":"(.+?)".+?"images":\["(.+?)".+?\}').findall(link)
for pid, pdate, pslug, pname, pdesc, pimage in match:
if (not ('-clip' in pslug)):
ts = int(pdate)
pdesc = datetime.datetime.fromtimestamp(ts).strftime('%a %b %d, %Y %H:%M')+'\n'+pdesc
caturl = "plugin://plugin.video.si/?url=#"+str(pid)+"&mode=20"
try:
addLink(caturl.encode(UTF8),pname,pimage,fanart,pdesc,GENRE_SPORTS,"")
except:
log("Problem adding directory")
else:
Category_url = "http://api.brightcove.com/services/library?callback=jQuery17209430150024127215_"+str(int(round(time.time() * 1000)))+"&command=search_videos&any=primarycategory%3A"+pcode+"&page_size=100&video_fields=id%2CshortDescription%2CcreationDate%2CthumbnailURL%2Clength%2Cname&custom_fields=primarycategory%2Csubcategory&sort_by=PUBLISH_DATE%3ADESC&get_item_count=true&token=HYk6klcc_dX8GkFqbW1C2tZHLqgLDxGWBMlica9EroqvNv-skogPlw..&format=json"
link = getRequest(Category_url)
match = re.compile('\{"id":(.+?),"name":"(.+?)".+?"shortDescription":"(.+?)".+?"creationDate":"(.+?)".+?"thumbnailURL":"(.+?)"').findall(link)
for pid, pname, pdesc, pdate, pimage in match:
ts = int(int(str(int(pdate)))/1000)
pdesc = datetime.datetime.fromtimestamp(ts).strftime('%a %b %d, %Y %H:%M')+'\n'+pdesc
pimage = pimage.replace('\\','')
caturl = "plugin://plugin.video.si/?url=#"+str(pid)+"&mode=20"
try:
addLink(caturl.encode(UTF8),pname,pimage,fanart,pdesc,GENRE_SPORTS,"")
except:
log("Problem adding directory")
def play_playlist(name, list):
playlist = xbmc.PlayList(1)
playlist.clear()
item = 0
for i in list:
item += 1
info = xbmcgui.ListItem('%s) %s' %(str(item),name))
playlist.add(i, info)
xbmc.executebuiltin('playlist.playoffset(video,0)')
def addDir(name,url,mode,iconimage,fanart,description,genre,date,showcontext=True,playlist=None,autoplay=False):
u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)
dir_playable = False
if mode != 12:
u += "&name="+urllib.quote_plus(name)+"&fanart="+urllib.quote_plus(fanart)
dir_image = "DefaultFolder.png"
dir_folder = True
else:
dir_image = "DefaultVideo.png"
dir_folder = False
dir_playable = True
ok=True
liz=xbmcgui.ListItem(name, iconImage=dir_image, thumbnailImage=iconimage)
liz.setInfo( type="Video", infoLabels={ "Title": name, "Plot": description, "Genre": genre, "Year": date } )
liz.setProperty( "Fanart_Image", fanart )
if dir_playable == True:
liz.setProperty('IsPlayable', 'true')
if not playlist is None:
playlist_name = name.split(') ')[1]
contextMenu_ = [('Play '+playlist_name+' PlayList','XBMC.RunPlugin(%s?mode=13&name=%s&playlist=%s)' %(sys.argv[0], urllib.quote_plus(playlist_name), urllib.quote_plus(str(playlist).replace(',','|'))))]
liz.addContextMenuItems(contextMenu_)
if autoplay == True:
xbmc.PlayList(1).add(u, liz)
else:
ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=dir_folder)
return ok
def addLink(url,name,iconimage,fanart,description,genre,date,showcontext=True,playlist=None, autoplay=False):
return addDir(name,url,12,iconimage,fanart,description,genre,date,showcontext,playlist,autoplay)
def play_video(video_url):
video_player_key = "AQ~~,AAAB9mw57HE~,xU4DCdZtHhuasNZF5WPK5LWKKRK4p1HG"
page_url = "http://sportsillustrated.cnn.com/video/"
video_player_id = "2546892348001"
publisher_id = "2157889318001"
video_url, video_content_id = video_url.split('#')
swf_url = get_swf_url("myExperience",video_player_id,publisher_id,str(video_content_id))
renditions = get_episode_info(video_player_key,str(video_content_id),page_url,video_player_id)
finalurl = renditions['programmedContent']['videoPlayer']['mediaDTO']['FLVFullLengthURL']
# use 'IOSRenditions' in place of 'renditions' in below for .m3u8 list, note case of 'r' in renditions, using 'renditions' gives you rtmp links
stored_size=stored_height = 0
for item in sorted(renditions['programmedContent']['videoPlayer']['mediaDTO']['renditions'], key = lambda item:item['frameHeight'], reverse = False):
stream_size = item['size']
stream_height = item['frameHeight']
if (int(stream_size) > stored_size):
finalurl = item['defaultURL']
stored_size = stream_size
stored_height = stream_height
#this is a kludge because I can't get some rtmps to play, so use the IOS .m3u8 list if it exists (the ones with IOSRenditions don't play rtmp correctly)
if (stored_height == 720) and (addon.getSetting('vid_res') == "1") and ("&mp4:23/" in finalurl):
match = re.compile('&mp4:(.+?)\?').findall(finalurl)
for x in match:
finalurl = "http://brightcove04.brightcove.com/"+x
else:
stored_size = 0
for item in sorted(renditions['programmedContent']['videoPlayer']['mediaDTO']['IOSRenditions'], key = lambda item:item['frameHeight'], reverse = False):
stream_size = item['size']
if (int(stream_size) > stored_size):
finalurl = item['defaultURL']
stored_size = stream_size
# finalurl = finalurl.replace('.mp4?','.mp4&') # this needs work where the app, playpath, wierdqs split doesn't work right below
if "rtmp:" in finalurl:
app, playpath, wierdqs = finalurl.split("&", 2)
qs = "?videoId=%s&lineUpId=&pubId=%s&playerId=%s&affiliateId=" % (video_content_id, publisher_id, video_player_id)
scheme,netloc = app.split("://")
netloc, app = netloc.split("/",1)
app = app.rstrip("/") + qs
log("APP:%s" %(app,))
tcurl = "%s://%s:1935/%s" % (scheme, netloc, app)
log("TCURL:%s" % (tcurl,))
finalurl = "%s tcUrl=%s app=%s playpath=%s%s swfUrl=%s conn=B:0 conn=S:%s&%s" % (tcurl,tcurl, app, playpath, qs, swf_url, playpath, wierdqs)
log("final rtmp: url =%s" % (finalurl,))
xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, xbmcgui.ListItem(path = finalurl))
def get_episode_info(video_player_key, video_content_id, video_url, video_player_id):
envelope = build_amf_request(video_player_key, video_content_id, video_url, video_player_id)
connection_url = "http://c.brightcove.com/services/messagebroker/amf?playerKey=" + video_player_key
values = bytes(remoting.encode(envelope).read())
header = {'Content-Type' : 'application/x-amf'}
response = remoting.decode(getURL(connection_url, values, header, amf = True)).bodies[0][1].body
log("Episode Info response: %s" % (response,))
return response
def getURL(url, values = None, header = {}, amf = False, cookieinfo = False):
try:
if values is None:
req = urllib2.Request(bytes(url))
else:
if amf == False:
data = urllib.urlencode(values)
elif amf == True:
data = values
req = urllib2.Request(bytes(url), data)
header.update({'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0'})
for key, value in header.iteritems():
req.add_header(key, value)
response = urllib2.urlopen(req)
link = response.read()
cookie = response.info()
response.close()
except urllib2.HTTPError, error:
log("HTTP Error reason: %s" % (error,))
return error.read()
else:
if cookieinfo is True:
return link, cookie
else:
return link
class ViewerExperienceRequest(object):
def __init__(self, URL, contentOverrides, experienceId, playerKey, TTLToken = ''):
self.TTLToken = TTLToken
self.URL = URL
self.deliveryType = float(0)
self.contentOverrides = contentOverrides
self.experienceId = experienceId
self.playerKey = playerKey
class ContentOverride(object):
def __init__(self, contentId, contentType = 0, target = 'videoPlayer'):
self.contentType = contentType
self.contentId = contentId
self.target = target
self.contentIds = None
self.contentRefId = None
self.contentRefIds = None
self.contentType = 0
self.featureId = float(0)
self.featuredRefId = None
def build_amf_request(video_player_key, video_content_id, video_url, video_player_id):
pyamf.register_class(ViewerExperienceRequest, 'com.brightcove.experience.ViewerExperienceRequest')
pyamf.register_class(ContentOverride, 'com.brightcove.experience.ContentOverride')
content_override = ContentOverride(int(video_content_id))
viewer_exp_req = ViewerExperienceRequest(video_url, [content_override], int(video_player_id), video_player_key)
env = remoting.Envelope(amfVersion=3)
env.bodies.append(
(
"/1",
remoting.Request(
target = "com.brightcove.experience.ExperienceRuntimeFacade.getDataForExperience",
body = [CONST, viewer_exp_req],
envelope = env
)
)
)
return env
def get_swf_url(flash_experience_id, player_id, publisher_id, video_id):
conn = httplib.HTTPConnection('c.brightcove.com')
qsdata = dict(width=640, height=480, flashID=flash_experience_id,
bgcolor="#000000", playerID=player_id, publisherID=publisher_id,
isSlim='true', wmode='opaque', optimizedContentLoad='true', autoStart='', debuggerID='')
qsdata['@videoPlayer'] = video_id
log("SWFURL: %s" % (urllib.urlencode(qsdata),))
conn.request("GET", "/services/viewer/federated_f9?&" + urllib.urlencode(qsdata))
resp = conn.getresponse()
location = resp.getheader('location')
base = location.split("?",1)[0]
location = base.replace("BrightcoveBootloader.swf", "federatedVideo/BrightcovePlayer.swf")
return location
# MAIN EVENT PROCESSING STARTS HERE
xbmcplugin.setContent(int(sys.argv[1]), 'movies')
try:
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_UNSORTED)
except:
pass
try:
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_LABEL)
except:
pass
try:
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_DATE)
except:
pass
try:
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_GENRE)
except:
pass
url=name=iconimage=mode=playlist=fchan=fres=fhost=fname=fepg=None
_parse_argv()
log("Mode: "+str(mode))
if not url is None:
try:
log("URL: "+str(url.encode(UTF8)))
except:
pass
try:
log("Name: "+str(name))
except:
pass
auto_play = False
if mode==None:
log("getSources")
getSources()
elif mode==12:
log("setResolvedUrl")
item = xbmcgui.ListItem(path=url)
xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, item)
elif mode==13:
log("play_playlist")
play_playlist(name, playlist)
elif mode==20:
play_video(url)
elif mode==21:
getCategory(url)
xbmcplugin.endOfDirectory(int(sys.argv[1]))
sys.modules.clear()