-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
executable file
·145 lines (127 loc) · 3.25 KB
/
bot.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
#!/usr/bin/env python
# encoding: utf-8
import ingrex, ingress
import random, time, sqlite3
import sys, os, re
def isDigit(s):
if (len(s) == 0):
return False
if s[0] in ('-', '+'):
return s[1:].isdigit()
return s.isdigit()
def log(msg):
'''
write log
'''
print(msg.time + u' ' + msg.text)
def unique(records):
'''
deduplication
'''
s, u = set(), list()
for item in records:
if not item[0] in s:
s.add(item[0])
u.append(item)
return u
def setConfig(db, key, value):
'''
save config to database
'''
sql = 'UPDATE config SET value = ? WHERE key = ?'
db.execute(sql, (value, key))
db.commit()
def removeCommand(db):
'''
delete all commands in database
'''
db.execute('DELETE FROM command')
db.commit()
def getCommand(db):
'''
get command from database
return a list of command
command is a list of:
[broadcast type, message]
'''
db.execute('SELECT * FROM command')
return db.fetchall()
def getConfig(db):
'''
read config from database
includ usr, pwd or cookie
and region, time range
and polling time interval
'''
db.execute('SELECT * FROM config')
conf = dict(db.fetchall())
for (k, v) in conf.items():
if isDigit(v):
conf[k] = int(v)
return conf
def getRules(db):
'''
read rules from database
return a list of rule
rule is a list of:
[regexp, faction, replacement]
'''
db.execute('SELECT * FROM rule')
rules = db.fetchall()
random.shuffle(rules)
return rules
def getIntel(db, conf):
'''
try to connect intel map with cookie
if cookie is invalid, use usr, pwd
return Ingrex.intel object
'''
cookie = conf['cookie']
field = {
'minLngE6': conf['minLngE6'],
'maxLngE6': conf['maxLngE6'],
'minLatE6': conf['minLatE6'],
'maxLatE6': conf['maxLatE6'],
}
try:
return ingrex.Intel(cookie, field)
except IndexError:
usr = conf['account']
pwd = conf['password']
cookie = ingress.login(usr, pwd)
setConfig(db, 'cookie', cookie)
return ingrex.Intel(cookie, field)
def main():
'''
main function
polling loop for intel
'''
conn = sqlite3.connect('bot.db')
cur = conn.cursor()
while True:
command = getCommand(cur)
config = getConfig(cur)
rules = getRules(cur)
removeCommand(conn)
intel = getIntel(conn, config)
result = intel.fetch_msg(config['mints'])
if result:
result = unique(result)
mints = result[0][1] + 1
setConfig(conn, 'mints', mints)
for item in result:
message = ingrex.Message(item)
log(message)
for rule in rules:
if re.search(rule[0], message.text):
msg = re.sub(rule[0], rule[2], message.text)
intel.send_msg(msg, rule[1])
break
for cmd in command:
intel.send_msg(cmd[1], cmd[0])
miniv = config['miniv']
maxiv = config['maxiv']
interval = random.randint(miniv, maxiv)
time.sleep(interval)
if __name__ == '__main__':
main()