From 8932f0ca188092891f8798f77c0643cc69f6f29f Mon Sep 17 00:00:00 2001 From: jwansek Date: Mon, 7 Aug 2023 17:12:05 +0100 Subject: Changed to work with new twitter API, changed cron system --- Dockerfile | 18 +++++++++++------- bot.py | 32 +++++++++++++++++++++++--------- entrypoint.sh | 4 ++++ 3 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index b52b107..8dcf570 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,15 @@ -FROM debian:latest -MAINTAINER Eden Attenborough "eddie.atten.ea29@gmail.com" -RUN apt-get update --allow-releaseinfo-change +FROM ubuntu:latest +MAINTAINER Eden Attenborough "eda@e.email" +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 tzdata python3-pip build-essential pkg-config cron libjpeg-dev zlib1g-dev libfreetype6-dev +RUN mkdir app COPY . /app WORKDIR /app -COPY crontab /etc/cron.d/oad-crontab -RUN chmod 0644 /etc/cron.d/oad-crontab && crontab /etc/cron.d/oad-crontab -ARG PIP_ONLY_BINARY=cmake +RUN touch .docker RUN pip3 install -r requirements.txt -ENTRYPOINT ["cron", "-f"] + +RUN echo "0 */3 * * * root python3 /app/bot.py > /proc/1/fd/1 2>/proc/1/fd/2" > /etc/crontab +ENTRYPOINT ["bash"] +CMD ["entrypoint.sh"] diff --git a/bot.py b/bot.py index 1148b93..f83ba1d 100755 --- a/bot.py +++ b/bot.py @@ -1,23 +1,37 @@ import os -os.chdir("/app") +os.chdir(os.path.dirname(__file__)) -from twython import Twython import get_images +import tweepy -os.environ["SSL_CERT_DIR"] = "/etc/ssl/certs" +def twitter_post(message, impath): + auth = tweepy.OAuth1UserHandler( + get_images.CONFIG["twitterapi"]["consumer_key"], + get_images.CONFIG["twitterapi"]["consumer_secret"], + get_images.CONFIG["twitterapi"]["access_token"], + get_images.CONFIG["twitterapi"]["access_token_secret"], + ) -twitter = Twython(*get_images.CONFIG["twitterapi"].values()) + api = tweepy.API(auth) + + media_upload = api.simple_upload(impath) + media_id = media_upload.media_id + + client = tweepy.Client( + **get_images.CONFIG["twitterapi"] + ) + + response = client.create_tweet(text = message, media_ids = [media_id]) + get_images.logging.info(str(response)) + return response def post(): images = get_images.main() while images is None: images = get_images.main() - impath, source, text = images - with open(impath, "rb") as img: - response = twitter.upload_media(media = img) - message = f"{text} ({source})" - out = twitter.update_status(status=message, media_ids=[response["media_id"]]) + impath, source, text = images + twitter_post("%s (%s)" % (text, source), impath) get_images.logging.info("Posted to twitter.") if __name__ == "__main__": diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..610bf84 --- /dev/null +++ b/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 -- cgit v1.2.3