This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
forked from x89/reddit-resub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit.py
executable file
·151 lines (136 loc) · 4.35 KB
/
reddit.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/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\
Default is to save from a user (safe).")
parser.add_argument('--user', '-u', help="Reddit username.")
parser.add_argument('--file', '-f', help="Provide a filename to use.")
class Resub:
_r = praw.Reddit('reddit-resub')
_default_subreddits = (
'announcements',
'art',
'askreddit',
'askscience',
'aww',
'blog',
'books',
'creepy',
'dataisbeautiful',
'diy',
'documentaries',
'earthporn',
'explainlikeimfive',
'fitness',
'food',
'funny',
'futurology',
'gadgets',
'gaming',
'getmotivated',
'gifs',
'history',
'iama',
'internetIsBeautiful',
'jokes',
'lifeprotips',
'listentothis',
'mildlyinteresting',
'movies',
'music',
'news',
'nosleep',
'nottheonion',
'oldschoolcool',
'personalfinance',
'philosophy',
'photoshopbattles',
'pics',
'science',
'showerthoughts',
'space',
'sports',
'television',
'tifu',
'todayilearned',
'twoXChromosomes',
'upliftingnews',
'videos',
'worldnews',
'writingprompts',
)
def __init__(self, subscribe, user=None, filename=None):
self._r.login(user)
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.import_subs()
else:
print("Exporting {user}'s subreddits to {file}".format(file=filename, user=self.get_user()))
self.export_subs()
def unsub_defaults(self):
'''
Unsubscribes from all default subreddits.
'''
print("Unsubscribing from all default subreddits")
for sub in self._default_subreddits:
try:
self._r.unsubscribe(sub)
print("Unsubscribed from default subreddit {sub}".format(sub=sub))
except praw.errors.NotFound:
print("Not subscribed to %s, skipping" % sub)
def import_subs(self):
'''
Uses subreddits defined in JSON format in a file to import to a Reddit
user account. Unsubscribes from default subreddits first.
'''
fh = open(self._filename, 'r')
new_subs = json.load(fh)
fh.close()
self.unsub_defaults()
my_subs = list(
set(self.get_subs()) - (set(self._default_subreddits) | set(new_subs))
)
for sub in my_subs:
self._r.unsubscribe(sub)
print("Unsubscribed from subreddit {sub}".format(sub=sub))
for sub in new_subs:
if sub not in my_subs:
try:
self._r.subscribe(sub)
print("Subscribed to {sub}".format(sub=sub))
except praw.errors.Forbidden:
print("Subreddit %s is private, skipping." % 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)
def export_subs(self):
'''
Saves the user's subreddits to file.
'''
fh = open(self._filename, 'w')
json.dump(self.get_subs(), fh)
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.get_my_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'))