-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtechmodnotify.py
180 lines (145 loc) · 6.8 KB
/
techmodnotify.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python2.7
# =============================================================================
# IMPORTS
# =============================================================================
import praw
import MySQLdb
import ConfigParser
import time
import parsedatetime.parsedatetime as pdt
import pprint
import logging
from datetime import datetime, timedelta
from requests.exceptions import HTTPError, ConnectionError, Timeout
from praw.errors import ExceptionList, APIException, InvalidCaptcha, InvalidUser, RateLimitExceeded
from socket import timeout
from pytz import timezone
from multiprocessing import Process
# =============================================================================
# GLOBALS
# =============================================================================
# Reads the config file
config = ConfigParser.ConfigParser()
config.read("techmodnotify.cfg")
# Reddit info
user_agent = ("TechModNotify bot by /u/zathegfx")
reddit = praw.Reddit(user_agent = user_agent)
USER = config.get("Reddit", "username")
PASS = config.get("Reddit", "password")
DB_HOST = config.get("SQL", "host")
DB_NAME = config.get("SQL", "db")
DB_USER = config.get("SQL", "user")
DB_PASS = config.get("SQL", "pass")
DB_TABLE = config.get("SQL", "table")
# =============================================================================
# Functions
# =============================================================================
def save_to_db(db, submissionID, permalink, author):
"""
Saves the permalink submission, the time, and the author to the DB
"""
cursor = db.cursor()
currentTime1 = datetime.now(timezone('UTC'))
currentTime = format(currentTime1, '%Y-%m-%d %H:%M:%S')
replyTime1 = currentTime1 + timedelta(0,300)
replyTime = format(replyTime1, '%Y-%m-%d %H:%M:%S')
cmd = "SELECT * FROM " + DB_TABLE + " WHERE submissionID = %s"
cursor.execute(cmd, [submissionID])
results = cursor.fetchall()
if (len(results) > 0):
return True;
else:
cmd = "INSERT INTO " + DB_TABLE + " (submissionID, currentTime, replyTime, permalink, author) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(cmd, [submissionID, currentTime, replyTime, permalink, author])
print currentTime + ' - Inserted new record into table: ' + submissionID
db.commit()
def search_db():
"""
Search the database for any records that are over 10 minutes
"""
while True:
db = MySQLdb.connect(DB_HOST, DB_USER, DB_PASS, DB_NAME )
cursor = db.cursor()
currentTime1 = datetime.now(timezone('UTC'))
currentTime = format(currentTime1, '%Y-%m-%d %H:%M:%S')
cmd = "SELECT * FROM " + DB_TABLE + " WHERE replyTime < %s"
cursor.execute(cmd, [currentTime])
results = cursor.fetchall()
alreadySent = []
if ( len(results) > 0 ):
for row in results:
if row[0] not in alreadySent:
submission = reddit.get_submission(submission_id=row[1])
hasFlair = submission.link_flair_css_class
k = str(hasFlair)
if (k == "None"):
flagDelete = False
flagDelete = new_reply(row[4], row[5])
if flagDelete:
cmd = "DELETE FROM " + DB_TABLE + " WHERE id = %s"
cursor.execute(cmd, [row[0]])
db.commit()
print currentTime + ' - No flair detected - send message - deleting record - ' + row[1]
else:
cmd = "DELETE FROM " + DB_TABLE + " WHERE id = %s"
cursor.execute(cmd, [row[0]])
db.commit()
print currentTime + ' - Flair deteced - deleting record - ' + row[1]
alreadySent.append(row[0])
time.sleep(5)
def new_reply(permalink, author):
reddit.login(USER, PASS)
try:
reddit.send_message(author, 'Message from /r/technology',
"Hello " + author + ","
"\n\nWe appreciate your contribution to /r/technology! We noticed "
"that you haven't flaired your [post](" + permalink + ") yet. In order to keep this sub "
"organized, every post is required to be flaired with respect to "
"the articles main focus. This allows the readers of /r/technology "
"to easily find the articles that most interest them. "
"\n\n If you could take a moment out of your time to properly flair "
"your post, we would gladly apprieciate it. Instruction on properly "
"flairing your post can be found [here](http://www.reddit.com/r/technology/wiki/flair). "
"\n\n Thank you!"
"\n\n Techonology Mod Team"
"\n\n_____\n\n"
"\n\n *This is a bot - if you have any questions or need to report an issue regarding "
"this bot, please [message the mods](https://www.reddit.com/message/compose?to=%2Fr%2Ftechnology) immediately*"
"\n\n**Your Post:** " + permalink + "")
print "Message Sent!"
return True
except InvalidUser as err:
print "InvalidUser", err
return True
except APIException as err:
print "APIException", err
return False
except IndexError as err:
print "IndexError", err
return False
except (HTTPError, ConnectionError, Timeout, timeout) as err:
print "HTTPError", err
return False
except RateLimitExceeded as err:
print "RateLimitExceeded", err
time.sleep(10)
def main():
reddit.login(USER, PASS)
db = MySQLdb.connect(DB_HOST, DB_USER, DB_PASS, DB_NAME )
print "start"
while True:
try:
for submission in praw.helpers.submission_stream(reddit, 'technology', limit=5, verbosity=0):
submissionID = submission.id
author = submission.author
permalink = submission.permalink
save_to_db(db, submissionID, permalink, author)
except Exception as err:
print 'There was an error in main(): '
print err
# =============================================================================
# RUNNER
# =============================================================================
if __name__ == '__main__':
Process(target=main).start()
Process(target=search_db).start()