aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJinwei Zhao <[email protected]>2017-01-31 19:39:47 +0800
committerJinwei Zhao <[email protected]>2017-02-01 21:41:54 +0800
commit94b20af64c9e0826b9cab3433873d689e6e3d4f7 (patch)
treef68a52c6eddbb9af1208fe0bbfaa678fb13c91a1
parent88750f859acf7e91d1f6403f89baa54189f51cbd (diff)
downloadCOMM2TG-94b20af64c9e0826b9cab3433873d689e6e3d4f7.tar.gz
parse message
-rwxr-xr-xasync_bot.py19
-rwxr-xr-xingrex/intel.py10
2 files changed, 13 insertions, 16 deletions
diff --git a/async_bot.py b/async_bot.py
index 668e0fe..51a347c 100755
--- a/async_bot.py
+++ b/async_bot.py
@@ -6,6 +6,7 @@ import time
6import json 6import json
7import logging 7import logging
8import inspect 8import inspect
9import urllib
9import sys 10import sys
10from selenium import webdriver 11from selenium import webdriver
11 12
@@ -36,7 +37,9 @@ minLatE6 = 0
36maxLngE6 = 0 37maxLngE6 = 0
37maxLatE6 = 0 38maxLatE6 = 0
38 39
40
39class CookieException(Exception): 41class CookieException(Exception):
42 """Intel Error"""
40 pass 43 pass
41 44
42 45
@@ -83,7 +86,6 @@ def read_config():
83 86
84 logging.basicConfig(level=logging.DEBUG, 87 logging.basicConfig(level=logging.DEBUG,
85 filename=LOG_FILENAME, 88 filename=LOG_FILENAME,
86 encoding='utf8',
87 filemode='w') 89 filemode='w')
88 console = logging.StreamHandler() 90 console = logging.StreamHandler()
89 console.setLevel(logging.INFO) 91 console.setLevel(logging.INFO)
@@ -147,6 +149,7 @@ def send_message(bot, message, monitor=False):
147 if monitor is True: 149 if monitor is True:
148 bot.sendMessage(chat_id="@voamonitor", text=message) 150 bot.sendMessage(chat_id="@voamonitor", text=message)
149 else: 151 else:
152 print(type(message))
150 bot.sendMessage(chat_id=CHANNEL_NAME, text=message) 153 bot.sendMessage(chat_id=CHANNEL_NAME, text=message)
151 logger.info(get_time() + ": sendMsg " + message) 154 logger.info(get_time() + ": sendMsg " + message)
152 break 155 break
@@ -184,6 +187,7 @@ def insert_message_to_database(time, id, msg):
184 187
185 188
186def main(): 189def main():
190 # global intel
187 logger = logging.getLogger('main') 191 logger = logging.getLogger('main')
188 192
189 # Lat & Lng of fetch region 193 # Lat & Lng of fetch region
@@ -211,15 +215,16 @@ def main():
211 logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " " + str(inspect.currentframe().f_lineno)) 215 logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " " + str(inspect.currentframe().f_lineno))
212 time.sleep(3) 216 time.sleep(3)
213 217
218
214 # fetch message 219 # fetch message
215 count = 0 220 count = 0
216 while True: 221 while True:
217 count += 1 222 count += 1
223 logger.info(get_time() + ": {} Fetching from Intel...".format(str(count)))
218 224
219 with open('cookie') as cookies: 225 with open('cookie') as cookies:
220 cookies = cookies.read().strip() 226 cookies = cookies.read().strip()
221 logger.info(get_time() + ": {} Fetching from Intel...".format(str(count))) 227
222
223 # fetch message per time 228 # fetch message per time
224 while True: 229 while True:
225 try: 230 try:
@@ -228,13 +233,6 @@ def main():
228 if result: 233 if result:
229 mints = result[0][1] + 1 234 mints = result[0][1] + 1
230 break 235 break
231 except CookieException:
232 while True:
233 try:
234 if fetch_cookie():
235 break
236 except CookieException:
237 time.sleep(3)
238 except Exception: 236 except Exception:
239 logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " " + str(inspect.currentframe().f_lineno)) 237 logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " " + str(inspect.currentframe().f_lineno))
240 time.sleep(3) 238 time.sleep(3)
@@ -248,7 +246,6 @@ def main():
248 246
249 message = ingrex.Message(item) 247 message = ingrex.Message(item)
250 if message.ptype == 'PLAYER_GENERATED': 248 if message.ptype == 'PLAYER_GENERATED':
251 logger.info(get_time() + " " + message.msg)
252 if find_message_record(message.guid) is False: 249 if find_message_record(message.guid) is False:
253 insert_message_to_database(message.time, message.guid, message.msg) 250 insert_message_to_database(message.time, message.guid, message.msg)
254 send_message(bot, message.msg, False) 251 send_message(bot, message.msg, False)
diff --git a/ingrex/intel.py b/ingrex/intel.py
index 506c6bc..2cc4e7f 100755
--- a/ingrex/intel.py
+++ b/ingrex/intel.py
@@ -43,14 +43,14 @@ class Intel(object):
43 count = 0 43 count = 0
44 while count < 3: 44 while count < 3:
45 try: 45 try:
46 request = self.session.post(url, data=json.dumps(payload), headers=self.headers, timeout=600) 46 request = self.session.post(url, data=json.dumps(payload), headers=self.headers, timeout=(10, 60))
47 return request.json()['result'] 47 return request.json()['result']
48 except requests.ConnectionError: 48 except requests.ConnectionError:
49 raise IntelError 49 raise IntelException
50 except Exception: 50 except Exception:
51 count += 1 51 count += 1
52 continue 52 continue
53 raise CookieError 53 raise CookieException
54 54
55 def fetch_msg(self, mints=-1, maxts=-1, reverse=False, tab='all'): 55 def fetch_msg(self, mints=-1, maxts=-1, reverse=False, tab='all'):
56 "fetch message from Ingress COMM, tab can be 'all', 'faction', 'alerts'" 56 "fetch message from Ingress COMM, tab can be 'all', 'faction', 'alerts'"
@@ -133,12 +133,12 @@ class Intel(object):
133 return self.fetch(url, payload) 133 return self.fetch(url, payload)
134 134
135 135
136class IntelError(BaseException): 136class IntelException(BaseException):
137 """Intel Error""" 137 """Intel Error"""
138 pass 138 pass
139 139
140 140
141class CookieError(IntelError): 141class CookieException(IntelException):
142 """Intel Error""" 142 """Intel Error"""
143 pass 143 pass
144 144
Powered by cgit v1.2.3 (git 2.41.0)