From a85f5a04a59441fa737a15d97fb185c4bace978f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Tue, 5 Jul 2022 13:47:12 +0200 Subject: [PATCH] added image adder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- save-image.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ server.py | 0 2 files changed, 44 insertions(+) create mode 100644 save-image.py delete mode 100644 server.py diff --git a/save-image.py b/save-image.py new file mode 100644 index 0000000..2d38e9b --- /dev/null +++ b/save-image.py @@ -0,0 +1,44 @@ +import time +import requests +import praw +import os +import json +current_time = time.strftime("%H-%M-%S") +current_path = os.path.dirname(os.path.realpath(__file__)) + +subreddit_name = "r/memes" + +nswf = False + +def debugging_message(message : str): + print("["+current_time+"][DEBUG] "+message) + +reddit = praw.Reddit(client_id="TB3qNrAO2cnRkpmI-Vd8hg", + client_secret="AO61coSKJ8O8KLlHAqP6W-nvufdgUw", + user_agent="Meme of the day" +) + +with open(current_path+"/used-posts.json", "r") as used_posts_file: + used_posts = json.load(used_posts_file) + +debugging_message("Getting a post from r/"+subreddit_name) +subreddit = reddit.subreddit(subreddit_name) +hot_post = subreddit.hot() +for post in hot_post: + try: + if post.id not in used_posts['ids']: + if post.url.endswith(".png") or post.url.endswith(".jpg"): + debugging_message("Found a new post: "+post.title+" ("+post.id+")") + print(post.url) + with open(current_path+"/used-posts.json", "w") as used_posts_file: + used_posts['ids'].append(post.id) + json.dump(used_posts, used_posts_file) + debugging_message("Saving post to file") + with open(current_path+'/wallpaper.png', "wb") as file: + file.write(requests.get(post.url).content) + debugging_message("Saving post to file done") + except: + debugging_message("Error while getting post skipping and continuing") + with open(current_path+"/used-posts.json", "w") as used_posts_file: + used_posts['ids'].append(post.id) + json.dump(used_posts, used_posts_file) \ No newline at end of file diff --git a/server.py b/server.py deleted file mode 100644 index e69de29..0000000