-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReddit Bot.py
More file actions
73 lines (57 loc) · 2.39 KB
/
Reddit Bot.py
File metadata and controls
73 lines (57 loc) · 2.39 KB
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
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 5 10:20:58 2019
Reddit Bot
@author: coryr
"""
import praw
import random as rand
from time import time as time
from time import sleep
"""
clientID: Given By Reddit, TYPE: STR
clientSecret: Given By Reddit, TYPE: STR
USERNAME: username for bot account, TYPE: STR
PASSWORD: password for bot account, TYPE: STR
mainUsername: Username of Personal Account, TYPE: STR
subReddit: Subreddit to post on, TYPE: STR
triggerPhrases1: List of phrases to search for, TYPE: LIST(STR)
triggerPhrases2: List of phrases to search for, TYPE: LIST(STR)
responses1: List of Repsonses if a phrase in triggerPhrases1 occurs in the post, TYPE: LIST(STR)
responses2: List of Repsonses if a phrase in triggerPhrases2 occurs in the post, TYPE: LIST(STR)
"""
def run_bot(clientID, clientSecret, USERNAME, PASSWORD, mainUsername, subReddit,
triggerPhrases1, triggerPhrases2, responses1, responses2):
try:
reddit = praw.Reddit(client_id = clientID,
client_secret = clientSecret,
username = USERNAME,
password = PASSWORD,
user_agent = "repostbot by /u/" + mainUsername)
except:
raise ValueError("Log In failed, check input values")
try:
subreddit = reddit.subreddit(subReddit)
except:
raise ValueError("Invalid Subreddit, please try again")
start_time = time()
for submission in subreddit.stream.submissions():
if submission.created_utc < start_time:
continue
try:
normalized_title = submission.title.lower()
for trigger1 in triggerPhrases1:
if trigger1 in normalized_title:
index = rand.randint(0,len(responses1)-1)
submission.reply(responses1[index])
print("posted: " , responses1[index])
sleep(605)
for trigger2 in triggerPhrases2:
if trigger2 in normalized_title:
index = rand.randint(0,len(responses2)-1)
submission.reply(responses2[index])
print("posted: " , responses2[index])
sleep(605)
continue
except:
continue