From d0f6ff7f09c15e08daddd9ae47025370a6e0070e Mon Sep 17 00:00:00 2001 From: Sakimori Date: Sat, 19 Dec 2020 22:14:45 -0500 Subject: [PATCH] caches soulscreams in matteo.db, valid for 7 days --- .gitignore | 5 ++- database.py | 75 ++++++++++++++++++++++++++++++++++++++++++++- onomancer.py | 9 ++++-- the-prestige.pyproj | 1 + the_prestige.py | 13 ++++---- 5 files changed, 92 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 14f0fc0..af5ace5 100644 --- a/.gitignore +++ b/.gitignore @@ -341,4 +341,7 @@ healthchecksdb # Personal config file, contains bot token config.json -ids \ No newline at end of file +ids + +# database +matteo.db \ No newline at end of file diff --git a/database.py b/database.py index 54e92f6..a7656bb 100644 --- a/database.py +++ b/database.py @@ -1,3 +1,76 @@ #handles the database interactions - +import os, json, datetime import sqlite3 as sql + + +def create_connection(): + #create connection, create db if doesn't exist + conn = None + try: + conn = sql.connect("matteo.db") + return conn + except: + print("oops, db connection no work") + return conn + + +def initialcheck(): + conn = create_connection() + soulscream_table_check_string = """ CREATE TABLE IF NOT EXISTS soulscreams ( + counter integer PRIMARY KEY, + name text NOT NULL, + soulscream text NOT NULL, + timestamp text NOT NULL + ); """ + + if conn is not None: + c = conn.cursor() + c.execute(soulscream_table_check_string) + + conn.commit() + conn.close() + + +def get_soulscream(username): + conn = create_connection() + + #returns none if not found or more than 3 days old + if conn is not None: + c = conn.cursor() + c.execute("SELECT * FROM soulscreams WHERE name=?", (username,)) + scream = c.fetchone() + + try: + cachetime = datetime.datetime.fromisoformat(scream[3]) + print(datetime.datetime.now(datetime.timezone.utc) - cachetime) + if datetime.datetime.now(datetime.timezone.utc) - cachetime >= datetime.timedelta(days = 7): + #delete old cache + c.execute("DELETE FROM soulscreams WHERE name=?", (username,)) + conn.commit() + conn.close() + return None + except TypeError: + conn.close() + return None + + conn.close() + return scream[2] + + conn.close() + return None + + + +def cache_soulscream(username, soulscream): + conn = create_connection() + store_string = """ INSERT INTO soulscreams(name, soulscream, timestamp) + VALUES (?,?, ?) """ + + if conn is not None: + c = conn.cursor() + c.execute(store_string, (username, soulscream, datetime.datetime.now(datetime.timezone.utc))) + conn.commit() + + + + conn.close() diff --git a/onomancer.py b/onomancer.py index 67f212d..7e318cb 100644 --- a/onomancer.py +++ b/onomancer.py @@ -10,8 +10,13 @@ name_stats_hook = "generateStats/" def get_stats(username): #check database for cached name first + scream = db.get_soulscream(username) + if scream is not None: + return scream - #yell at onomancer + #yell at onomancer if not in cache or too old response = requests.get(onomancer_url + name_stats_hook + username) + print("yelled at onomancer") if response.status_code == 200: - return response.json() \ No newline at end of file + db.cache_soulscream(username, response.json()["soulscream"]) + return response.json()["soulscream"] \ No newline at end of file diff --git a/the-prestige.pyproj b/the-prestige.pyproj index 20153e4..4f1cecf 100644 --- a/the-prestige.pyproj +++ b/the-prestige.pyproj @@ -44,6 +44,7 @@ +