diff options
| -rwxr-xr-x | Dockerfile | 5 | ||||
| -rwxr-xr-x | cron/Dockerfile | 2 | ||||
| -rwxr-xr-x | cron/daily.py | 80 | ||||
| -rwxr-xr-x | cron/graph.py | 31 | ||||
| -rw-r--r-- | cron/hourly.py | 7 | ||||
| -rwxr-xr-x | database.py | 7 | ||||
| -rwxr-xr-x | docker-compose.yml | 10 | ||||
| -rwxr-xr-x | subreddit.py | 7 | ||||
| -rwxr-xr-x | ytapi.py | 7 |
9 files changed, 66 insertions, 90 deletions
@@ -1,8 +1,9 @@ FROM ubuntu:22.04 -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 update -y -RUN apt-get install -y python3-pip build-essential libjpeg-dev zlib1g-dev +RUN apt-get install -y python3-pip build-essential tzdata COPY . /app WORKDIR /app RUN pip3 install -r requirements.txt diff --git a/cron/Dockerfile b/cron/Dockerfile index 821b48a..0239a65 100755 --- a/cron/Dockerfile +++ b/cron/Dockerfile @@ -1,6 +1,8 @@ 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 diff --git a/cron/daily.py b/cron/daily.py index 0f93d23..8e26093 100755 --- a/cron/daily.py +++ b/cron/daily.py @@ -7,7 +7,7 @@ from operator import itemgetter import subreddit import database import datetime -import graph +# import graph def main(): subreddit.display("Starting every day program...") @@ -15,7 +15,7 @@ def main(): with database.Database() as db: db.update_stats() subreddit.display("Posting and updating wiki...") - update_tables(db.get_scores(), db.get_stats()) + # update_tables(db.get_scores(), db.get_stats()) subreddit.display("Formatting leaderboard...") leaderboard = format_monthly_leaderboard() @@ -35,36 +35,36 @@ def main(): 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\n\nTotal λ in circulation|Useful advice given|Unique users\n:--|:--|:--\n%i|%i|%i" % (data[-1][1], data[-1][2], data[-1][3]) +# 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\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) +# # subreddit.REDDIT.subreddit("u_SmallYTChannelBot").submit("/r/SmallYTChannel Statistics: %s" % date, url = imageurl).reply(content) def get_mods(): @@ -78,17 +78,17 @@ def format_monthly_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 - } +# 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) +# image = subreddit.IMGUR.upload_from_path(path, config = config) - return "https://i.imgur.com/%s.png" % image["id"] +# return "https://i.imgur.com/%s.png" % image["id"] if __name__ == "__main__": main() diff --git a/cron/graph.py b/cron/graph.py deleted file mode 100755 index 723997b..0000000 --- a/cron/graph.py +++ /dev/null @@ -1,31 +0,0 @@ -from mpl_toolkits.axes_grid1 import host_subplot -import mpl_toolkits.axisartist as AA -import matplotlib.pyplot as plt -import matplotlib -import datetime - -def make_graph(data): - fig = plt.figure() - - lambdaCount = [i[1] for i in data] - helpGiven = [i[2] for i in data] - uniqueUsers = [i[3] for i in data] - date = [datetime.datetime.strptime(i[4], "%Y-%m-%d") for i in data] - - fig, ax1 = plt.subplots() - ax1.plot(date, lambdaCount, label = "Total λ in circulation", color = "r") - ax1.set_ylabel("Total λ / help given") - - ax1.plot(date, helpGiven, label = "Times help given", color = "g") - - ax2 = ax1.twinx() - ax2.plot(date, uniqueUsers, label = "Unique users") - ax2.set_ylabel("No. Unique Users") - - ax1.legend() - ax2.legend(loc = 4) - fig.autofmt_xdate() - - filepath = "graph.png" - fig.savefig(filepath) - return filepath
\ No newline at end of file diff --git a/cron/hourly.py b/cron/hourly.py index daf46ec..89a6027 100644 --- a/cron/hourly.py +++ b/cron/hourly.py @@ -3,12 +3,14 @@ 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*"]) @@ -38,6 +40,11 @@ def push(fp = "/tmp/sytc_nolog.sql.gz"): 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/database.py b/database.py index b80b548..300b224 100755 --- a/database.py +++ b/database.py @@ -28,12 +28,7 @@ class Database: commit (bool, optional): autocommit. Defaults to True. """ def get_date(stri): - # strip microseconds - stri = stri.split(",")[0] - try: - return datetime.datetime.strptime(stri, "%Y-%m-%d %H:%M:%S") - except ValueError: - return datetime.datetime.strptime(stri, "%b %d %Y %H:%M:%S") + return datetime.datetime.fromisoformat(stri) addFlag = False s = line.split("\t") diff --git a/docker-compose.yml b/docker-compose.yml index 5af0778..62945b8 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3' - services: sytc: build: @@ -12,6 +10,8 @@ services: - mariadb:mysql volumes: - ./config.json:/app/config.json + restart: unless-stopped + cron: build: context: . @@ -25,8 +25,10 @@ services: - sytc volumes: - ./config.json:/app/config.json + restart: unless-stopped networks: db-network: - external: - name: mariadb + external: true + name: mariadb + diff --git a/subreddit.py b/subreddit.py index 219f59a..0360dea 100755 --- a/subreddit.py +++ b/subreddit.py @@ -1,4 +1,4 @@ -from imgurpython import ImgurClient +# from imgurpython import ImgurClient from operator import itemgetter import praw.models @@ -30,7 +30,7 @@ SUBREDDIT = REDDIT.subreddit(CONFIG["subreddit"]) COMMENT_TAIL = CONFIG["comment_tail"] FREE_FLAIRS = CONFIG["free_flairs"] -IMGUR = ImgurClient(**CONFIG["imgurapi"]) +# IMGUR = ImgurClient(**CONFIG["imgurapi"]) logging.basicConfig( format = "%(message)s", @@ -49,7 +49,8 @@ logging.basicConfig( # logger.addHandler(handler) def get_time(): - return time.strftime("%b %d %Y %H:%M:%S", time.gmtime()) + # return time.strftime("%b %d %Y %H:%M:%S", time.gmtime()) + return datetime.datetime.now().astimezone().isoformat() def display(message, concerning = None): logging.info(message) @@ -4,10 +4,9 @@ import json import js2py import os -if os.path.split(os.getcwd())[-1] == "onceaday": - configpath = "../config.json" -else: - configpath = "config.json" +configpath = os.path.join(os.path.dirname(__file__), "config.json") +if not os.path.exists(configpath): + configpath = os.path.join(os.path.dirname(__file__), "..", "config.json") with open(configpath, "r") as f: CONFIG = json.load(f) |
