From ed0131fb10851c7a50d3555a31ba2da2c42024d8 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Mon, 8 May 2017 21:38:47 -0700 Subject: [PATCH] Made makeshift caching for get user guilds func --- webapp/titanembeds/oauth.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/webapp/titanembeds/oauth.py b/webapp/titanembeds/oauth.py index dffeab8..4c793fe 100644 --- a/webapp/titanembeds/oauth.py +++ b/webapp/titanembeds/oauth.py @@ -1,6 +1,9 @@ from config import config +import json from requests_oauthlib import OAuth2Session from flask import session, abort, url_for +from titanembeds.database import get_keyvalproperty, set_keyvalproperty +from titanembeds.utils import make_cache_key authorize_url = "https://discordapp.com/api/oauth2/authorize" token_url = "https://discordapp.com/api/oauth2/token" @@ -42,14 +45,18 @@ def user_has_permission(permission, index): return bool((int(permission) >> index) & 1) def get_user_guilds(): + cache = get_keyvalproperty("OAUTH/USERGUILDS/"+make_cache_key()) + if cache: + return cache req = discordrest_from_user("/users/@me/guilds") + if req.status_code != 200: + abort(req.status_code) + req = json.dumps(req.json()) + set_keyvalproperty("OAUTH/USERGUILDS/"+make_cache_key(), req, 100) return req def get_user_managed_servers(): - guilds = get_user_guilds() - if guilds.status_code != 200: - abort(guilds.status_code) - guilds = guilds.json() + guilds = json.loads(get_user_guilds()) filtered = [] for guild in guilds: permission = guild['permissions'] # Manage Server, Ban Members, Kick Members