| 1 | import praw
|
| 2 | import config
|
| 3 | import os.path
|
| 4 | import json
|
| 5 | import time
|
| 6 |
|
| 7 | def authentication():
|
| 8 | print ("Authenticating...")
|
| 9 | reddit = praw.Reddit(username = config.username,
|
| 10 | password = config.password,
|
| 11 | client_id = config.client_id,
|
| 12 | client_secret = config.client_secret,
|
| 13 | user_agent = "WFOP_Flair_Timer_MinecraftHelp")
|
| 14 | print ("Authenticated as {}.".format(reddit.user.me()))
|
| 15 | return reddit
|
| 16 |
|
| 17 | def main(reddit, posts: dict):
|
| 18 | while True:
|
| 19 | for submission in reddit.subreddit(config.subreddit).new(limit=config.searchlimit):
|
| 20 | if not submission.saved:
|
| 21 | if submission.id not in posts.keys() and submission.link_flair_text == config.flair_text:
|
| 22 | posts[submission.id] = time.time()
|
| 23 | print(f"Post {submission} has been flaired {config.flair_text}")
|
| 24 | if submission.id in posts.keys() and submission.link_flair_text != config.flair_text:
|
| 25 | posts.pop(submission.id)
|
| 26 | print(f"Post {submission} has been unflaired {config.flair_text}")
|
| 27 |
|
| 28 | for submission in posts:
|
| 29 | if time.time() > posts[submission] + (config.hours * 60 * 60):
|
| 30 | posts.pop(submission)
|
| 31 | reddit.submission(submission).save()
|
| 32 | reddit.subreddit(config.subreddit).message(subject=f"{config.messagetitle}", message=f"It has been {config.hours/24} day(s) since this was flaired [{config.flair_text}](https://old.reddit.com{reddit.submission(submission).permalink})")
|
| 33 | print(f"Post {submission} has been flaired {config.flair_text} for {config.hours/24} days, sent modmail")
|
| 34 | break
|
| 35 |
|
| 36 | save_posts(posts)
|
| 37 | time.sleep(config.interval)
|
| 38 |
|
| 39 | def load_posts():
|
| 40 | if not os.path.exists("posts.json"):
|
| 41 | with open("posts.json", "w+") as file:
|
| 42 | json.dump({}, file)
|
| 43 | with open("posts.json", "r+") as file:
|
| 44 | data = json.load(file)
|
| 45 | return data
|
| 46 |
|
| 47 | def save_posts(data):
|
| 48 | with open('posts.json', 'w+') as file:
|
| 49 | json.dump(data, file)
|
| 50 |
|
| 51 |
|
| 52 | while True:
|
| 53 | try:
|
| 54 | posts = load_posts()
|
| 55 | main(reddit = authentication(), posts = posts)
|
| 56 | except Exception as e:
|
| 57 | print(e)
|