From 94b20af64c9e0826b9cab3433873d689e6e3d4f7 Mon Sep 17 00:00:00 2001 From: Jinwei Zhao Date: Tue, 31 Jan 2017 19:39:47 +0800 Subject: parse message --- async_bot.py | 19 ++++++++----------- 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 import json import logging import inspect +import urllib import sys from selenium import webdriver @@ -36,7 +37,9 @@ minLatE6 = 0 maxLngE6 = 0 maxLatE6 = 0 + class CookieException(Exception): + """Intel Error""" pass @@ -83,7 +86,6 @@ def read_config(): logging.basicConfig(level=logging.DEBUG, filename=LOG_FILENAME, - encoding='utf8', filemode='w') console = logging.StreamHandler() console.setLevel(logging.INFO) @@ -147,6 +149,7 @@ def send_message(bot, message, monitor=False): if monitor is True: bot.sendMessage(chat_id="@voamonitor", text=message) else: + print(type(message)) bot.sendMessage(chat_id=CHANNEL_NAME, text=message) logger.info(get_time() + ": sendMsg " + message) break @@ -184,6 +187,7 @@ def insert_message_to_database(time, id, msg): def main(): + # global intel logger = logging.getLogger('main') # Lat & Lng of fetch region @@ -211,15 +215,16 @@ def main(): logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " " + str(inspect.currentframe().f_lineno)) time.sleep(3) + # fetch message count = 0 while True: count += 1 + logger.info(get_time() + ": {} Fetching from Intel...".format(str(count))) with open('cookie') as cookies: cookies = cookies.read().strip() - logger.info(get_time() + ": {} Fetching from Intel...".format(str(count))) - + # fetch message per time while True: try: @@ -228,13 +233,6 @@ def main(): if result: mints = result[0][1] + 1 break - except CookieException: - while True: - try: - if fetch_cookie(): - break - except CookieException: - time.sleep(3) except Exception: logger.error(get_time() + ": Unexpected error: " + str(sys.exc_info()[0]) + " " + str(inspect.currentframe().f_lineno)) time.sleep(3) @@ -248,7 +246,6 @@ def main(): message = ingrex.Message(item) if message.ptype == 'PLAYER_GENERATED': - logger.info(get_time() + " " + message.msg) if find_message_record(message.guid) is False: insert_message_to_database(message.time, message.guid, message.msg) 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): count = 0 while count < 3: try: - request = self.session.post(url, data=json.dumps(payload), headers=self.headers, timeout=600) + request = self.session.post(url, data=json.dumps(payload), headers=self.headers, timeout=(10, 60)) return request.json()['result'] except requests.ConnectionError: - raise IntelError + raise IntelException except Exception: count += 1 continue - raise CookieError + raise CookieException def fetch_msg(self, mints=-1, maxts=-1, reverse=False, tab='all'): "fetch message from Ingress COMM, tab can be 'all', 'faction', 'alerts'" @@ -133,12 +133,12 @@ class Intel(object): return self.fetch(url, payload) -class IntelError(BaseException): +class IntelException(BaseException): """Intel Error""" pass -class CookieError(IntelError): +class CookieException(IntelException): """Intel Error""" pass -- cgit v1.2.3