-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
112 lines (94 loc) · 3.78 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import time
import sys
def saveStrings(it, filename):
with open(filename, 'w') as f:
for item in it:
f.write('%s\n' % item)
def loadStrings(filename):
with open(filename, 'r') as f:
for line in f.readlines():
yield line.rstrip('\n')
def saveRecentSubs(recent, filename):
saveStrings(('%s %d' % item for item in sorted(recent.items())), filename)
def loadRecentSubs(filename):
recent = {}
for line in loadStrings(filename):
sub_name, timestamp = line.split(' ')
recent[sub_name] = int(timestamp)
return recent
def saveSubreddits(subreddits, filename):
saveStrings(sorted(subreddits), filename)
def loadSubreddits(filename):
return set(loadStrings(filename))
def subName(sub):
return sub.display_name.lower()
def loadSubscriptions(reddit, recent_file):
print('Loading subscriptions...')
subscriptions = set(subName(s) for s in reddit.user.subreddits(limit=10000))
addToRecent(subscriptions - set(loadRecentSubs(recent_file).keys()), recent_file)
return subscriptions
def addToRecent(sub_names, recent_file):
recent = loadRecentSubs(recent_file)
for sub_name in sub_names:
recent[sub_name] = int(time.time())
saveRecentSubs(recent, recent_file)
def dropFromRecent(sub_names, recent_file):
recent = loadRecentSubs(recent_file)
for sub_name in sub_names:
recent.pop(sub_name, None)
saveRecentSubs(recent, recent_file)
def batch(operation, items, size=50):
items = list(items)
for i in range(0, len(items), size):
print('Sending batched %s request %d' % (operation, 1 + i // size))
getattr(items[i], operation)(items[i + 1:i + size])
def trimSubs(reddit, recent_file):
sub_names = set()
for sub_name, timestamp in loadRecentSubs(recent_file).items():
if timestamp + 60 * 60 < time.time():
sub_names.add(sub_name)
if not sub_names:
return
print('Trimming %s' % ', '.join('r/' + sub_name for sub_name in sub_names))
dropFromRecent(sub_names, recent_file)
batch('unsubscribe', (reddit.subreddit(sub_name) for sub_name in sub_names))
def updateBlacklist(blacklist_file, recent_file, subscriptions):
blacklist = loadSubreddits(blacklist_file) - subscriptions
to_blacklist = set(loadRecentSubs(recent_file).keys()) - subscriptions
for sub_name in to_blacklist:
print('Blacklist r/%s' % sub_name)
blacklist |= to_blacklist
saveSubreddits(blacklist, blacklist_file)
dropFromRecent(blacklist, recent_file)
return blacklist
def tick(reddit, recent_file, blacklist_file):
subscriptions = loadSubscriptions(reddit, recent_file)
blacklist = updateBlacklist(blacklist_file, recent_file, subscriptions)
to_update = []
to_hide = []
to_subscribe = []
print('Loading front page...')
for submission in reddit.subreddit('all').hot(limit=1000):
sub_name = subName(submission.subreddit)
if sub_name in subscriptions:
# print('Allowing r/%-30s %s' % (sub_name, submission.title[:100]))
to_update.append(sub_name)
elif sub_name in blacklist:
print('Hiding r/%-30s %s' % (sub_name, submission.title[:100]))
to_hide.append(submission)
else:
print('Subbing r/%-30s %s' % (sub_name, submission.title[:100]))
to_update.append(sub_name)
to_subscribe.append(submission.subreddit)
batch('hide', to_hide)
batch('subscribe', to_subscribe)
addToRecent(to_update, recent_file)
trimSubs(reddit, recent_file)
def run(reddit, recent_file, blacklist_file):
while True:
try:
tick(reddit, recent_file, blacklist_file)
except:
print('Ran into an error: %s' % sys.exc_info()[0])
print('Sleeping...')
time.sleep(60)