-
Notifications
You must be signed in to change notification settings - Fork 6
/
resub.py
executable file
·110 lines (92 loc) · 3.34 KB
/
resub.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
#!/usr/bin/env python3
import praw
import argparse
import json
parser = argparse.ArgumentParser(
description='Resubscribe to your old subreddits.')
parser.add_argument('--import', '-i', action="store_true",
help="Specify -i to import to the user, the default is to \
save from a user.")
parser.add_argument('--user', '-u', help="Reddit username.")
parser.add_argument('--file', '-f', help="Provide a filename to use.")
class Resub:
_r = praw.Reddit(user_agent='reddit-resub 2017-05-08')
def __init__(self, subscribe, user=None, filename=None):
self._user = self.get_user()
if not filename:
filename = '{user}.subs'.format(user=self._user)
self._filename = filename
if subscribe:
print("Subscribing to subreddits in '{file}'".format(
file=filename, user=self.get_user()))
self.sub_clever()
else:
print("Exporting {user}'s subreddits to {file}".format(
file=filename, user=self.get_user()))
self.export_subs()
def unsub(self, subreddit):
'''
Unsubscribes and prints to STDOUT
'''
self._r.subreddit(subreddit).unsubscribe()
print("Unsubscribed from subreddit {sub}".format(sub=subreddit))
def sub(self, subreddit):
'''
Try to subscribe to a given subreddit.
'''
self._r.subreddit(subreddit).subscribe()
print("Subscribed to {sub}".format(sub=subreddit))
def get_wanted_subs(self):
'''
Opens the list of subreddits we want to subscribe to
from a json fille.
'''
fh = open(self._filename, 'r')
subs = json.load(fh)
fh.close()
return list(set(subs))
def sub_clever(self):
'''
Use the minimal number of API calls to subscribe / unsubscribe.
'''
wanted_subs = set(self.get_wanted_subs())
current_subs = set(self.get_subs())
for sub in wanted_subs - current_subs:
if sub in wanted_subs:
# Subscribe to wanted subs we're not already subbed to
self.sub(sub)
for sub in current_subs - wanted_subs:
# Unsub from everything remaining
self.unsub(sub)
def get_user(self):
'''
Specifically returns the username from the Reddit object, not the one
specified by the user / script. This is guaranteed to be correct in
other words.
'''
return str(self._r.user.me())
def export_subs(self):
'''
Saves the user's subreddits to file.
'''
fh = open(self._filename, 'w')
subs = sorted(self.get_subs(), key=str.lower)
json.dump(subs, fh, indent=2)
fh.close()
def get_subs(self):
'''
Returns a unique list of subreddits to which the user is subscribed.
'''
my_subs = set()
for sub in self._r.user.subreddits(limit=None):
my_subs.add(str(sub))
return list(my_subs)
if __name__ == "__main__":
args = parser.parse_args()
# Boolean, True if --import / -i
# If true then subscribe, if false then export to file.
subscribe = getattr(args, 'import')
r = Resub(subscribe,
filename=getattr(args, 'file'),
user=getattr(args, 'user')
)