From 6f29d39a9727688f7f4b25670614abbe66baa957 Mon Sep 17 00:00:00 2001 From: jwansek Date: Mon, 19 Apr 2021 21:41:50 +0100 Subject: added cron docker container, added docker-compose --- .gitignore | 1 + SmallYTChannelDatabase.db | Bin 27417600 -> 0 bytes docker-compose.yml | 14 +++++++ oadrequirements.txt | 6 --- onceaday.py | 100 -------------------------------------------- onceaday/Dockerfile | 10 +++++ onceaday/crontab | 1 + onceaday/onceaday.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++ onceaday/requirements.txt | 6 +++ readme.md | 8 ++-- 10 files changed, 139 insertions(+), 110 deletions(-) delete mode 100755 SmallYTChannelDatabase.db create mode 100644 docker-compose.yml delete mode 100644 oadrequirements.txt delete mode 100755 onceaday.py create mode 100644 onceaday/Dockerfile create mode 100644 onceaday/crontab create mode 100644 onceaday/onceaday.py create mode 100644 onceaday/requirements.txt diff --git a/.gitignore b/.gitignore index c838c6a..09dcb3e 100755 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ chblacklist.txt api.log config.json *.log +*.db # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/SmallYTChannelDatabase.db b/SmallYTChannelDatabase.db deleted file mode 100755 index aa102d9..0000000 Binary files a/SmallYTChannelDatabase.db and /dev/null differ diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..171539e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3' + +services: + sytc: + build: + context: . + dockerfile: Dockerfile + image: jwansek/smallytchannelbot + network_mode: host + cron: + build: + context: . + dockerfile: ./onceaday/Dockerfile + network_mode: host \ No newline at end of file diff --git a/oadrequirements.txt b/oadrequirements.txt deleted file mode 100644 index 23c2cef..0000000 --- a/oadrequirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -praw==7.1.0 -matplotlib==3.3.4 -PyMySQL==0.9.3 -imgurpython==1.1.7 -google_api_python_client==1.12.8 -js2py==0.70 diff --git a/onceaday.py b/onceaday.py deleted file mode 100755 index 1570c9a..0000000 --- a/onceaday.py +++ /dev/null @@ -1,100 +0,0 @@ -import os -from operator import itemgetter -os.chdir("/home/eden/SmallYTChannelBotSubmissions") - -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(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): - 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] - - subreddit.SUBREDDIT.stylesheet.upload("wikigraph", imagepath) - - 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).mod.distinguish(sticky = True) - - subreddit.SUBREDDIT.wiki["lambdatables"].edit(content, reason = "Update: %s" % date) - subreddit.SUBREDDIT.wiki[date].edit(content, reason = "Update: %s" % date) - - currentdata = subreddit.SUBREDDIT.wiki["index"].content_md - currentdata += "\n\n* [%s](/r/SmallYTChannel/wiki/%s)" % (date, date) - - subreddit.SUBREDDIT.wiki["index"].edit(currentdata, reason = "Update: %s" % date) - -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/onceaday/Dockerfile b/onceaday/Dockerfile new file mode 100644 index 0000000..b737d90 --- /dev/null +++ b/onceaday/Dockerfile @@ -0,0 +1,10 @@ +FROM debian:latest +MAINTAINER Eden Attenborough "eddie.atten.ea29@gmail.com" +RUN apt-get update -y +RUN apt-get install -y tzdata python3-pip python-dev build-essential pkg-config cron +COPY . /app +WORKDIR /app +COPY onceaday/crontab /etc/cron.d/oad-crontab +RUN chmod 0644 /etc/cron.d/oad-crontab && crontab /etc/cron.d/oad-crontab +RUN pip3 install -r onceaday/requirements.txt +ENTRYPOINT ["cron", "-f"] \ No newline at end of file diff --git a/onceaday/crontab b/onceaday/crontab new file mode 100644 index 0000000..c8ce32a --- /dev/null +++ b/onceaday/crontab @@ -0,0 +1 @@ +@daily python3 /app/onceaday/onceaday.py /app/onceaday diff --git a/onceaday/onceaday.py b/onceaday/onceaday.py new file mode 100644 index 0000000..97705c7 --- /dev/null +++ b/onceaday/onceaday.py @@ -0,0 +1,103 @@ +import os +import sys + +os.chdir(sys.argv[1]) +sys.path.insert(0, 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(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): + 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] + + subreddit.SUBREDDIT.stylesheet.upload("wikigraph", imagepath) + + 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).mod.distinguish(sticky = True) + + subreddit.SUBREDDIT.wiki["lambdatables"].edit(content, reason = "Update: %s" % date) + subreddit.SUBREDDIT.wiki[date].edit(content, reason = "Update: %s" % date) + + currentdata = subreddit.SUBREDDIT.wiki["index"].content_md + currentdata += "\n\n* [%s](/r/SmallYTChannel/wiki/%s)" % (date, date) + + subreddit.SUBREDDIT.wiki["index"].edit(currentdata, reason = "Update: %s" % date) + +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/onceaday/requirements.txt b/onceaday/requirements.txt new file mode 100644 index 0000000..23c2cef --- /dev/null +++ b/onceaday/requirements.txt @@ -0,0 +1,6 @@ +praw==7.1.0 +matplotlib==3.3.4 +PyMySQL==0.9.3 +imgurpython==1.1.7 +google_api_python_client==1.12.8 +js2py==0.70 diff --git a/readme.md b/readme.md index b826e5a..8737117 100755 --- a/readme.md +++ b/readme.md @@ -8,13 +8,13 @@ The source code for /u/SmallYTChannelBot. - Edit the configuration file and rename -- Build the image: +- Build the images: -`sudo docker build -t smallytchannelbot:latest .` +`sudo docker-compose build` -- Start the container: +- Start the containers: -`sudo docker run --name smallytchannelbot --net=host -d smallytchannelbot` +`sudo docker-compose up -d` # TODOs -- cgit v1.2.3