2022-07-05 11:50:53 +00:00
|
|
|
try:
|
|
|
|
import time
|
|
|
|
import requests
|
|
|
|
import praw
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
print("Please install the required python modules with pip install -r requirements.txt or pip3 install -r requirements.txt!")
|
|
|
|
exit()
|
2022-07-05 11:47:12 +00:00
|
|
|
current_time = time.strftime("%H-%M-%S")
|
|
|
|
current_path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
2022-07-05 12:28:32 +00:00
|
|
|
subreddit_name = "memes"
|
2022-07-05 11:47:12 +00:00
|
|
|
|
|
|
|
nswf = False
|
|
|
|
|
|
|
|
def debugging_message(message : str):
|
|
|
|
print("["+current_time+"][DEBUG] "+message)
|
|
|
|
|
2022-07-05 11:50:53 +00:00
|
|
|
if not os.path.isfile(current_path+"/used-posts.json"):
|
|
|
|
with open(current_path+"/used-posts.json", "w") as used_posts_file:
|
|
|
|
json.dump({"ids": []}, used_posts_file)
|
|
|
|
|
2022-07-05 11:47:12 +00:00
|
|
|
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)
|
|
|
|
|
2022-07-05 12:28:32 +00:00
|
|
|
debugging_message("Getting a post from r/"+subreddit_name)
|
2022-07-05 11:47:12 +00:00
|
|
|
subreddit = reddit.subreddit(subreddit_name)
|
2022-07-05 12:28:32 +00:00
|
|
|
posts = subreddit.top(time_filter="day", limit=5)
|
|
|
|
for post in posts:
|
2022-07-05 11:47:12 +00:00
|
|
|
try:
|
|
|
|
if post.id not in used_posts['ids']:
|
2022-07-05 12:28:32 +00:00
|
|
|
print(post.url)
|
2022-07-05 11:47:12 +00:00
|
|
|
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)
|