diff options
author | jwansek <eddie.atten.ea29@gmail.com> | 2025-06-17 14:28:49 +0100 |
---|---|---|
committer | jwansek <eddie.atten.ea29@gmail.com> | 2025-06-17 14:28:49 +0100 |
commit | 4bcba9bfeff003e081833b4d6290290271aae967 (patch) | |
tree | 02e0bd2a7119333be1d5e53d9b40bd85e6254359 | |
parent | 2bdc35bd8159c6ccbb9d87f0abca25f6dd784f52 (diff) | |
download | SmallYTChannelBot-4bcba9bfeff003e081833b4d6290290271aae967.tar.gz SmallYTChannelBot-4bcba9bfeff003e081833b4d6290290271aae967.zip |
Updated cron scripts
-rwxr-xr-x | .gitignore | 1 | ||||
-rwxr-xr-x | config.json.example | 38 | ||||
-rwxr-xr-x | cron/Dockerfile | 12 | ||||
-rwxr-xr-x | cron/daily.py (renamed from onceaday/onceaday.py) | 4 | ||||
-rw-r--r-- | cron/entrypoint.sh | 4 | ||||
-rwxr-xr-x | cron/graph.py (renamed from onceaday/graph.py) | 0 | ||||
-rw-r--r-- | cron/hourly.py | 43 | ||||
-rwxr-xr-x | cron/requirements.txt (renamed from onceaday/requirements.txt) | 1 | ||||
-rwxr-xr-x | docker-compose.yml | 4 | ||||
-rwxr-xr-x | exampleconfig.json | 37 | ||||
-rwxr-xr-x | onceaday/Dockerfile | 10 | ||||
-rwxr-xr-x | onceaday/crontab | 1 |
12 files changed, 103 insertions, 52 deletions
@@ -8,6 +8,7 @@ api.log config.json *.log *.db +*.sql* # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/config.json.example b/config.json.example new file mode 100755 index 0000000..6349b60 --- /dev/null +++ b/config.json.example @@ -0,0 +1,38 @@ +{ + "discord_webhook": "https://discord.com/api/webhooks/*********************/******************************************************", + "redditapi": + { + "client_id": "***************", + "client_secret": "************************", + "user_agent": "SmallYTChannelBot", + "username": "SmallYTChannelBot", + "password": "*******************" + }, + "imgurapi": + { + "client_id": "***************", + "client_secret": "********************************" + }, + "youtubeapi": + { + "developer_key": "******************************" + }, + "subreddit": "SmallYTChannel", + "comment_tail": "\n\n\n ^/u/SmallYTChannelBot ^*made* ^*by* ^/u/jwnskanzkwk. ^*For* ^*more* ^*information,* ^*read* ^*the* ^[FAQ.](https://www.reddit.com/user/SmallYTChannelBot/comments/a4u7qj/smallytchannelbot_faq/)", + "free_flairs": + [ + "Discussion", + "Meta", + "Collab" + ], + "mysql": + { + "host": "mysql", + "port": 3306, + "user": "root", + "passwd": "******************", + "database": "SmallYTChannel" + }, + "min_comment_len": 120, + "lambda_cost": 1 +} diff --git a/cron/Dockerfile b/cron/Dockerfile new file mode 100755 index 0000000..21aa4e4 --- /dev/null +++ b/cron/Dockerfile @@ -0,0 +1,12 @@ +FROM reg.reaweb.uk/smallytchannelbot +MAINTAINER Eden Attenborough "eddie.atten.ea29@gmail.com" +ARG DEBIAN_FRONTEND=noninteractive +RUN apt-get install -y tzdata cron mysql-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 ["entrypoint.sh"] diff --git a/onceaday/onceaday.py b/cron/daily.py index 7762d05..0f93d23 100755 --- a/onceaday/onceaday.py +++ b/cron/daily.py @@ -1,8 +1,7 @@ import os import sys -os.chdir(sys.argv[1]) -sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) +sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..")) from operator import itemgetter import subreddit @@ -37,6 +36,7 @@ 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() 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/onceaday/graph.py b/cron/graph.py index 723997b..723997b 100755 --- a/onceaday/graph.py +++ b/cron/graph.py diff --git a/cron/hourly.py b/cron/hourly.py new file mode 100644 index 0000000..daf46ec --- /dev/null +++ b/cron/hourly.py @@ -0,0 +1,43 @@ +import os +import sys + +sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..")) + +from discord_webhook import DiscordWebhook +from operator import itemgetter +import subprocess +import subreddit +import database +import datetime + +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__": + dump() + push() + diff --git a/onceaday/requirements.txt b/cron/requirements.txt index e3a623c..465e808 100755 --- a/onceaday/requirements.txt +++ b/cron/requirements.txt @@ -1 +1,2 @@ matplotlib==3.3.4 +discord-webhook diff --git a/docker-compose.yml b/docker-compose.yml index deb7375..5af0778 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,8 +15,8 @@ services: cron: build: context: . - dockerfile: ./onceaday/Dockerfile - image: reg.reaweb.uk/smallytchannelbot_oad + dockerfile: ./cron/Dockerfile + image: reg.reaweb.uk/smallytchannelbot_cron networks: - db-network external_links: diff --git a/exampleconfig.json b/exampleconfig.json deleted file mode 100755 index 0ad4134..0000000 --- a/exampleconfig.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "redditapi": - { - "client_id": "xxxxxxxxxxxxxxx", - "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxx", - "user_agent": "SmallYTChannelBot", - "username": "SmallYTChannelBot", - "password": "xxxxxxxxxxxxxxxxxxxxxxxxxxx" - }, - "imgurapi": - { - "client_id": "xxxxxxxxxxxxxxxx", - "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - }, - "youtubeapi": - { - "developer_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - }, - "subreddit": "SmallYTChannel", - "comment_tail": "\n\n\n ^/u/SmallYTChannelBot ^*made* ^*by* ^/u/jwnskanzkwk. ^*PM* ^*for* ^*bug* ^*reports.* ^*For* ^*more* ^*information,* ^*read* ^*the* ^[FAQ.](https://www.reddit.com/user/SmallYTChannelBot/comments/a4u7qj/smallytchannelbot_faq/)", - "free_flairs": - [ - "Discussion", - "Meta", - "Collab" - ], - "mysql": - { - "host": "localhost", - "port": 3306, - "user": "root", - "passwd": "xxxxxxxxxxxxxxxx", - "database": "SmallYTChannel" - }, - "min_comment_len": 120, - "lambda_cost": 2 -} diff --git a/onceaday/Dockerfile b/onceaday/Dockerfile deleted file mode 100755 index 6f733db..0000000 --- a/onceaday/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM reg.reaweb.uk/smallytchannelbot -MAINTAINER Eden Attenborough "eddie.atten.ea29@gmail.com" -ARG DEBIAN_FRONTEND=noninteractive -RUN apt-get install -y tzdata 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"] diff --git a/onceaday/crontab b/onceaday/crontab deleted file mode 100755 index c8ce32a..0000000 --- a/onceaday/crontab +++ /dev/null @@ -1 +0,0 @@ -@daily python3 /app/onceaday/onceaday.py /app/onceaday |