diff options
-rwxr-xr-x | async_bot.py | 19 | ||||
-rwxr-xr-x | ingrex/intel.py | 10 |
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 | |||
6 | import json | 6 | import json |
7 | import logging | 7 | import logging |
8 | import inspect | 8 | import inspect |
9 | import urllib | ||
9 | import sys | 10 | import sys |
10 | from selenium import webdriver | 11 | from selenium import webdriver |
11 | 12 | ||
@@ -36,7 +37,9 @@ minLatE6 = 0 | |||
36 | maxLngE6 = 0 | 37 | maxLngE6 = 0 |
37 | maxLatE6 = 0 | 38 | maxLatE6 = 0 |
38 | 39 | ||
40 | |||
39 | class CookieException(Exception): | 41 | class 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 | ||
186 | def main(): | 189 | def 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 | ||
136 | class IntelError(BaseException): | 136 | class IntelException(BaseException): |
137 | """Intel Error""" | 137 | """Intel Error""" |
138 | pass | 138 | pass |
139 | 139 | ||
140 | 140 | ||
141 | class CookieError(IntelError): | 141 | class CookieException(IntelException): |
142 | """Intel Error""" | 142 | """Intel Error""" |
143 | pass | 143 | pass |
144 | 144 | ||