Use Cache dict for facebook storage

master
pictuga 2013-12-22 12:43:03 +01:00
parent afe31e695a
commit d97a72bc0b
2 changed files with 18 additions and 24 deletions

View File

@ -79,10 +79,10 @@ def formatString(string, getter, error=False):
def PreWorker(url, cache): def PreWorker(url, cache):
if urlparse.urlparse(url).netloc == 'graph.facebook.com': if urlparse.urlparse(url).netloc == 'graph.facebook.com':
facebook = cache.new('facebook', True) facebook = cache.new('facebook', persistent=True, dic=True)
token = urlparse.parse_qs(urlparse.urlparse(url).query)['access_token'][0] token = urlparse.parse_qs(urlparse.urlparse(url).query)['access_token'][0]
if 't'+token not in facebook: if token not in facebook['token']:
# this token ain't known, look for info about it # this token ain't known, look for info about it
eurl = "https://graph.facebook.com/debug_token?input_token={token}&access_token={app_token}".format(token=token, app_token=morss.FBAPPTOKEN) eurl = "https://graph.facebook.com/debug_token?input_token={token}&access_token={app_token}".format(token=token, app_token=morss.FBAPPTOKEN)
data = json.loads(urllib2.urlopen(eurl).read())['data'] data = json.loads(urllib2.urlopen(eurl).read())['data']
@ -92,21 +92,18 @@ def PreWorker(url, cache):
expires = int(data['expires_at']) expires = int(data['expires_at'])
short = 'issued_at' not in data short = 'issued_at' not in data
facebook.set('t'+token, user_id) facebook['token'][token] = {'user': user_id, 'expires': expires}
facebook.set('e'+token, expires)
good = True
# do some woodoo to know if we already have sth better # do some woodoo to know if we already have sth better
if 'u'+user_id not in facebook: if user_id not in facebook['user']:
# grab a new one anyway, new user # grab a new one anyway, new user
facebook.set('o'+user_id, token) facebook['user'][user_id] = {'original': token}
good = True good = True
else: else:
# maybe it's a better one # maybe it's a better one
last = facebook.get('u'+user_id) last = facebook['user'][user_id]['token']
last_expires = facebook.get('e'+last) last_expires = facebook['token'][last]['expires']
if expires > last_expires: if expires > last_expires:
# new is better # new is better
@ -119,17 +116,15 @@ def PreWorker(url, cache):
token = values['access_token'][0] token = values['access_token'][0]
expires = int(time.time() + int(values['expires'][0])) expires = int(time.time() + int(values['expires'][0]))
facebook.set('t'+token, user_id) facebook['token'][token] = {'user': user_id, 'expires': expires}
facebook.set('e'+token, expires)
if good: facebook['user'][user_id]['token'] = token
facebook.set('u'+user_id, token)
# hey look for a newer token and use it # hey look for a newer token and use it
token = urlparse.parse_qs(urlparse.urlparse(url).query)['access_token'][0] token = urlparse.parse_qs(urlparse.urlparse(url).query)['access_token'][0]
user_id = facebook.get('t'+token) user_id = facebook['token'][token]['user']
last = facebook.get('u'+user_id) last = facebook['user'][user_id]['token']
original = facebook.get('o'+user_id) original = facebook['user'][user_id]['original']
nurl = url.replace(token, last) nurl = url.replace(token, last)
ncache = url.replace(token, original) ncache = url.replace(token, original)
@ -234,7 +229,7 @@ class Builder(object):
if self.cache: if self.cache:
facebook = self.cache.new('facebook', True) facebook = self.cache.new('facebook', True)
token = urlparse.parse_qs(urlparse.urlparse(self.link).query)['access_token'][0] token = urlparse.parse_qs(urlparse.urlparse(self.link).query)['access_token'][0]
expires = facebook.get('e'+token) expires = facebook['token'][token]['expires']
lifespan = expires - time.time() lifespan = expires - time.time()
if lifespan < 5*24*3600: if lifespan < 5*24*3600:

View File

@ -629,7 +629,7 @@ if __name__ == '__main__':
cachePath = os.path.expanduser('~') + '/.cache/morss' cachePath = os.path.expanduser('~') + '/.cache/morss'
if options.facebook: if options.facebook:
facebook = Cache(cachePath, 'facebook', True) facebook = Cache(cachePath, 'facebook', persistent=True, dic=True)
# get real token from code # get real token from code
code = urlparse.parse_qs(urlparse.urlparse(url).query)['code'][0] code = urlparse.parse_qs(urlparse.urlparse(url).query)['code'][0]
@ -648,12 +648,11 @@ if __name__ == '__main__':
user_id = json.loads(urllib2.urlopen(iurl).read())['id'] user_id = json.loads(urllib2.urlopen(iurl).read())['id']
# do sth out of it # do sth out of it
facebook.set('t'+ltoken, user_id) if user_id not in facebook['user']:
facebook.set('e'+ltoken, expires) facebook['user'][user_id] = {'original': ltoken}
facebook.set('u'+user_id, ltoken)
if 'o'+user_id not in token: facebook['token'][ltoken] = {'user': user_id, 'expires': expires}
facebook.set('o'+user_id, ltoken) facebook['user'][user_id]['token'] = ltoken
facebook.save() facebook.save()