-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature to track askhistorians reminders
- Loading branch information
Showing
9 changed files
with
278 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import discord_logging | ||
import sqlalchemy | ||
from datetime import datetime, timedelta | ||
import os | ||
|
||
log = discord_logging.init_logging() | ||
|
||
from database import Database | ||
from classes.comment import DbComment | ||
from classes.reminder import Reminder | ||
from classes.subreddit import Subreddit | ||
from classes.user import User | ||
import utils | ||
|
||
if __name__ == "__main__": | ||
backup_folder = r"D:\backup\RemindMeBot" | ||
date_str = "24-05-15 00:00" | ||
backup_before = datetime.strptime(date_str, "%y-%m-%d %H:%M") | ||
|
||
found = False | ||
for subdir, dirs, files in os.walk(backup_folder): | ||
for filename in reversed(files): | ||
if filename.endswith(".db"): | ||
input_path = os.path.join(subdir, filename) | ||
try: | ||
backup_date = datetime.strptime(filename[:-3], "%Y-%m-%d_%H-%M") | ||
if backup_date > backup_before: | ||
continue | ||
|
||
database = Database(override_location=input_path, readonly=True, quiet=True) | ||
user = database.get_or_add_user("SilynJaguar") | ||
reminders = database.session.query(Reminder).filter_by(user=user).all() | ||
#log.info(f"{backup_date}: {banned_count}") | ||
database.close() | ||
found = True | ||
break | ||
except (ValueError, sqlalchemy.exc.OperationalError): | ||
continue | ||
if found: | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import discord_logging | ||
import sqlalchemy | ||
from datetime import datetime, timedelta | ||
import os | ||
|
||
log = discord_logging.init_logging() | ||
|
||
from database import Database | ||
from classes.comment import DbComment | ||
from classes.reminder import Reminder | ||
from classes.subreddit import Subreddit | ||
from classes.user import User | ||
import utils | ||
|
||
if __name__ == "__main__": | ||
backup_folder = r"D:\backup\RemindMeBot" | ||
|
||
for subdir, dirs, files in os.walk(backup_folder): | ||
for filename in files: | ||
if filename.endswith(".db"): | ||
input_path = os.path.join(subdir, filename) | ||
try: | ||
backup_date = datetime.strptime(filename[:-3], "%Y-%m-%d_%H-%M") | ||
|
||
database = Database(override_location=input_path, readonly=True, quiet=True) | ||
banned_count = database.session.query(Subreddit).filter_by(banned=True).count() | ||
log.info(f"{backup_date}: {banned_count}") | ||
database.close() | ||
except (ValueError, sqlalchemy.exc.OperationalError): | ||
continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from sqlalchemy import Column, ForeignKey, Integer, String | ||
from database import Base | ||
from database.UtcDateTime import UtcDateTime | ||
|
||
import utils | ||
|
||
|
||
class DbStat(Base): | ||
__tablename__ = 'stats' | ||
|
||
id = Column(Integer, primary_key=True) | ||
subreddit = Column(String(80), nullable=False) | ||
thread_id = Column(String(12), nullable=False) | ||
comment_id = Column(String(12)) | ||
initial_date = Column(UtcDateTime, nullable=False) | ||
count_reminders = Column(Integer, nullable=False) | ||
|
||
def __init__( | ||
self, | ||
subreddit, | ||
thread_id, | ||
comment_id, | ||
count_reminders=1 | ||
): | ||
self.subreddit = subreddit | ||
self.thread_id = thread_id | ||
self.comment_id = comment_id | ||
self.count_reminders = count_reminders | ||
self.initial_date = utils.datetime_now() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import discord_logging | ||
from sqlalchemy.orm import joinedload | ||
|
||
import static | ||
from classes.stat import DbStat | ||
|
||
log = discord_logging.get_logger() | ||
|
||
|
||
class _DatabaseStats: | ||
def __init__(self): | ||
self.session = self.session # for pycharm linting | ||
|
||
def add_increment_stat(self, subreddit, thread_id, comment_id): | ||
log.debug(f"Adding or incrementing new stat") | ||
if subreddit is None or thread_id is None: | ||
log.debug(f"Empty arguments, returning") | ||
return | ||
|
||
if subreddit.lower() != "askhistorians": | ||
log.debug(f"Subreddit doesn't match filter, returning") | ||
return | ||
|
||
existing_stat = self.session.query(DbStat)\ | ||
.filter(DbStat.subreddit == subreddit)\ | ||
.filter(DbStat.thread_id == thread_id)\ | ||
.filter(DbStat.comment_id == comment_id)\ | ||
.first() | ||
|
||
if existing_stat is not None: | ||
log.debug(f"Stat exists, incrementing") | ||
existing_stat.count_reminders += 1 | ||
else: | ||
log.debug(f"Stat doesn't exist, creating") | ||
new_stat = DbStat(subreddit, thread_id, comment_id) | ||
self.session.add(new_stat) | ||
|
||
def get_stats_for_ids(self, subreddit, thread_id, comment_id=None): | ||
log.debug("Fetching stat") | ||
|
||
stat = self.session.query(DbStat)\ | ||
.filter(DbStat.subreddit == subreddit)\ | ||
.filter(DbStat.thread_id == thread_id)\ | ||
.filter(DbStat.comment_id == comment_id)\ | ||
.first() | ||
|
||
if stat is None: | ||
log.debug("No stat found") | ||
else: | ||
log.debug(f"Stat found with: {stat.count_reminders}") | ||
|
||
return stat | ||
|
||
def get_stats_for_subreddit(self, subreddit, earliest_date): | ||
log.debug("Fetching stats for subreddit") | ||
|
||
stats = self.session.query(DbStat)\ | ||
.filter(DbStat.subreddit == subreddit)\ | ||
.filter(DbStat.initial_date > earliest_date)\ | ||
.order_by(DbStat.initial_date.asc())\ | ||
.all() | ||
|
||
log.debug(f"{len(stats)} stats found") | ||
return stats | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import utils | ||
import notifications | ||
import static | ||
from praw_wrapper import reddit_test | ||
import messages | ||
from datetime import timedelta | ||
from classes.reminder import Reminder | ||
|
||
|
||
def test_add_stat(database, reddit): | ||
utils.debug_time = utils.parse_datetime_string("2019-01-05 12:00:00") | ||
reminder = Reminder( | ||
source="https://www.reddit.com/message/messages/XXXXX", | ||
message="""[https://www.reddit.com/r/AskHistorians/comments/1emshj8/___/] | ||
RemindMe! 2 days""", | ||
user=database.get_or_add_user("Watchful1"), | ||
requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"), | ||
target_date=utils.parse_datetime_string("2019-01-05 05:00:00") | ||
) | ||
database.add_reminder(reminder) | ||
|
||
stat = database.get_stats_for_ids("AskHistorians", "1emshj8") | ||
assert stat.count_reminders == 1 | ||
assert stat.initial_date == utils.debug_time | ||
|
||
reminder = Reminder( | ||
source="https://www.reddit.com/message/messages/YYYYY", | ||
message="""[https://www.reddit.com/r/AskHistorians/comments/1emshj8/___/] | ||
RemindMe! 2 days""", | ||
user=database.get_or_add_user("Watchful1"), | ||
requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"), | ||
target_date=utils.parse_datetime_string("2019-01-05 05:00:00") | ||
) | ||
database.add_reminder(reminder) | ||
|
||
stat = database.get_stats_for_ids("AskHistorians", "1emshj8") | ||
assert stat.count_reminders == 2 | ||
assert stat.initial_date == utils.debug_time | ||
|
||
|
||
def test_add_stats(database, reddit): | ||
utils.debug_time = utils.parse_datetime_string("2019-01-05 12:00:00") | ||
reminders = [ | ||
Reminder( | ||
source="https://www.reddit.com/message/messages/XXXXX", | ||
message="[https://www.reddit.com/r/AskHistorians/comments/1emshj8/___/]", | ||
user=database.get_or_add_user("Watchful1"), | ||
requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"), | ||
target_date=utils.parse_datetime_string("2019-01-05 05:00:00") | ||
), | ||
Reminder( | ||
source="https://www.reddit.com/message/messages/XXXXX", | ||
message="[https://www.reddit.com/r/AskHistorians/comments/1emshk6/___/]", | ||
user=database.get_or_add_user("Watchful1"), | ||
requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"), | ||
target_date=utils.parse_datetime_string("2019-01-06 05:00:00") | ||
), | ||
Reminder( | ||
source="https://www.reddit.com/message/messages/XXXXX", | ||
message="[https://www.reddit.com/r/AskHistorians/comments/1emshk6/___/]", | ||
user=database.get_or_add_user("Watchful1"), | ||
requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"), | ||
target_date=utils.parse_datetime_string("2019-01-07 05:00:00") | ||
), | ||
Reminder( | ||
source="https://www.reddit.com/message/messages/XXXXX", | ||
message="[https://www.reddit.com/r/AskHistorians/comments/1emshk6/___/]", | ||
user=database.get_or_add_user("Watchful1"), | ||
requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"), | ||
target_date=utils.parse_datetime_string("2019-01-07 05:00:00") | ||
), | ||
Reminder( | ||
source="https://www.reddit.com/message/messages/XXXXX", | ||
message="[https://www.reddit.com/r/history/comments/1emshf5/___/]", | ||
user=database.get_or_add_user("Watchful1"), | ||
requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"), | ||
target_date=utils.parse_datetime_string("2019-01-08 05:00:00") | ||
), | ||
Reminder( | ||
source="https://www.reddit.com/message/messages/XXXXX", | ||
message="[https://www.reddit.com/r/AskHistorians/comments/1emshj8/___/]", | ||
user=database.get_or_add_user("Watchful1"), | ||
requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"), | ||
target_date=utils.parse_datetime_string("2019-01-09 05:00:00") | ||
) | ||
] | ||
for reminder in reminders: | ||
database.add_reminder(reminder) | ||
|
||
stats = database.get_stats_for_subreddit("AskHistorians", utils.debug_time - timedelta(days=1)) | ||
assert len(stats) == 2 | ||
assert stats[0].count_reminders == 2 | ||
assert stats[1].count_reminders == 3 |