aboutsummaryrefslogtreecommitdiff
path: root/bot.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot.py')
-rwxr-xr-xbot.py294
1 files changed, 113 insertions, 181 deletions
diff --git a/bot.py b/bot.py
index 80ee4ed..10b1107 100755
--- a/bot.py
+++ b/bot.py
@@ -2,19 +2,17 @@
2 2
3import os 3import os
4import re 4import re
5import sys
5import time 6import time
6import json 7import json
8import ingrex
7import logging 9import logging
8import urllib 10import inspect
9from selenium import webdriver
10
11import telegram 11import telegram
12from telegram.error import NetworkError
13from pymongo import MongoClient
14 12
15import ingrex 13from selenium import webdriver
14from pymongo import MongoClient
16 15
17Debug = True
18bot = None 16bot = None
19BOT_TOKEN = '' 17BOT_TOKEN = ''
20CHANNEL_NAME = '' 18CHANNEL_NAME = ''
@@ -26,30 +24,24 @@ DBUser = ''
26DBPass = '' 24DBPass = ''
27DBHost = '' 25DBHost = ''
28BlockList = '' 26BlockList = ''
29
30LOG_FILENAME = 'voh.log' 27LOG_FILENAME = 'voh.log'
31logging.basicConfig(level=logging.DEBUG, 28TIME_ZONE='Asia/Shanghai'
32 filename=LOG_FILENAME, 29minLngE6 = 0
33 filemode='w') 30minLatE6 = 0
34console = logging.StreamHandler() 31maxLngE6 = 0
35console.setLevel(logging.INFO) 32maxLatE6 = 0
36# set a format which is simpler for console use
37formatter = logging.Formatter('%(name)-8s: %(levelname)-4s %(message)s')
38# tell the handler to use this format
39console.setFormatter(formatter)
40# add the handler to the root logger
41logging.getLogger('').addHandler(console)
42 33
43 34
44class CookieException(Exception): 35class CookieException(Exception):
45 ''' CookieError ''' 36 """Intel Error"""
37 pass
46 38
47 39
48def getTime(): 40def get_time():
49 return time.strftime('%x %X %Z') 41 return time.strftime('%x %X %Z')
50 42
51 43
52def readConfig(): 44def read_config():
53 global Email 45 global Email
54 global Passwd 46 global Passwd
55 global BOT_TOKEN 47 global BOT_TOKEN
@@ -60,6 +52,11 @@ def readConfig():
60 global DBPass 52 global DBPass
61 global DBHost 53 global DBHost
62 global BlockList 54 global BlockList
55 global LOG_FILENAME
56 global minLngE6
57 global minLatE6
58 global maxLngE6
59 global maxLatE6
63 60
64 configfile = open("./config.json") 61 configfile = open("./config.json")
65 config = json.load(configfile) 62 config = json.load(configfile)
@@ -67,259 +64,194 @@ def readConfig():
67 Passwd = config["Passwd"] 64 Passwd = config["Passwd"]
68 BOT_TOKEN = config["BOT_TOKEN"] 65 BOT_TOKEN = config["BOT_TOKEN"]
69 CHANNEL_NAME = config["CHANNEL_NAME"] 66 CHANNEL_NAME = config["CHANNEL_NAME"]
70 PhantomjsPath = config["PhantomjsPath"] 67 PhantomJSPath = config["PhantomJSPath"]
71 DBName = config["DBName"] 68 DBName = config["DBName"]
72 DBUser = config["DBUser"] 69 DBUser = config["DBUser"]
73 DBPass = config["DBPass"] 70 DBPass = config["DBPass"]
74 DBHost = config["DBHost"] 71 DBHost = config["DBHost"]
75 BlockList = config["BlockList"] 72 BlockList = config["BlockList"]
73 minLngE6 = config["minLngE6"]
74 minLatE6 = config["minLatE6"]
75 maxLngE6 = config["maxLngE6"]
76 maxLatE6 = config["maxLatE6"]
76 77
77 os.environ['TZ'] = 'Asia/Shanghai' 78 os.environ['TZ'] = TIME_ZONE
78 time.tzset() 79 time.tzset()
79 80
81 logging.basicConfig(level=logging.DEBUG,
82 filename=LOG_FILENAME,
83 filemode='w')
84 console = logging.StreamHandler()
85 console.setLevel(logging.INFO)
86 formatter = logging.Formatter('%(name)-8s: %(levelname)-4s %(message)s')
87 console.setFormatter(formatter)
88 logging.getLogger('').addHandler(console)
80 89
81def fetchCookie():
82 global Debug
83 global Email
84 global Passwd
85 global PhantomJSPath
86 90
87 logger = logging.getLogger('fetchCookie') 91def fetch_cookie():
88 logger.info(getTime() + ': Fetching Cookie...') 92 logger = logging.getLogger('fetch_cookie')
93 logger.info(get_time() + ': Fetching Cookie...')
89 94
90 driver = webdriver.PhantomJS(PhantomjsPath) 95 driver = webdriver.PhantomJS(PhantomJSPath)
91 driver.get('https://www.ingress.com/intel') 96 driver.get('https://www.ingress.com/intel')
92 97
93 # get login page 98 # get login page
94 link = driver.find_elements_by_tag_name('a')[0].get_attribute('href') 99 link = driver.find_elements_by_tag_name('a')[0].get_attribute('href')
95 driver.get(link) 100 driver.get(link)
96 if Debug: 101 driver.get_screenshot_as_file('1.png')
97 driver.get_screenshot_as_file('1.png') 102
98 # simulate manual login 103 # simulate manual login
99 driver.set_page_load_timeout(10) 104 driver.set_page_load_timeout(10)
100 driver.set_script_timeout(20) 105 driver.set_script_timeout(20)
101 driver.find_element_by_id('Email').send_keys(Email) 106 driver.find_element_by_id('Email').send_keys(Email)
102 if Debug: 107 driver.get_screenshot_as_file('2.png')
103 driver.get_screenshot_as_file('2.png')
104 driver.find_element_by_css_selector('#next').click() 108 driver.find_element_by_css_selector('#next').click()
105 time.sleep(3) 109 time.sleep(3)
106 driver.find_element_by_id('Passwd').send_keys(Passwd) 110 driver.find_element_by_id('Passwd').send_keys(Passwd)
107 if Debug: 111 driver.get_screenshot_as_file('3.png')
108 driver.get_screenshot_as_file('3.png')
109 driver.find_element_by_css_selector('#signIn').click() 112 driver.find_element_by_css_selector('#signIn').click()
110 time.sleep(3) 113 time.sleep(3)
111 if Debug: 114 driver.get_screenshot_as_file('4.png')
112 driver.get_screenshot_as_file('3.png') 115
113 # get cookies 116 # get cookies
114 temp = driver.get_cookies() 117 cookies = driver.get_cookies()
115 118
116 csrftoken = '' 119 csrftoken = ''
117 SACSID = '' 120 SACSID = ''
118 for a in temp: 121 for key in cookies:
119 if (a['name'] == 'csrftoken'): 122 if key['name'] == 'csrftoken':
120 csrftoken = a['value'] 123 csrftoken = key['value']
121 if (a['name'] == 'SACSID'): 124 if key['name'] == 'SACSID':
122 SACSID = a['value'] 125 SACSID = key['value']
123 126
124 if csrftoken == '' or SACSID == '': 127 if csrftoken == '' or SACSID == '':
125 logger.error(getTime() + ': Fetch Cookie Failed')
126 raise CookieException 128 raise CookieException
127 129
128 with open('cookie', 'w') as file: 130 with open('cookie', 'w') as file:
129 cookie = 'SACSID='+SACSID+'; csrftoken='+csrftoken+'; ingress.intelmap.shflt=viz; ingress.intelmap.lat=29.098418372855484; ingress.intelmap.lng=119.81689453125; ingress.intelmap.zoom=17' 131 cookie = 'SACSID='+SACSID+'; csrftoken='+csrftoken+'; ingress.intelmap.shflt=viz; ingress.intelmap.lat=29.098418372855484; ingress.intelmap.lng=119.81689453125; ingress.intelmap.zoom=17'
130 file.write(cookie) 132 file.write(cookie)
131 133
134 logger.info(get_time() + ': Fetching Cookie Succeed')
132 driver.quit() 135 driver.quit()
133 logger.info(getTime() + ': Fetching Cookie Succeed')
134 return True 136 return True
135 137
136 138
137def sendMessge(bot, msg): 139def send_message(bot, message, monitor=False):
138 "sendMsg" 140 logger = logging.getLogger('send_message')
139
140 logger = logging.getLogger('sendMessage')
141 while True: 141 while True:
142 try: 142 try:
143 url = 'https://api.telegram.org/bot' 143 if monitor is True:
144 url += BOT_TOKEN 144 bot.sendMessage(chat_id="@voamonitor", text=message)
145 url += '/sendMessage?chat_id=' 145 else:
146 url += CHANNEL_NAME 146 print(type(message))
147 url += '&text=' 147 bot.sendMessage(chat_id=CHANNEL_NAME, text=message)
148 url += urllib.parse.quote(msg) 148 logger.info(get_time() + ": sendMsg " + message)
149
150 req = urllib.request.Request(url, headers={'Content-Type': 'application/x-www-form-urlencoded'})
151 resp = urllib.request.urlopen(req)
152 data = resp.read()
153 logger.info(data)
154 logger.info(getTime() + ": sendMsg " + msg)
155 break 149 break
156 except NetworkError: 150 except telegram.TelegramError:
151 logger.error(get_time() + ": Send Message to Channel Failed")
157 time.sleep(1) 152 time.sleep(1)
158 153 except Exception:
159 154 logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno))
160def sendMonitor(bot, msg):
161 logger = logging.getLogger('sendMonitor')
162 while True:
163 try:
164 bot.sendMessage(chat_id="@voamonitor", text=msg)
165 logger.info(getTime() + ": sendMsg " + msg)
166 break
167 except NetworkError:
168 time.sleep(1) 155 time.sleep(1)
169 156
170 157
171def formatMessage(raw): 158def find_message_record(id):
172 pattern = re.compile(BlockList)
173 match = pattern.search(str(raw))
174 if match:
175 return "Blocked"
176
177 msg = ''
178 plext = raw[2]['plext']
179 markup = plext['markup']
180
181 for mark in markup:
182 if mark[0] == 'SECURE':
183 msg += mark[1]['plain']
184 elif mark[0] == 'SENDER':
185 player = mark[1]['plain']
186 team = mark[1]['team']
187
188 pattern = re.compile(':')
189 match = pattern.search(player)
190 if match:
191 if team == 'RESISTANCE':
192 player = player[:match.span()[0]] + ' 🐳' + player[match.span()[0]:]
193 elif team == 'ENLIGHTENED':
194 player = player[:match.span()[0]] + ' 🐸' + player[match.span()[0]:]
195 msg += player
196
197 elif mark[0] == 'PLAYER' or mark[0] == 'AT_PLAYER':
198 player = mark[1]['plain']
199 team = mark[1]['team']
200
201 msg += player
202 if team == 'RESISTANCE':
203 msg += ' 🐳'
204 elif team == 'ENLIGHTENED':
205 msg += ' 🐸'
206
207 elif mark[0] == 'TEXT':
208 msg += mark[1]['plain']
209
210 pattern = re.compile('\[secure\]')
211 match = pattern.search(msg)
212 if match:
213 if msg.find(':') != -1:
214 msg = msg[:9] + '@' + msg[9:]
215 else:
216 msg = msg[:10] + '@' + msg[10:]
217 else:
218 msg = '@' + msg
219
220 return msg
221
222
223def FindRecord(id):
224 uri = 'mongodb://' + DBHost 159 uri = 'mongodb://' + DBHost
225 Conn = MongoClient(uri) 160 conn = MongoClient(uri)
226 Conn.api.authenticate(DBUser, DBPass, DBName) 161 conn.api.authenticate(DBUser, DBPass, DBName)
227 database = Conn[DBName] 162 database = conn[DBName]
228 mycollection = database.entries 163 collection = database.entries
229 res = mycollection.find({"id": id}) 164 count = collection.find({"id": id}).count()
230 if res.count() == 0: 165 conn.close()
166 if count == 0:
231 return False 167 return False
232 else: 168 else:
233 return True 169 return True
234 170
235 171
236def insertDB(time, id, msg): 172def insert_message_to_database(time, id, msg):
237 uri = 'mongodb://' + DBHost 173 uri = 'mongodb://' + DBHost
238 Conn = MongoClient(uri) 174 conn = MongoClient(uri)
239 Conn.api.authenticate(DBUser, DBPass, DBName) 175 conn.api.authenticate(DBUser, DBPass, DBName)
240 database = Conn[DBName] 176 database = conn[DBName]
241 mycollection = database.entries 177 collection = database.entries
242 post = {"id": id, "time": time, "msg": msg} 178 post = {"id": id, "time": time, "msg": msg}
243 mycollection.insert(post) 179 collection.insert(post)
244 Conn.close() 180 conn.close()
245 181
246 182
247def main(): 183def main():
248 logger = logging.getLogger('main') 184 logger = logging.getLogger(__name__)
249 185
186 # Lat & Lng of fetch region
250 field = { 187 field = {
251 'minLngE6': 119618783, 188 'minLngE6': minLngE6,
252 'minLatE6': 29912919, 189 'minLatE6': minLatE6,
253 'maxLngE6': 121018722, 190 'maxLngE6': maxLngE6,
254 'maxLatE6': 30573739, 191 'maxLatE6': maxLatE6,
255 } 192 }
256 193
257 mints = -1 194 mints = -1
258 maxts=-1 195 maxts = -1
259 reverse=False 196 reverse = False
260 tab='all' 197 tab = 'all'
261 198
199 # fetch cookie
262 while True: 200 while True:
263 try: 201 try:
264 if fetchCookie(): 202 if fetch_cookie():
265 break 203 break
266 except CookieException: 204 except CookieException:
205 logger.error(get_time() + ': Fetch Cookie Failed')
206 time.sleep(3)
207 except:
208 logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno))
267 time.sleep(3) 209 time.sleep(3)
268 210
211
212 # fetch message
269 count = 0 213 count = 0
270 while True: 214 while True:
271 count += 1 215 count += 1
216 logger.info(get_time() + ": {} Fetching from Intel...".format(str(count)))
272 217
273 with open('cookie') as cookies: 218 with open('cookie') as cookies:
274 cookies = cookies.read().strip() 219 cookies = cookies.read().strip()
275 220
276 logger.info(getTime() + ": {} Fetching from Intel...".format(str(count))) 221 # fetch message per time
277
278 while True: 222 while True:
279 try: 223 try:
280
281 intel = ingrex.Intel(cookies, field) 224 intel = ingrex.Intel(cookies, field)
282 result = intel.fetch_msg(mints, maxts, reverse, tab) 225 result = intel.fetch_msg(mints, maxts, reverse, tab)
283
284 if result: 226 if result:
285 mints = result[0][1] + 1 227 mints = result[0][1] + 1
286 break 228 break
287 229 except:
288 except CookieException: 230 logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno))
289 while True: 231 time.sleep(3)
290 try:
291 if fetchCookie():
292 break
293 except CookieException:
294 time.sleep(3)
295 except Exception:
296 pass
297 232
298 for item in result[::-1]: 233 for item in result[::-1]:
299 message = ingrex.Message(item) 234 # Check spam message
235 pattern = re.compile(BlockList)
236 match = pattern.search(str(item))
237 if match:
238 continue
300 239
240 message = ingrex.Message(item)
301 if message.ptype == 'PLAYER_GENERATED': 241 if message.ptype == 'PLAYER_GENERATED':
302 # logger.info(getTime() + str(item)) 242 if find_message_record(message.guid) is False:
303 243 insert_message_to_database(message.time, message.guid, message.msg)
304 msg = formatMessage(item) 244 send_message(bot, message.msg, False)
305 if msg == 'Blocked':
306 logger.info(getTime() + " " + message.text)
307 else:
308 msg = message.time + " " + msg
309 # logger.info(getTime() + " " + msg)
310 if FindRecord(message.guid) is False:
311 insertDB(message.time, message.guid, msg)
312 # sendMonitor(bot, msg)
313 sendMessge(bot, msg)
314 245
315 time.sleep(10) 246 time.sleep(10)
316 247
317if __name__ == '__main__': 248if __name__ == '__main__':
318 readConfig() 249 read_config()
319 bot = telegram.Bot(BOT_TOKEN) 250 bot = telegram.Bot(BOT_TOKEN)
320 251
321 while True: 252 while True:
322 try: 253 try:
323 main() 254 main()
324 except Exception: 255 except Exception as e:
325 sendMonitor(bot, 'Main Error') 256 send_message(bot, 'Main Unexpected error' + str(sys.exc_info()[0]) + " Line: " + str(inspect.currentframe().f_lineno), True)
257 time.sleep(3) \ No newline at end of file
Powered by cgit v1.2.3 (git 2.41.0)