/
corpbot.py
73 lines (59 loc) · 2.22 KB
/
corpbot.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
#!/usr/bin/env python3
import praw
import pickle
from pyaml import yaml
import time
import os
#Load config
with open("config.yaml", "r") as f:
config = yaml.load(f)["api"]
#Load brandlist
with open("brandlist.txt", "r") as f:
brands = [x for x in f.read().split("\n") if x != ""]
#Defaults
with open("defaults.txt", "r") as f:
defaults = [x for x in f.read().split("\n") if x != ""]
print("Loaded {} brands".format(len(brands)))
def save_scanned(scanned):
"""
Save the posts we've already scanned
"""
with open("already_scanned.dat", "wb") as f:
pickle.dump(scanned, f)
#Try to load the things we've seen before
#Avoids replying to the same posts
try:
with open("already_scanned.dat", "rb") as f:
already_scanned = pickle.load(f)
except Exception:
already_scanned = []
save_scanned(already_scanned)
print("Identifying as {}".format(config["username"]))
reddit = praw.Reddit(
client_id = config["clientid"],
client_secret = config["secret"],
username = config["username"],
password = config["password"],
user_agent = config["uagent"]
)
if not os.path.exists("ads-{}".format(time.strftime("%Y-%m-%d"))):
with open("ads-{}".format(time.strftime("%Y-%m-%d")), "a+") as f:
f.write("| Subreddit | Title | Brand |\n")
f.write("|:----------|-----|:-----:|\n")
reddit.login(config["username"], config["password"])
def test_post(submission):
if submission.id not in already_scanned:
# Make sure we don't see it again
already_scanned.append(submission.id)
save_scanned(already_scanned)
for brand in brands:
if " {} ".format(brand) in submission.title:
print("Possible match: {} [{}]".format(submission.title, brand))
with open("ads-{}".format(time.strftime("%Y-%m-%d")), "a+") as f:
f.write("| {} | [{}]({}) | {} |\n".format(submission.subreddit, submission.title,submission.permalink, brand))
while 1:
# Retrieve r/all
for sub in defaults:
subreddit = reddit.get_subreddit(sub)
for post in subreddit.get_new():
test_post(post)