caches soulscreams in matteo.db, valid for 7 days
This commit is contained in:
parent
30886ce7b9
commit
d0f6ff7f09
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -342,3 +342,6 @@ healthchecksdb
|
||||||
# Personal config file, contains bot token
|
# Personal config file, contains bot token
|
||||||
config.json
|
config.json
|
||||||
ids
|
ids
|
||||||
|
|
||||||
|
# database
|
||||||
|
matteo.db
|
75
database.py
75
database.py
|
@ -1,3 +1,76 @@
|
||||||
#handles the database interactions
|
#handles the database interactions
|
||||||
|
import os, json, datetime
|
||||||
import sqlite3 as sql
|
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()
|
||||||
|
|
|
@ -10,8 +10,13 @@ name_stats_hook = "generateStats/"
|
||||||
|
|
||||||
def get_stats(username):
|
def get_stats(username):
|
||||||
#check database for cached name first
|
#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)
|
response = requests.get(onomancer_url + name_stats_hook + username)
|
||||||
|
print("yelled at onomancer")
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
return response.json()
|
db.cache_soulscream(username, response.json()["soulscream"])
|
||||||
|
return response.json()["soulscream"]
|
|
@ -44,6 +44,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="config.json" />
|
<Content Include="config.json" />
|
||||||
<Content Include="ids" />
|
<Content Include="ids" />
|
||||||
|
<Content Include="matteo.db" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
|
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
|
||||||
<!-- Uncomment the CoreCompile target to enable the Build command in
|
<!-- Uncomment the CoreCompile target to enable the Build command in
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import discord
|
import discord, json, os
|
||||||
import json
|
|
||||||
import database as db
|
import database as db
|
||||||
import os
|
|
||||||
import onomancer as ono
|
import onomancer as ono
|
||||||
|
|
||||||
client = discord.Client()
|
client = discord.Client()
|
||||||
|
@ -27,6 +25,7 @@ def config():
|
||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
|
db.initialcheck()
|
||||||
print(f"logged in as {client.user} with token {config()['token']}")
|
print(f"logged in as {client.user} with token {config()['token']}")
|
||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
|
@ -40,10 +39,10 @@ async def on_message(msg):
|
||||||
return
|
return
|
||||||
|
|
||||||
if msg.channel.id == config()["soulscream channel id"]:
|
if msg.channel.id == config()["soulscream channel id"]:
|
||||||
try:
|
#try:
|
||||||
await msg.channel.send(ono.get_stats(msg.author.nick)["soulscream"])
|
await msg.channel.send(ono.get_stats(msg.author.nick))
|
||||||
except TypeError:
|
#except TypeError:
|
||||||
await msg.channel.send(ono.get_stats(msg.author.name)["soulscream"])
|
#await msg.channel.send(ono.get_stats(msg.author.name))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user