-
Notifications
You must be signed in to change notification settings - Fork 0
/
flair.py
82 lines (72 loc) · 2.7 KB
/
flair.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
# coding=utf-8
import praw
import sqlite3
import time
import datetime
con = sqlite3.connect('estados_municipios.db')
cursor = con.cursor()
USER = ''
PASSWORD = ''
CLIENT_ID = ''
CLIENT_SECRET = ''
USER_AGENT = ''
def dbLookup(msg):
if len(msg.split(',')) != 2:
# procura na lista de paises
query = 'SELECT id FROM paises WHERE nome == ?;'
cursor.execute(query, (msg,))
if cursor.fetchone():
return True
else:
return False
else:
cidade = msg.split(',')[0].strip()
estado = msg.split(',')[1].strip()
# check cidade pertence ao estado
query = 'SELECT estados.id FROM municipios JOIN estados ON municipios.estados_id == estados.id WHERE municipios.nome == ? AND estados.uf == ?;'
cursor.execute(query, (cidade, estado))
if not cursor.fetchone():
return False
return True
def write_log(text):
print(text)
log = open('log_flairbot.txt', 'a')
log.write('{} {}\n'.format(str(datetime.datetime.now()),text))
log.close()
def main():
r = praw.Reddit(client_id = CLIENT_ID,
client_secret = CLIENT_SECRET,
user_agent = USER_AGENT,
username = USER,
password = PASSWORD)
print(r.user.me()) #forca exception se login falhou
print('logged in')
while True:
time.sleep(0.5)
try:
for msg in r.inbox.unread():
try:
write_log('AUTHOR: {} - SUBJECT: {} - BODY: {}'.format(msg.author, msg.subject, msg.body))
except UnicodeEncodeError:
write_log('AUTHOR: {} - unprintable chars'.format(msg.author))
sub = r.subreddit('brasil')
if msg.subject and msg.subject.lower() == 'flair':
if dbLookup(msg.body):
estado = 'world' if len(
msg.body.split(',')) < 2 else msg.body.split(',')[1].strip()
sub.flair.set(msg.author, msg.body, estado)
msg.author.message('flair', 'Flair configurado.')
write_log('flair ok')
else:
msg.author.message('flair', 'Configuração de flair falhou.')
write_log('flair fail')
if msg.subject and msg.subject.lower() == 'remover flair':
sub.flair.delete(msg.author)
msg.author.message('flair', 'Flair removido.')
write_log('remove flair ok')
r.inbox.mark_read([msg])
except Exception as e:
print(e)
pass
if __name__ == '__main__':
main()