diff options
Diffstat (limited to 'bot.py')
-rwxr-xr-x | bot.py | 80 |
1 files changed, 56 insertions, 24 deletions
@@ -5,12 +5,11 @@ import re | |||
5 | import time | 5 | import time |
6 | import json | 6 | import json |
7 | import logging | 7 | import logging |
8 | import platform | ||
9 | import urllib | 8 | import urllib |
10 | from selenium import webdriver | 9 | from selenium import webdriver |
11 | 10 | ||
12 | import telegram | 11 | import telegram |
13 | from telegram.error import NetworkError, Unauthorized | 12 | from telegram.error import NetworkError |
14 | from pymongo import MongoClient | 13 | from pymongo import MongoClient |
15 | 14 | ||
16 | import ingrex | 15 | import ingrex |
@@ -22,6 +21,11 @@ CHANNEL_NAME = '' | |||
22 | Email = '' | 21 | Email = '' |
23 | Passwd = '' | 22 | Passwd = '' |
24 | PhantomjsPath = '' | 23 | PhantomjsPath = '' |
24 | DBName = '' | ||
25 | DBUser = '' | ||
26 | DBPass = '' | ||
27 | DBHost = '' | ||
28 | BlockList = '' | ||
25 | 29 | ||
26 | LOG_FILENAME = 'voh.log' | 30 | LOG_FILENAME = 'voh.log' |
27 | logging.basicConfig(level = logging.DEBUG, | 31 | logging.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 |
37 | logging.getLogger('').addHandler(console) | 41 | logging.getLogger('').addHandler(console) |
38 | 42 | ||
43 | |||
39 | class CookieException(Exception): | 44 | class CookieException(Exception): |
40 | ''' CookieError ''' | 45 | ''' CookieError ''' |
41 | 46 | ||
47 | |||
42 | def getTime(): | 48 | def getTime(): |
43 | return time.strftime('%x %X %Z') | 49 | return time.strftime('%x %X %Z') |
44 | 50 | ||
51 | |||
45 | def readConfig(): | 52 | def 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 | ||
67 | def fetchCookie(): | 80 | def 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 | |||
122 | def sendMessge(bot, msg): | 136 | def 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 | |||
145 | def sendMonitor(bot, msg): | 159 | def 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 | |||
155 | def formatMessage(raw): | 170 | def 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 | ||
207 | def insertDB(time, msg): | 222 | def 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 | ||
235 | def 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 | ||
216 | def main(): | 245 | def 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 | ||
281 | if __name__ == '__main__': | 313 | if __name__ == '__main__': |
282 | readConfig() | 314 | readConfig() |