-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjustnosinclair.py
executable file
·84 lines (73 loc) · 3.03 KB
/
justnosinclair.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
import praw
from prawcore import exceptions
import re
import os
import time
account = 'JustNoSinclair'
reddit = praw.Reddit(account)
def read_text_set(filename):
result = []
try:
with open(filename) as f:
result = [_.strip() for _ in f if _]
except FileNotFoundError:
pass
return result
def remove_subreddit(sr_list, sr, error):
fn = error
fn.replace(" ", "_")
try:
sr.unsubscribe()
except Exception as e:
print ("Unsubscribe error: (" + type(e) + ") " + e)
fn_list = [_.lower() for _ in read_text_set("local_subreddits/" + fn)]
with open("local_subreddits/" + fn, "w") as f:
for lsr in sorted(fn_list):
f.write(lsr + "\n")
sr_list.remove(sr)
with open("local_subreddits/active", "w") as f:
for lsr in sorted(sr_list):
f.write(lsr + "\n")
print(sr + " is " + error + ", removed from list of local subreddits")
# ~ posts_replied_to = read_text_set("posts_replied_to")
posts_replied_to = [_.submission.id for _ in reddit.redditor(account).comments.new(limit=None)]
domains = {_.lower() for _ in read_text_set("sinclair_domains")}
local_subreddits = [_.display_name.lower() for _ in reddit.user.subreddits(limit=None)] or [_.lower() for _ in read_text_set("local_subreddits/active")] or ["politics"]
comment = ""
with open('comment_text', 'r') as f:
comment = f.read().rstrip()
try:
subreddits = (reddit.subreddit(sr) for sr in local_subreddits)
for subreddit in subreddits:
try:
for submission in subreddit.hot(limit=50):
submission_timely = time.time() - submission.created < 86400
if submission.id not in posts_replied_to \
and re.search("|".join(domains), submission.url, re.IGNORECASE) \
and submission_timely:
try:
print("SINCLAIR", "[" + subreddit.display_name.lower() + "]", submission.title, submission.url)
submission.reply(comment)
posts_replied_to.append(submission.id)
with open("posts_replied_to", "a") as f:
f.write(submission.id + "\n")
except exceptions.Forbidden:
remove_subreddit(local_subreddits, subreddit, "banned")
except Exception as e:
print(type(e))
print(e)
except exceptions.Forbidden:
remove_subreddit(local_subreddits, subreddit, "private")
except exceptions.NotFound:
remove_subreddit(local_subreddits, subreddit, "invalid")
except exceptions.Redirect:
remove_subreddit(local_subreddits, subreddit, "not_found")
except KeyError:
remove_subreddit(local_subreddits, subreddit, "removed")
except Exception as e:
print(type(e))
print(e)
except Exception as e:
print(type(e))
print(e)