-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
156 lines (129 loc) · 5.34 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
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
152
153
154
155
156
import praw
import time
import string
import random
SUBREDDIT_NAME = 'warriors'
import credentials
CLIENT_ID = credentials.reddit['CLIENT_ID']
CLIENT_SECRET = credentials.reddit['CLIENT_SECRET']
USERNAME = credentials.reddit['USERNAME']
PASSWORD = credentials.reddit['PASSWORD']
USER_AGENT = 'script: reply to comments that contain iggy in /r/warriors (by /u/dont-call-me-iggy)'
def authenticate():
print("Authenticating...")
return praw.Reddit(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
user_agent=USER_AGENT,
username=USERNAME,
password=PASSWORD)
NBA_DESCRIPTIONS = set([
'three-time NBA champion',
'NBA Finals MVP',
'NBA All-Star',
])
PERSONAL_DESCRIPTIONS = set([
'bearer of the best-looking biceps in the NBA',
'noted LeBron stopper',
])
def generate_response():
return '''
Hi, you seem to have used "Iggy" to refer to Andre Iguodala, {} and {}. In [several](https://www.youtube.com/watch?v=-qSY3OVrS9E) [instances](https://twitter.com/andre/status/908090035697631232) Andre has said that he would rather be called by a different nickname.
*****
^(If you have complaints or suggestions on how to improve this bot please message myself or the moderators!)
'''.format(random.sample(NBA_DESCRIPTIONS, 1)[0], random.sample(PERSONAL_DESCRIPTIONS, 1)[0])
def has_iggy(text):
text = text.lower()
if "azalea" in text:
return False
if "'iggy'" in text or '"iggy"' in text:
return False
# strip punctuation and split on spaces
exclude = set(string.punctuation) - set("-") # for dont-call-me-iggy
text = ''.join(ch if ch not in exclude else " " for ch in text).split()
if "bot" in text:
return False
iggies = []
for g in range(2, 5):
for y in range(1, 8):
iggies.append('i' + 'g'*g + 'y'*y)
return any(iggy in text for iggy in iggies)
start_time = time.time()
# keep track of the time last posted
posted_last_time = time.time()
# keep track of last thread ids
# we limit it to 100 so that the array doesn't grow huge
posted_thread_ids = []
def in_game_thread(post, reddit):
submission_id = post.submission.id
submission = reddit.submission(id=submission_id)
if "game thread" in submission.title.lower():
return True
if "daily discussion" in submission.title.lower():
return True
return False
def watch_stream():
print('Starting comment stream...')
reddit = authenticate()
print("Authenticated as {}".format(reddit.user.me()))
for comment in reddit.subreddit(SUBREDDIT_NAME).stream.comments():
global posted_last_time
global posted_thread_ids
if comment.author == reddit.user.me():
print ("comment #", comment.id, "not valid because from self")
continue
if comment.created_utc < start_time:
print ("comment #", comment.id, "not valid because of age")
continue
if comment.submission.id in posted_thread_ids:
print ("comment #", comment.id, "not valid because comment is already posted in thread")
continue
if in_game_thread(comment, reddit):
print ("comment #", comment.id, "not valid because comment is in a game thread")
continue
if time.time() - posted_last_time < 60 * 60 * 3: # three hours
print ("comment #", comment.id, "not valid because another comment was posted in the last three hours")
# add the thread_id anyway, so that we don't reply to a 'new' iggy while ignoring an 'old' one
posted_thread_ids = posted_thread_ids[-100:] # keep newest 100
posted_thread_ids.append(comment.submission.id)
continue
if comment.saved:
continue
if has_iggy(comment.body):
print ("processing comment #", comment.id)
comment.save()
reply = comment.reply(generate_response())
posted_last_time = time.time()
posted_thread_ids = posted_thread_ids[-100:] # keep newest 100
posted_thread_ids.append(comment.submission.id)
print ("Posted to: https://www.reddit.com/r/" + SUBREDDIT_NAME + "/comments/" + comment.submission.id + "//" + reply.id + "")
def run_tests():
print ("Should all be true:")
print (has_iggy("blah blah blah iggy"))
print (has_iggy("iggy!!!!"))
print (has_iggy("That reaction after the Iggy 3 was all time"))
print (has_iggy("KLAY WITH 31. IGGY 4 FOR 5 FROM THREE. KD WITH 32. My lord."))
print (has_iggy("iggy!!three!!!"))
print (has_iggy("Talk about flipping the switch! Iggy's playing smart and intense."))
print (has_iggy("Iggy, Dwest and JaVale all stepped up big time. Huge win!"))
print (has_iggy("DONT CALL HIM IGGYYYYYY!!!!"))
print (has_iggy("IGGGYYYY"))
print ()
print ("Should all be false:")
print (has_iggy("andre igoudala"))
print (has_iggy("getting jiggy with it"))
print (has_iggy("dont call him 'iggy'!"))
print (has_iggy('dont call him "iggy"!'))
print (has_iggy("iggy-dala"))
print (has_iggy("iggy azalea"))
print (has_iggy("paging /u/dont-call-me-iggy"))
print ("Should return 10 different sample responses:")
for i in range(1):
print (generate_response())
if __name__ == '__main__':
watch_stream()
# if you want to run tests
# run_tests()
# pass