moved the logic of filter days into the sql query

This commit is contained in:
jsl12
2022-05-11 23:20:04 -05:00
parent 8aa8dfa034
commit de894a67aa
2 changed files with 17 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ import asyncio
import logging import logging
import re import re
import sqlite3 import sqlite3
from datetime import datetime, timedelta
from pathlib import Path from pathlib import Path
from typing import List from typing import List
@@ -11,7 +12,7 @@ from nextcord import RawReactionActionEvent, Emoji
from nextcord import utils from nextcord import utils
from . import jokes from . import jokes
from .reactions import ReactionData, filter_days from .reactions import ReactionData
LIL_STINKY_ID = 704043422276780072 LIL_STINKY_ID = 704043422276780072
@@ -30,11 +31,11 @@ class Kwaylon(Client):
self.jokes = list(jokes.collect_jokes()) self.jokes = list(jokes.collect_jokes())
self.lock = asyncio.Lock() self.lock = asyncio.Lock()
self.most_regex = re.compile('most\s+(?P<emoji>\S+)') self.most_regex = re.compile('^most\s+(?P<emoji>\S+)')
self.leaderboard_regex = re.compile( # self.leaderboard_regex = re.compile(
'^most\s*?(?P<emoji>\S+?)\s*?(leaderboard|((?:.+?(?P<days>\d+) days)))', # '^most\s*?(?P<emoji>\S+?)\s*?(leaderboard|((?:.+?(?P<days>\d+) days)))',
re.IGNORECASE # re.IGNORECASE
) # )
def text_channels(self) -> List[TextChannel]: def text_channels(self) -> List[TextChannel]:
return [chan for chan in self.get_all_channels() if isinstance(chan, TextChannel)] return [chan for chan in self.get_all_channels() if isinstance(chan, TextChannel)]
@@ -82,12 +83,11 @@ class Kwaylon(Client):
async with message.channel.typing(): async with message.channel.typing():
with self.data.connect() as con: with self.data.connect() as con:
df = self.data.read_emoji(emoji_name, con) days = get_days(message.content) or 14
df = self.data.read_emoji(emoji_name, con=con,
after=(datetime.today() - timedelta(days=days)).astimezone())
con.close() con.close()
days = get_days(message.content) or 14
df = filter_days(df, days)
if df.shape[0] > 0: if df.shape[0] > 0:
LOGGER.info(f'{df.shape[0]} messages with {emoji_ref} after filtering') LOGGER.info(f'{df.shape[0]} messages with {emoji_ref} after filtering')

View File

@@ -43,8 +43,12 @@ class ReactionData:
# else: # else:
# LOGGER.info(f'Wrote {len(data)} rows to {self.path.name}') # LOGGER.info(f'Wrote {len(data)} rows to {self.path.name}')
def read_emoji(self, emoji: str, con: sqlite3.Connection = None) -> pd.DataFrame: def read_emoji(self, emoji: str, after: datetime = None,
return self.read_sql(query=f"SELECT * FROM reactions WHERE emoji LIKE '{emoji}'", con=con) con: sqlite3.Connection = None) -> pd.DataFrame:
q = f"SELECT * FROM reactions WHERE emoji LIKE '{emoji}'"
if after is not None:
q += f" AND datetime >= '{after}'"
return self.read_sql(query=q, con=con)
def read_all(self, con: sqlite3.Connection = None) -> pd.DataFrame: def read_all(self, con: sqlite3.Connection = None) -> pd.DataFrame:
return self.read_sql(query='SELECT * FROM reactions', con=con) return self.read_sql(query='SELECT * FROM reactions', con=con)
@@ -53,6 +57,7 @@ class ReactionData:
close = con is None close = con is None
con = con or self.connect() con = con or self.connect()
LOGGER.info(query)
res = pd.read_sql(query, con=con, index_col=None) res = pd.read_sql(query, con=con, index_col=None)
LOGGER.info(f'Read {res.shape[0]} reactions') LOGGER.info(f'Read {res.shape[0]} reactions')
if close: if close: