aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-x.gitignore1
-rwxr-xr-xbot.py80
-rwxr-xr-xconfig.json.example8
3 files changed, 64 insertions, 25 deletions
diff --git a/.gitignore b/.gitignore
index 5b0c117..0144014 100755
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ __pycache__
4*.log 4*.log
5cookie 5cookie
6config.json 6config.json
7.idea/
diff --git a/bot.py b/bot.py
index fd3c81a..a521764 100755
--- a/bot.py
+++ b/bot.py
@@ -5,12 +5,11 @@ import re
5import time 5import time
6import json 6import json
7import logging 7import logging
8import platform
9import urllib 8import urllib
10from selenium import webdriver 9from selenium import webdriver
11 10
12import telegram 11import telegram
13from telegram.error import NetworkError, Unauthorized 12from telegram.error import NetworkError
14from pymongo import MongoClient 13from pymongo import MongoClient
15 14
16import ingrex 15import ingrex
@@ -22,6 +21,11 @@ CHANNEL_NAME = ''
22Email = '' 21Email = ''
23Passwd = '' 22Passwd = ''
24PhantomjsPath = '' 23PhantomjsPath = ''
24DBName = ''
25DBUser = ''
26DBPass = ''
27DBHost = ''
28BlockList = ''
25 29
26LOG_FILENAME = 'voh.log' 30LOG_FILENAME = 'voh.log'
27logging.basicConfig(level = logging.DEBUG, 31logging.basicConfig(level = logging.DEBUG,
@@ -36,18 +40,26 @@ console.setFormatter(formatter)
36# add the handler to the root logger 40# add the handler to the root logger
37logging.getLogger('').addHandler(console) 41logging.getLogger('').addHandler(console)
38 42
43
39class CookieException(Exception): 44class CookieException(Exception):
40 ''' CookieError ''' 45 ''' CookieError '''
41 46
47
42def getTime(): 48def getTime():
43 return time.strftime('%x %X %Z') 49 return time.strftime('%x %X %Z')
44 50
51
45def readConfig(): 52def readConfig():
46 global Email 53 global Email
47 global Passwd 54 global Passwd
48 global BOT_TOKEN 55 global BOT_TOKEN
49 global CHANNEL_NAME 56 global CHANNEL_NAME
50 global PhantomjsPath 57 global PhantomjsPath
58 global DBName
59 global DBUser
60 global DBPass
61 global DBHost
62 global BlockList
51 63
52 configfile = open("./config.json") 64 configfile = open("./config.json")
53 config = json.load(configfile) 65 config = json.load(configfile)
@@ -55,14 +67,15 @@ def readConfig():
55 Passwd = config["Passwd"] 67 Passwd = config["Passwd"]
56 BOT_TOKEN = config["BOT_TOKEN"] 68 BOT_TOKEN = config["BOT_TOKEN"]
57 CHANNEL_NAME = config["CHANNEL_NAME"] 69 CHANNEL_NAME = config["CHANNEL_NAME"]
70 PhantomjsPath = config["PhantomjsPath"]
71 DBName = config["DBName"]
72 DBUser = config["DBUser"]
73 DBPass = config["DBPass"]
74 DBHost = config["DBHost"]
75 BlockList = config["BlockList"]
58 76
59 osname = platform.system() 77 os.environ['TZ'] = 'Asia/Shanghai'
60 if osname == "Windows": 78 time.tzset()
61 PhantomjsPath = 'C:\\Users\\LuciaHydrion\\AppData\\Roaming\\npm\\phantomjs.cmd'
62 elif osname == "Linux":
63 PhantomjsPath = '/home/clarkzjw/code/phantomjs-2.1.1-linux-i686/bin/phantomjs'
64 os.environ['TZ'] = 'Asia/Shanghai'
65 time.tzset()
66 79
67def fetchCookie(): 80def fetchCookie():
68 global Debug 81 global Debug
@@ -119,6 +132,7 @@ def fetchCookie():
119 logger.info(getTime() + ': Fetching Cookie Succeed') 132 logger.info(getTime() + ': Fetching Cookie Succeed')
120 return True 133 return True
121 134
135
122def sendMessge(bot, msg): 136def sendMessge(bot, msg):
123 "sendMsg" 137 "sendMsg"
124 138
@@ -136,12 +150,12 @@ def sendMessge(bot, msg):
136 resp = urllib.request.urlopen(req) 150 resp = urllib.request.urlopen(req)
137 data = resp.read() 151 data = resp.read()
138 152
139 #bot.sendMessage(chat_id=CHANNEL_NAME, text=msg)
140 logger.info(getTime() + ": sendMsg " + msg) 153 logger.info(getTime() + ": sendMsg " + msg)
141 break 154 break
142 except NetworkError: 155 except NetworkError:
143 time.sleep(1) 156 time.sleep(1)
144 157
158
145def sendMonitor(bot, msg): 159def sendMonitor(bot, msg):
146 logger = logging.getLogger('sendMonitor') 160 logger = logging.getLogger('sendMonitor')
147 while True: 161 while True:
@@ -152,8 +166,9 @@ def sendMonitor(bot, msg):
152 except NetworkError: 166 except NetworkError:
153 time.sleep(1) 167 time.sleep(1)
154 168
169
155def formatMessage(raw): 170def formatMessage(raw):
156 pattern = re.compile('xmps.biz|enl.sh|ingressfarm.com|Polygon') 171 pattern = re.compile(BlockList)
157 match = pattern.search(str(raw)) 172 match = pattern.search(str(raw))
158 if match: 173 if match:
159 return "Blocked" 174 return "Blocked"
@@ -161,7 +176,7 @@ def formatMessage(raw):
161 msg = '' 176 msg = ''
162 plext = raw[2]['plext'] 177 plext = raw[2]['plext']
163 markup = plext['markup'] 178 markup = plext['markup']
164 plaintext = plext['text'] 179 #plaintext = plext['text']
165 180
166 for mark in markup: 181 for mark in markup:
167 if mark[0] == 'SECURE': 182 if mark[0] == 'SECURE':
@@ -204,14 +219,28 @@ def formatMessage(raw):
204 219
205 return msg 220 return msg
206 221
207def insertDB(time, msg): 222def FindRecord(id):
208 logger = logging.getLogger('insertDB') 223 uri = 'mongodb://' + DBHost
209 Conn = MongoClient() 224 Conn = MongoClient(uri)
210 database = Conn['COMM_Hangzhou'] 225 Conn.api.authenticate(DBUser, DBPass, DBName)
226 database = Conn[DBName]
211 mycollection = database.entries 227 mycollection = database.entries
212 post = {"time": time, "msg": msg} 228 res = mycollection.find({"id": id})
213 mycollection.insert(post) 229 if res.count() == 0:
230 return False
231 else:
232 return True
233
214 234
235def insertDB(time, id, msg):
236 uri = 'mongodb://' + DBHost
237 Conn = MongoClient(uri)
238 Conn.api.authenticate(DBUser, DBPass, DBName)
239 database = Conn[DBName]
240 mycollection = database.entries
241 post = {"id": id, "time": time, "msg": msg}
242 mycollection.insert(post)
243 Conn.close()
215 244
216def main(): 245def main():
217 logger = logging.getLogger('main') 246 logger = logging.getLogger('main')
@@ -230,7 +259,7 @@ def main():
230 259
231 while True: 260 while True:
232 try: 261 try:
233 if fetchCookie(): 262 if fetchCookie():
234 break 263 break
235 except CookieException: 264 except CookieException:
236 time.sleep(3) 265 time.sleep(3)
@@ -253,7 +282,8 @@ def main():
253 if result: 282 if result:
254 mints = result[0][1] + 1 283 mints = result[0][1] + 1
255 break 284 break
256 except CookieError: 285
286 except CookieException:
257 while True: 287 while True:
258 try: 288 try:
259 if fetchCookie(): 289 if fetchCookie():
@@ -263,6 +293,7 @@ def main():
263 293
264 for item in result[::-1]: 294 for item in result[::-1]:
265 message = ingrex.Message(item) 295 message = ingrex.Message(item)
296
266 if message.ptype == 'PLAYER_GENERATED': 297 if message.ptype == 'PLAYER_GENERATED':
267 logger.info(getTime() + str(item)) 298 logger.info(getTime() + str(item))
268 299
@@ -272,11 +303,12 @@ def main():
272 else: 303 else:
273 msg = message.time + " " + msg 304 msg = message.time + " " + msg
274 logger.info(getTime() + " " + msg) 305 logger.info(getTime() + " " + msg)
275 insertDB(message.time, msg) 306 if FindRecord(message.guid) == False:
276 sendMonitor(bot, msg) 307 insertDB(message.time, message.guid, msg)
277 #sendMessge(bot, msg) 308 sendMonitor(bot, msg)
309 #sendMessge(bot, msg)
278 310
279 time.sleep(10) 311 time.sleep(5)
280 312
281if __name__ == '__main__': 313if __name__ == '__main__':
282 readConfig() 314 readConfig()
diff --git a/config.json.example b/config.json.example
index c048ee9..8d9b824 100755
--- a/config.json.example
+++ b/config.json.example
@@ -2,5 +2,11 @@
2 "Email": "Your Ingress Account Email", 2 "Email": "Your Ingress Account Email",
3 "Passwd": "Your Ingress Account Password", 3 "Passwd": "Your Ingress Account Password",
4 "BOT_TOKEN": "Your Telegram Bot Token", 4 "BOT_TOKEN": "Your Telegram Bot Token",
5 "CHANNEL_NAME": "Your Channel ID" 5 "CHANNEL_NAME": "Your Channel ID",
6 "PhantomjsPath": "/usr/local/phantomjs/bin/phantomjs",
7 "DBName": "",
8 "DBUser": "",
9 "DBPass": "",
10 "DBHost": "ip:port",
11 "BlockList": ""
6} 12}
Powered by cgit v1.2.3 (git 2.41.0)