aboutsummaryrefslogtreecommitdiffstats
path: root/cron
diff options
context:
space:
mode:
Diffstat (limited to 'cron')
-rwxr-xr-xcron/Dockerfile14
-rwxr-xr-xcron/daily.py94
-rw-r--r--cron/entrypoint.sh4
-rw-r--r--cron/hourly.py50
-rwxr-xr-xcron/requirements.txt3
5 files changed, 165 insertions, 0 deletions
diff --git a/cron/Dockerfile b/cron/Dockerfile
new file mode 100755
index 0000000..0239a65
--- /dev/null
+++ b/cron/Dockerfile
@@ -0,0 +1,14 @@
+FROM reg.reaweb.uk/smallytchannelbot
+MAINTAINER Eden Attenborough "eddie.atten.ea29@gmail.com"
+ARG DEBIAN_FRONTEND=noninteractive
+ENV TZ=Europe/London
+RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
+RUN apt-get install -y tzdata cron mariadb-client
+COPY . /app
+WORKDIR /app
+RUN pip3 install -r cron/requirements.txt
+
+RUN echo "@daily root python3 /app/cron/daily.py > /proc/1/fd/1 2>/proc/1/fd/2" > /etc/crontab
+RUN echo "@hourly root python3 /app/cron/hourly.py > /proc/1/fd/1 2>/proc/1/fd/2" >> /etc/crontab
+ENTRYPOINT ["bash"]
+CMD ["/app/cron/entrypoint.sh"]
diff --git a/cron/daily.py b/cron/daily.py
new file mode 100755
index 0000000..8e26093
--- /dev/null
+++ b/cron/daily.py
@@ -0,0 +1,94 @@
+import os
+import sys
+
+sys.path.insert(1, os.path.join(os.path.dirname(__file__), ".."))
+
+from operator import itemgetter
+import subreddit
+import database
+import datetime
+# import graph
+
+def main():
+ subreddit.display("Starting every day program...")
+ subreddit.display("Updating database statistics...")
+ with database.Database() as db:
+ db.update_stats()
+ subreddit.display("Posting and updating wiki...")
+ # update_tables(db.get_scores(), db.get_stats())
+
+ subreddit.display("Formatting leaderboard...")
+ leaderboard = format_monthly_leaderboard()
+ subreddit.display("Made the following leaderboard:\n======================\n%s\n======================\n" % leaderboard)
+ subreddit.display("Updating sidebar...")
+ #it'd be cool to find a way to access this directly without iteration
+ for widget in subreddit.SUBREDDIT.widgets.sidebar:
+ if widget.shortName == "Monthly Lambda Leaderboard":
+ widget.mod.update(text = leaderboard)
+ subreddit.display("Updated in new reddit...")
+
+ sidebar = subreddit.SUBREDDIT.mod.settings()["description"]
+ oldtable = sidebar.split("------")[-1]
+ subreddit.SUBREDDIT.wiki['config/sidebar'].edit(content = sidebar.replace(oldtable, "\n\n## Monthly Lambda Leaderboard\n\n" + leaderboard))
+ subreddit.display("Updated in old reddit...")
+ subreddit.display("Completed.")
+
+ subreddit.logging.info("Called OAD prog @ %s" % subreddit.get_time())
+
+# def update_tables(scores, data):
+# # really ought to switch to jinja for this...
+# content = ""
+# date = str(datetime.date.today())
+# mods = get_mods()
+# # imagepath = graph.make_graph(data)
+# # imageurl = upload_image(imagepath, date)
+# bylambda = [i for i in sorted(scores, key = itemgetter(1), reverse = True) if i[0] not in mods][:10]
+# byhelps = sorted(scores, key = itemgetter(2), reverse = True)[:10]
+
+# content += "\n\n##/r/SmallYTChannel lambda tables: %s" % date
+
+# content += "\n\n###By lambda:"
+# content += "\n\nUsername|Lambda|Help given\n:--|:--|:--"
+# for line in bylambda:
+# content += "\n/u/%s|%i|%i" % (line[0], line[1], line[2])
+
+# content += "\n\n###By Help given:"
+# content += "\n\nUsername|Lambda|Help given\n:--|:--|:--"
+# for line in byhelps:
+# λ = str(line[1])
+# if line[0] in mods:
+# λ = "∞"
+# content += "\n/u/%s|%s|%i" % (line[0], λ, line[2])
+
+# # content += "\n\n##Statistics from %s:\n\nIf you're looking at this through the wiki, not through the bot's profile, then" % (date)
+# # content += "the most up-to-date graph will be shown below. To see the graph at this date, follow [this link.](%s)" % (imageurl)
+# content += "\n\n![](%%%%wikigraph%%%%)\n\nTotal λ in circulation|Useful advice given|Unique users\n:--|:--|:--\n%i|%i|%i" % (data[-1][1], data[-1][2], data[-1][3])
+
+# # subreddit.REDDIT.subreddit("u_SmallYTChannelBot").submit("/r/SmallYTChannel Statistics: %s" % date, url = imageurl).reply(content)
+
+
+def get_mods():
+ return [str(i) for i in subreddit.SUBREDDIT.moderator()] + ["AutoModerator"]
+
+def format_monthly_leaderboard():
+ with database.Database() as db:
+ leaderboard = db.get_lambda_leaderboard()
+ out = "**Username**|**Medal**|**Times Helped**|**Lambda**\n:-|:-|:-|:-\n"
+ for username, times_helped, λ in leaderboard:
+ out += "/u/%s|%1s|%s|%sλ\n" % (username, subreddit.get_medal(λ)[:-1], times_helped, λ)
+ return out + "\nLast updated: %s" % subreddit.get_time()
+
+# def upload_image(path, date):
+# config = {
+# 'album': None,
+# 'name': 'SmallYTChannelBot Statistics graph: %s' % date,
+# 'title': 'SmallYTChannelBot Statistics graph: %s' % date,
+# 'description': 'SmallYTChannelBot Statistics graph: %s' % date
+# }
+
+# image = subreddit.IMGUR.upload_from_path(path, config = config)
+
+# return "https://i.imgur.com/%s.png" % image["id"]
+
+if __name__ == "__main__":
+ main()
diff --git a/cron/entrypoint.sh b/cron/entrypoint.sh
new file mode 100644
index 0000000..610bf84
--- /dev/null
+++ b/cron/entrypoint.sh
@@ -0,0 +1,4 @@
+# https://stackoverflow.com/questions/27771781/how-can-i-access-docker-set-environment-variables-from-a-cron-job/35088810#35088810
+printenv | grep -v "no_proxy" >> /etc/environment
+
+cron -f \ No newline at end of file
diff --git a/cron/hourly.py b/cron/hourly.py
new file mode 100644
index 0000000..89a6027
--- /dev/null
+++ b/cron/hourly.py
@@ -0,0 +1,50 @@
+import os
+import sys
+
+sys.path.insert(1, os.path.join(os.path.dirname(__file__), ".."))
+
+from matplotlib.ticker import EngFormatter
+from discord_webhook import DiscordWebhook
+from operator import itemgetter
+import subprocess
+import subreddit
+import database
+import datetime
+import time
+
+def dump():
+ subprocess.run(["rm", "-fv", "/tmp/*.sql*"])
+ proc1 = subprocess.Popen(
+ [
+ "mysqldump", subreddit.CONFIG["mysql"]["database"],
+ "--ignore-table", "SmallYTChannel.log", "--verbose",
+ "-u", subreddit.CONFIG["mysql"]["user"],
+ "-h", subreddit.CONFIG["mysql"]["host"],
+ "-p%s" % subreddit.CONFIG["mysql"]["passwd"]
+ ],
+ stdout = subprocess.PIPE
+ )
+ proc2 = subprocess.Popen("gzip > /tmp/sytc_nolog.sql.gz", shell = True, stdin = proc1.stdout, stdout = subprocess.PIPE)
+ output = proc2.communicate()
+
+def push(fp = "/tmp/sytc_nolog.sql.gz"):
+ webhook = DiscordWebhook(
+ url = subreddit.CONFIG["discord_webhook"],
+ content = "Hourly /u/SmallYTChannelBot database dump from %s" % datetime.datetime.now().astimezone().isoformat()
+ )
+
+ with open(fp, "rb") as f:
+ webhook.add_file(file = f.read(), filename = os.path.split(fp)[-1])
+
+ response = webhook.execute()
+ subreddit.display(str(response))
+
+if __name__ == "__main__":
+ fmt = EngFormatter("B")
+ starttime = time.time()
+ dump()
+ subreddit.display("\n\nDumped %s in %.1fs\n" % (fmt(os.path.getsize("/tmp/sytc_nolog.sql.gz")), time.time() - starttime))
+ starttime = time.time()
+ push()
+ subreddit.display("Pushed to discord in %.2fs\n" % (time.time() - starttime))
+
diff --git a/cron/requirements.txt b/cron/requirements.txt
new file mode 100755
index 0000000..86f552b
--- /dev/null
+++ b/cron/requirements.txt
@@ -0,0 +1,3 @@
+numpy==1.26.4
+matplotlib
+discord-webhook