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

[018c0b]: plugin.image.facebook / default.py Maximize Restore History

Download this file

default.py    449 lines (391 with data), 15.2 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/python
import facebook
import urllib, sys, os
from addon import AddonHelper
__plugin__ = 'facebook'
__author__ = 'ruuk'
__url__ = 'http://code.google.com/p/facebookphotos-xbmc/'
__date__ = '01-18-2011'
__version__ = '0.9.7'
def LOG(msg):
try:
print 'FACEBOOKPHOTOS: ' + msg.decode('utf-8','ignore').encode('ascii','replace')
except:
fb.xbmc().log('FACEBOOKPHOTOS: ' + msg.decode('utf-8','ignore').encode('ascii','replace'))
def ERROR(message):
errtext = sys.exc_info()[1]
message = 'ERROR - %s::%s (%d) - %s' % (message,sys.exc_info()[2].tb_frame.f_code.co_name, sys.exc_info()[2].tb_lineno, errtext)
LOG(message)
return str(errtext)
class GraphWrapAuthError(Exception):
def __init__(self, type, message):
Exception.__init__(self, message)
self.type = type
self.message = message
class GraphWrap(facebook.GraphAPI):
def setLogin(self,email,passw):
self.login_email = email
self.login_pass = passw
def setAppData(self,aid,redirect='http://www.facebook.com/connect/login_success.html',scope=None):
self.client_id = aid
self.redirect = redirect
self.scope = scope
def checkHasPermission(self,permission):
import simplejson
url = 'https://api.facebook.com/method/users.hasAppPermission?format=json&ext_perm='+permission+'&access_token='+self.access_token
fobj = urllib.urlopen(url)
try:
response = simplejson.loads(fobj.read())
finally:
fobj.close()
return (response == 1)
def checkIsAppUser(self):
import simplejson
url = 'https://api.facebook.com/method/users.isAppUser?format=json&access_token='+self.access_token
fobj = urllib.urlopen(url)
try:
response = simplejson.loads(fobj.read())
finally:
fobj.close()
return response
def getConnections(self, id, connection_name, **args):
fail = False
try:
return self.get_connections(id, connection_name, **args)
except facebook.GraphAPIError,e:
print e.type
if not e.type == 'OAuthException': raise
fail = True
if fail:
if not self.getNewToken():
if self.access_token: raise GraphWrapAuthError('RENEW_TOKEN_FAILURE','Failed to get new token')
else: return None
return self.get_connections(id, connection_name, **args)
def getObject(self, id, **args):
fail = False
try:
return self.get_object(id, **args)
except facebook.GraphAPIError,e:
if not e.type == 'OAuthException': raise
fail = True
if fail:
if not self.getNewToken():
if self.access_token: raise GraphWrapAuthError('RENEW_TOKEN_FAILURE','Failed to get new token')
else: return None
return self.get_object(id, **args)
def getNewToken(self):
from webviewer import mechanize #@UnresolvedImport
br = mechanize.Browser()
br.set_handle_robots(False)
scope = ''
if self.scope: scope = '&scope=' + self.scope
url = 'https://graph.facebook.com/oauth/authorize?client_id='+self.client_id+\
'&redirect_uri='+self.redirect+\
'&type=user_agent&display=popup'+scope
print url
try:
res = br.open(url)
html = res.read()
except:
print "ERROR: TOKEN PAGE INITIAL READ"
raise
script = False
try:
#check for login form
br.select_form(nr=0)
print "HTML"
except:
self.genericError()
script = True
print "SCRIPT"
if script:
#no form, maybe we're logged in and the token is in javascript on the page
token = self.parseTokenFromScript(html)
else:
try:
#fill out the form and submit
br['email'] = self.login_email
br['pass'] = self.login_pass
res = br.submit()
url = res.geturl()
print "FORM"
except:
print "FORM ERROR"
raise
script = False
token = self.extractTokenFromURL(url)
if not token: script = True
if script:
print "SCRIPT TOKEN"
#no token in the url, let's try to parse it from javascript on the page
html = res.read()
token = self.parseTokenFromScript(html)
if not self.tokenIsValid(token):
#if script: LOG("HTML:" + html)
return False
self.access_token = token
self.saveToken()
return True
def extractTokenFromURL(self,url):
try:
#we submitted the form, check the result url for the access token
from cgi import parse_qs
import urlparse
token = parse_qs(urlparse.urlparse(url.replace('#','?',1))[4])['access_token'][0]
print "URL TOKEN: %s" % token
return token
except:
self.genericError()
return None
def tokenIsValid(self,token):
if not token: return False
if 'login_form' in token and 'standard_explanation' in token:
reason = self.re().findall('id="standard_explanation">(?:<p>)?([^<]*)<',token)
if reason: print reason[0]
LOG("TOKEN: " + token)
raise GraphWrapAuthError('LOGIN_FAILURE',reason)
return False
if 'html' in token or 'script' in token or len(token) > 160:
LOG("TOKEN: " + token)
raise GraphWrapAuthError('RENEW_TOKEN_FAILURE','Failed to get new token')
return False
return True
def genericError(self):
print 'ERROR: %s::%s (%d) - %s' % (self.__class__.__name__
, sys.exc_info()[2].tb_frame.f_code.co_name, sys.exc_info()[2].tb_lineno, sys.exc_info()[1])
def parseTokenFromScript(self,html):
return urllib.unquote_plus(html.split("#access_token=")[-1].split("&expires")[0])
def saveToken(self,token=None):
if token: self.access_token = token
f = open(fb.TOKEN_PATH,'w')
f.write(self.access_token)
f.close()
class facebookSession(AddonHelper):
def __init__(self):
AddonHelper.__init__(self,'plugin.image.facebook')
self.IMAGES_PATH = self.addonPath('resources/images')
self.CACHE_PATH = self.dataPath('cache')
if not os.path.exists(self.CACHE_PATH): os.makedirs(self.CACHE_PATH)
self.TOKEN_PATH = self.dataPath('token')
self.get_friends_photos = (self.getSetting('get_friends_photos') == "true")
self.get_album_photos = (self.getSetting('get_album_photos') == "true")
self.loadToken()
self.graph = GraphWrap(self.token)
self.setLoginData()
self.graph.setAppData('150505371652086',scope='user_photos,friends_photos,user_photo_video_tags,friends_photo_video_tags')
self.urlprx = None
self.proxy = None
def getToken(self):
if self.graph.getNewToken():
self.token = self.graph.access_token
def setLoginData(self):
login_email = self.getSetting('login_email')
login_pass = self.getSetting('login_pass')
self.graph.setLogin(login_email,login_pass)
self.hasLoginData = True
if not (login_email and login_pass): self.hasLoginData = False
def loadToken(self):
self.token = ''
if os.path.exists(self.TOKEN_PATH):
f = open(self.TOKEN_PATH,'r')
self.token = f.read()
f.close()
def CATEGORIES(self):
self.addDir(self.lang(30004),os.path.join(self.IMAGES_PATH,'albums.png'),url='me',mode=1)
self.addDir(self.lang(30010),os.path.join(self.IMAGES_PATH,'videos.png'),url='me',mode=3)
self.addDir(self.lang(30005),os.path.join(self.IMAGES_PATH,'friends.png'),url='friends',mode=2)
self.addDir(self.lang(30006),os.path.join(self.IMAGES_PATH,'photosofme.png'),url='me',mode=101)
self.addDir(self.lang(30011),os.path.join(self.IMAGES_PATH,'videosofme.png'),url='me',mode=102)
def ALBUMS(self,uid='me',name=''):
albums = self.graph.getConnections(uid,'albums')
tot = len(albums['data'])
for a in albums['data']:
aid = a.get('id','')
fbase = self.binascii().hexlify(aid.encode('utf-8'))
fn = os.path.join(self.CACHE_PATH,fbase + '.jpg') #still works even if image is not jpg - doesn't work without the extension
tn = "https://graph.facebook.com/"+aid+"/picture?access_token=" + self.graph.access_token
if not os.path.exists(fn):
if self.get_album_photos: fn = self.getFile(tn,fn)
else: fn = ''
if not self.addDir(a.get('name',''),fn,url=aid,mode=101,tot=tot): break
if uid != 'me':
self.addDir( self.lang(30012).replace('@REPLACE@',name),
os.path.join(self.IMAGES_PATH,'videos.png'),
url=uid,
mode=3)
self.addDir(self.lang(30007).replace('@REPLACE@',name),os.path.join(self.IMAGES_PATH,'photosofme.png'),url=uid,mode=101)
self.addDir(self.lang(30013).replace('@REPLACE@',name),os.path.join(self.IMAGES_PATH,'videosofme.png'),url=uid,mode=102)
def FRIENDS(self):
friends = self.graph.getConnections('me','friends')
srt = []
show = {}
for f in friends['data']:
name = f.get('name','')
s = name.rsplit(' ',1)[-1] + name.rsplit(' ',1)[0]
srt.append(s)
show[s] = f
srt.sort()
tot = len(srt)
for s in srt:
uid = show[s].get('id','')
fbase = self.binascii().hexlify(uid.encode('utf-8'))
fn = os.path.join(self.CACHE_PATH,fbase + '.jpg') #still works even if image is not jpg - doesn't work without the extension
tn = "https://graph.facebook.com/"+uid+"/picture?type=large&access_token=" + self.graph.access_token
if not os.path.exists(fn):
if self.get_friends_photos:
try:
fn = self.getFile(tn,fn)
except:
fn = ''
else:
fn = ''
#fn = "https://graph.facebook.com/"+uid+"/picture?access_token=" + self.graph.access_token + "&nonsense=image.jpg" #<-- crashes XBMC
name = show[s].get('name','')
if not self.addDir(name,fn,url=uid,mode=1,tot=tot,name=name): break
def PHOTOS(self,aid,isPaging=False):
if isPaging:
photos = self.graph.request(aid)
else:
photos = self.graph.getConnections(aid,'photos')
tot = len(photos['data'])
paging = photos.get('paging')
next = None
if paging:
next = paging.get('next')
prev = paging.get('previous')
if self.areAlmostTheSame(prev,next):
prev = None
next = None
if prev:
if not self.addDir('<- %s' % self.lang(30014),os.path.join(self.IMAGES_PATH,'previous.png'),url=prev,mode=201): return
for p in photos['data']:
tn = p.get('picture','') + '?fix=' + str(self.time().time()) #why does this work? I have no idea. Why did I try it. I have no idea :)
#print "BEFORE: " + tn
tn = self.re().sub('/hphotos-\w+-\w+/\w+\.\w+/','/hphotos-ak-snc1/hs255.snc1/',tn) # this seems to get better results then using the random server
#print "-AFTER: " + tn
if not self.addLink(self.removeCRLF(p.get('name',p.get('id','None'))),p.get('source',''),tn,tot): return
if next:
if not self.addDir('%s ->' % self.lang(30015),os.path.join(self.IMAGES_PATH,'next.png'),url=next,mode=201): return
def VIDEOS(self,uid,uploaded=False,isPaging=False):
if isPaging:
videos = self.graph.request(uid)
else:
if uploaded: videos = self.graph.getConnections(uid,'videos/uploaded')
else: videos = self.graph.getConnections(uid,'videos')
tot = len(videos['data'])
paging = videos.get('paging')
next = None
if paging:
next = paging.get('next')
prev = paging.get('previous')
if self.areAlmostTheSame(prev,next):
prev = None
next = None
if prev:
if not self.addDir('<- %s' % self.lang(30014),os.path.join(self.IMAGES_PATH,'previous.png'),url=prev,mode=202): return
for v in videos['data']:
tn = v.get('picture','') + '?fix=' + str(self.time().time()) #why does this work? I have no idea. Why did I try it. I have no idea :)
if not self.addLink(self.removeCRLF(v.get('name',v.get('id','None'))),v.get('source',''),tn,tot): return
if next:
if not self.addDir('%s ->' % self.lang(30015),os.path.join(self.IMAGES_PATH,'next.png'),url=next,mode=202): return
def areAlmostTheSame(self,first,second):
if not first or not second: return False
first = self.re().sub('(\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d)\d(%2B\d{4})',r'\1x\2',first)
second = self.re().sub('(\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d)\d(%2B\d{4})',r'\1x\2',second)
return first == second
def removeCRLF(self,text):
return " ".join(text.split())
def makeAscii(self,name):
return name.encode('ascii','replace')
def doPlugin(self):
mode = self.getParamInt('mode',None)
url = self.getParamString('url',None)
name = self.getParamString('name',None)
update_dir = False
success = True
cache = True
if not self.hasLoginData:
self.openSettings()
self.setLoginData()
if not self.token:
self.xbmcgui().Dialog().ok(self.lang(30101),self.lang(30102),self.lang(30103),self.lang(30104))
self.xbmcplugin().endOfDirectory(int(sys.argv[1]),succeeded=success,updateListing=update_dir,cacheToDisc=cache)
token = getAuth()
if not token: self.getToken()
return
if not self.hasLoginData:
self.xbmcgui().Dialog().ok(self.lang(30105),self.lang(30106),self.lang(30107))
success = False
elif not self.token:
self.xbmcgui().Dialog().ok(self.lang(30108),self.lang(30109),self.lang(30110))
success = False
elif mode==None or url==None or len(url)<1:
self.CATEGORIES()
elif mode==1:
self.ALBUMS(url,name)
elif mode==2:
self.FRIENDS()
elif mode==3:
self.VIDEOS(url,uploaded=True)
elif mode==101:
self.PHOTOS(url)
elif mode==102:
self.VIDEOS(url)
elif mode==201:
self.PHOTOS(url,isPaging=True)
update_dir=True
elif mode==202:
self.VIDEOS(url,isPaging=True)
update_dir=True
self.xbmcplugin().endOfDirectory(int(sys.argv[1]),succeeded=success,updateListing=update_dir,cacheToDisc=cache)
## XBMC Plugin stuff starts here --------------------------------------------------------
def get_params():
param=[]
paramstring=sys.argv[2]
if len(paramstring)>=2:
params=sys.argv[2]
cleanedparams=params.replace('?','')
if (params[len(params)-1]=='/'):
params=params[0:len(params)-2]
pairsofparams=cleanedparams.split('&')
param={}
for i in range(len(pairsofparams)):
splitparams={}
splitparams=pairsofparams[i].split('=')
if (len(splitparams))==2:
param[splitparams[0]]=splitparams[1]
return param
def getAuth():
redirect = urllib.quote('http://2ndmind.com/facebookphotos/complete.html')
scope = urllib.quote('user_photos,friends_photos,user_photo_video_tags,friends_photo_video_tags,user_videos,friends_videos')
url = 'https://graph.facebook.com/oauth/authorize?client_id=150505371652086&redirect_uri=%s&type=user_agent&scope=%s' % (redirect,scope)
import xbmcplugin #@UnresolvedImport
xbmcplugin.endOfDirectory(int(sys.argv[1]),succeeded=False)
from webviewer import webviewer #@UnresolvedImport
url,html = webviewer.getWebResult(url,autoForms=[{'action':'login.php'},{'action':'uiserver.php'}]) #@UnusedVariable
token = fb.graph.extractTokenFromURL(url)
if fb.graph.tokenIsValid(token):
fb.graph.saveToken(token)
return token
return None
### Do plugin stuff --------------------------------------------------------------------------
try:
fb = facebookSession()
fb.doPlugin()
except GraphWrapAuthError,e:
import xbmcgui,xbmcaddon #@UnresolvedImport
__addon__ = xbmcaddon.Addon(id='plugin.image.facebook')
__language__ = __addon__.getLocalizedString
if e.type == 'LOGIN_FAILURE':
xbmcgui.Dialog().ok(__language__(30115),__language__(30116),e.message)
else:
xbmcgui.Dialog().ok(__language__(30111),__language__(30112),__language__(30113),__language__(30114))
token = getAuth()
if not token: facebookSession().getToken()
except:
import xbmcgui,xbmcaddon #@UnresolvedImport @Reimport
__addon__ = xbmcaddon.Addon(id='plugin.image.facebook')
__language__ = __addon__.getLocalizedString
msg = ERROR('Unhandled')
xbmcgui.Dialog().ok(__language__(30108),__language__(30117),'',msg)
raise