generated from public/aoc_template
add some logging to stdout
This commit is contained in:
parent
8109e8a85f
commit
c44a4cf451
106
aoc_bot.py
106
aoc_bot.py
@ -1,8 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import json
|
||||
import os.path
|
||||
|
||||
import requests
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
@ -44,11 +42,11 @@ class AOCBot:
|
||||
self.__cache_file = config["AOC"]["cache_file"]
|
||||
except AttributeError as e:
|
||||
print("CONFIGURATION ERROR: %s" % e)
|
||||
sys.exit(1)
|
||||
|
||||
def fetch_leaderboard(self, year: int = datetime.now().year) -> dict:
|
||||
return requests.get(
|
||||
"https://adventofcode.com/%d/leaderboard/private/view/%s.json"
|
||||
% (year, self.__aoc_group_id),
|
||||
"https://adventofcode.com/%d/leaderboard/private/view/%s.json" % (year, self.__aoc_group_id),
|
||||
headers={"User-Agent": self.__aoc_user_agent},
|
||||
cookies={"session": self.__aoc_session_id},
|
||||
).json()
|
||||
@ -56,8 +54,7 @@ class AOCBot:
|
||||
def command_info(self, msg_from: str, message: str):
|
||||
self.__irc_bot.privmsg(
|
||||
msg_from,
|
||||
"I am %s => %s"
|
||||
% (self.__irc_bot.getUser().nickname, self.__irc_bot.getUser().username),
|
||||
"I am %s => %s" % (self.__irc_bot.getUser().nickname, self.__irc_bot.getUser().username),
|
||||
)
|
||||
self.__irc_bot.privmsg(msg_from, "I am currently in the following channels:")
|
||||
for c in self.__irc_bot.getChannelList():
|
||||
@ -79,17 +76,12 @@ class AOCBot:
|
||||
|
||||
today_list = []
|
||||
for member, member_data in self.__cache_data.items():
|
||||
if (
|
||||
not member.startswith("__")
|
||||
and "days" in member_data
|
||||
and day in member_data["days"]
|
||||
):
|
||||
if not member.startswith("__") and "days" in member_data and day in member_data["days"]:
|
||||
today_list.append(member)
|
||||
|
||||
self.__irc_bot.privmsg(
|
||||
self.__irc_channel,
|
||||
"Day %s's leaderboard (last updated: %s):"
|
||||
% (day, self.__cache_data["__last_update__"]),
|
||||
"Day %s's leaderboard (last updated: %s):" % (day, self.__cache_data["__last_update__"]),
|
||||
)
|
||||
for i, member in enumerate(
|
||||
sorted(
|
||||
@ -103,16 +95,14 @@ class AOCBot:
|
||||
|
||||
if "1" in self.__cache_data[member]["days"][day]:
|
||||
p1_time = "in " + human_readable_time_from_delta(
|
||||
datetime.fromisoformat(self.__cache_data[member]["days"][day]["1"])
|
||||
- day_start
|
||||
datetime.fromisoformat(self.__cache_data[member]["days"][day]["1"]) - day_start
|
||||
)
|
||||
else:
|
||||
p1_time = "*not yet solved*"
|
||||
|
||||
if "2" in self.__cache_data[member]["days"][day]:
|
||||
p2_time = "in " + human_readable_time_from_delta(
|
||||
datetime.fromisoformat(self.__cache_data[member]["days"][day]["2"])
|
||||
- day_start
|
||||
datetime.fromisoformat(self.__cache_data[member]["days"][day]["2"]) - day_start
|
||||
)
|
||||
else:
|
||||
p2_time = "not yet solved"
|
||||
@ -137,15 +127,10 @@ class AOCBot:
|
||||
sys.exit(0)
|
||||
|
||||
def on_raw(self, msg_from: str, msg_type: str, msg_to: str, message: str):
|
||||
print(
|
||||
"[%s] <%s> (%s) -> <%s>: %s"
|
||||
% (datetime.now().strftime("%H:%M:%S"), msg_from, msg_type, msg_to, message)
|
||||
)
|
||||
print("[%s] <%s> (%s) -> <%s>: %s" % (datetime.now().strftime("%H:%M:%S"), msg_from, msg_type, msg_to, message))
|
||||
|
||||
def calc_scores(self):
|
||||
member_count = len(
|
||||
[x for x in self.__cache_data.keys() if not x.startswith("__")]
|
||||
)
|
||||
member_count = len([x for x in self.__cache_data.keys() if not x.startswith("__")])
|
||||
for day in map(str, range(1, 26)):
|
||||
p1_times = []
|
||||
p2_times = []
|
||||
@ -163,22 +148,12 @@ class AOCBot:
|
||||
if member.startswith("__") or day not in member_data["days"]:
|
||||
continue
|
||||
|
||||
if (
|
||||
"1" in member_data["days"][day]
|
||||
and member_data["days"][day]["1"] in p1_times
|
||||
):
|
||||
score = member_count - sorted(p1_times).index(
|
||||
member_data["days"][day]["1"]
|
||||
)
|
||||
if "1" in member_data["days"][day] and member_data["days"][day]["1"] in p1_times:
|
||||
score = member_count - sorted(p1_times).index(member_data["days"][day]["1"])
|
||||
self.__cache_data[member]["days"][day]["score"] += score
|
||||
|
||||
if (
|
||||
"2" in member_data["days"][day]
|
||||
and member_data["days"][day]["2"] in p2_times
|
||||
):
|
||||
score = member_count - sorted(p2_times).index(
|
||||
member_data["days"][day]["2"]
|
||||
)
|
||||
if "2" in member_data["days"][day] and member_data["days"][day]["2"] in p2_times:
|
||||
score = member_count - sorted(p2_times).index(member_data["days"][day]["2"])
|
||||
self.__cache_data[member]["days"][day]["score"] += score
|
||||
|
||||
def update_leaderboard(self):
|
||||
@ -210,30 +185,21 @@ class AOCBot:
|
||||
for part in member_data["completion_day_level"][day]:
|
||||
if part not in self.__cache_data[member]["days"][day]:
|
||||
completion_time = datetime.fromtimestamp(
|
||||
member_data["completion_day_level"][day][part][
|
||||
"get_star_ts"
|
||||
]
|
||||
member_data["completion_day_level"][day][part]["get_star_ts"]
|
||||
)
|
||||
if member_data["name"] not in new_stars:
|
||||
new_stars[member_data["name"]] = {}
|
||||
|
||||
finishing_time = human_readable_time_from_delta(
|
||||
completion_time - day_start
|
||||
)
|
||||
new_stars[member_data["name"]][
|
||||
"d" + day + "p" + part
|
||||
] = finishing_time
|
||||
self.__cache_data[member]["days"][day][
|
||||
part
|
||||
] = completion_time.isoformat()
|
||||
finishing_time = human_readable_time_from_delta(completion_time - day_start)
|
||||
new_stars[member_data["name"]]["d" + day + "p" + part] = finishing_time
|
||||
self.__cache_data[member]["days"][day][part] = completion_time.isoformat()
|
||||
|
||||
if len(new_stars) > 0:
|
||||
self.__irc_bot.privmsg(self.__irc_channel, "New Stars found:")
|
||||
for member, parts in new_stars.items():
|
||||
line = member + ": "
|
||||
line += ", ".join(
|
||||
"%s (%s)" % (part, new_stars[member][part])
|
||||
for part in sorted(new_stars[member].keys())
|
||||
"%s (%s)" % (part, new_stars[member][part]) for part in sorted(new_stars[member].keys())
|
||||
)
|
||||
|
||||
self.__irc_bot.privmsg(self.__irc_channel, line)
|
||||
@ -243,11 +209,17 @@ class AOCBot:
|
||||
self.calc_scores()
|
||||
|
||||
def start(self):
|
||||
print("[AoCBot] Loading Cache File")
|
||||
self.__cache_data = JSONFile(self.__cache_file, create=True)
|
||||
self.__aoc_session_id = (
|
||||
open(self.__aoc_session_file, "r").readlines()[0].strip()
|
||||
)
|
||||
|
||||
print("[AoCBot] Loading Session ID")
|
||||
try:
|
||||
self.__aoc_session_id = open(self.__aoc_session_file, "r").readlines()[0].strip()
|
||||
except FileNotFoundError:
|
||||
print("[AoCBot] Session ID not found: %s" % self.__aoc_session_file)
|
||||
sys.exit(1)
|
||||
|
||||
print("[AoCBot] Starting IrcBot")
|
||||
self.__irc_bot = IrcBot(
|
||||
server=self.__irc_server,
|
||||
port=self.__irc_port,
|
||||
@ -255,22 +227,22 @@ class AOCBot:
|
||||
username=self.__irc_user,
|
||||
realname=self.__irc_real_name,
|
||||
)
|
||||
|
||||
print("[AoCBot] Joining Channel %s" % self.__irc_channel)
|
||||
self.__irc_bot.join(self.__irc_channel)
|
||||
self.__irc_bot.schedule(
|
||||
"update_leaderboard", timedelta(minutes=15), self.update_leaderboard
|
||||
)
|
||||
|
||||
print("[AoCBot] Scheduling Leaderboard Update for Group ID %s" % self.__aoc_group_id)
|
||||
self.__irc_bot.schedule("update_leaderboard", timedelta(minutes=15), self.update_leaderboard)
|
||||
|
||||
print("[AoCBot] Registering Commands")
|
||||
self.__irc_bot.on(ServerMessage.RAW, self.on_raw)
|
||||
self.__irc_bot.register_channel_command(
|
||||
"!info", self.__irc_channel, self.command_info
|
||||
)
|
||||
self.__irc_bot.register_channel_command(
|
||||
"!today", self.__irc_channel, self.command_today
|
||||
)
|
||||
self.__irc_bot.register_channel_command(
|
||||
"!day", self.__irc_channel, self.command_today
|
||||
)
|
||||
self.__irc_bot.register_channel_command("!info", self.__irc_channel, self.command_info)
|
||||
self.__irc_bot.register_channel_command("!today", self.__irc_channel, self.command_today)
|
||||
self.__irc_bot.register_channel_command("!day", self.__irc_channel, self.command_today)
|
||||
self.__irc_bot.register_privmsg_command("info", self.command_info)
|
||||
self.__irc_bot.register_privmsg_command("quit", self.command_quit)
|
||||
|
||||
print("[AoCBot] Starting Main Loop")
|
||||
self.__irc_bot.run()
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user