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