forked from rjailbreak/flairbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flairbot.py
99 lines (90 loc) · 3.47 KB
/
flairbot.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
import praw
import urllib.request
import json
import creds
BOT_NAME = creds.BOT_NAME
USER_AGENT = creds.USER_AGENT
print("||===============================Starting flairbot.py===============================||")
url = "https://raw.githubusercontent.com/rjailbreak/flairbot/master/data.json"
response = urllib.request.urlopen(url)
data = json.loads(response.read())
print()
IOSTYPE = data['versions']
SUB_NAMES = data['subreddits']
DEVICES = dict()
DEVICES.update(data['devices']['iPad'])
DEVICES.update(data['devices']['iPhone'])
DEVICES.update(data['devices']['iPod'])
def main():
global BOT_NAME
global USER_AGENT
global IOSTYPE
global SUB_NAMES
global DEVICES
r = praw.Reddit(BOT_NAME, user_agent=USER_AGENT)
print('Searching Inbox.')
pms = r.inbox.unread(mark_read=True, limit=100)
for pm in pms:
if not pm.author:
pm.mark_read()
print("User was deleted")
continue
pauthor = pm.author
pbody = pm.body
psubject = pm.subject
if psubject == "Flair Request":
arr = pbody.split("\n")
error = 0
device = ""
ios = ""
sub = 3
# device
try:
if arr[0].startswith("0"):
value = str(arr[0][1:])
device = DEVICES[value]
else:
error = 1
if arr[1].startswith("1"):
value = int(arr[1][1:])
if value != 0:
device += ", "
ios = "iOS " + IOSTYPE[value]
else:
error = 1
if arr[2].startswith("2"):
sub = str(arr[2][1:])
except:
error = 1
if error == 1:
pauthor.message('Flair Rejected on r/jailbreak',
('You edited something in the message before sending or ' +
'something went wrong. Please try again.'))
print("Flair rejected: " + device + ios)
else:
flairText = device + ios
if sub != 0:
try:
r.subreddit(SUB_NAMES[sub]).flair.set(
redditor=pauthor.name, text=flairText, css_class="flair-default")
subText = "r/" + SUB_NAMES[sub]
except Exception:
print("User deleted")
pm.mark_read()
continue
else:
r.subreddit('jailbreak').flair.set(redditor=pauthor.name,
text=flairText, css_class="flair-default")
r.subreddit('iOSthemes').flair.set(redditor=pauthor.name,
text=flairText, css_class="flair-default")
subText = "r/jailbreak and r/iOSthemes"
pauthor.message('Flair Approved', 'Your subreddit flair, "' + flairText +
'" on ' + subText + ' has been approved. Thank you for using ' +
'u/JailbreakFlairBot which was created by u/ibbignerd.')
print("Approved flair, \"" + flairText + "\" for " + pauthor.name + " on " + subText)
pm.mark_read()
else:
pm.mark_read()
print("Done!")
if __name__ == '__main__':
main()