From 66d411c97ba82bfe3b072b57d2b7f8a6554723d6 Mon Sep 17 00:00:00 2001 From: Jinwei Zhao Date: Tue, 31 Jan 2017 08:35:05 +0800 Subject: format --- bot.py | 34 +++++++++++++++++++--------------- config.json.example | 10 +++++----- ingrex/intel.py | 6 ++++-- ingrex/praser.py | 1 + ingrex/utils.py | 19 +++++++++++++------ searchbot.py | 11 ++++++++++- 6 files changed, 52 insertions(+), 29 deletions(-) diff --git a/bot.py b/bot.py index a521764..dc50264 100755 --- a/bot.py +++ b/bot.py @@ -28,8 +28,8 @@ DBHost = '' BlockList = '' LOG_FILENAME = 'voh.log' -logging.basicConfig(level = logging.DEBUG, - filename = LOG_FILENAME, +logging.basicConfig(level=logging.DEBUG, + filename=LOG_FILENAME, filemode='w') console = logging.StreamHandler() console.setLevel(logging.INFO) @@ -77,6 +77,7 @@ def readConfig(): os.environ['TZ'] = 'Asia/Shanghai' time.tzset() + def fetchCookie(): global Debug global Email @@ -149,7 +150,7 @@ def sendMessge(bot, msg): req = urllib.request.Request(url, headers={'Content-Type': 'application/x-www-form-urlencoded'}) resp = urllib.request.urlopen(req) data = resp.read() - + logger.info(data) logger.info(getTime() + ": sendMsg " + msg) break except NetworkError: @@ -176,7 +177,6 @@ def formatMessage(raw): msg = '' plext = raw[2]['plext'] markup = plext['markup'] - #plaintext = plext['text'] for mark in markup: if mark[0] == 'SECURE': @@ -219,6 +219,7 @@ def formatMessage(raw): return msg + def FindRecord(id): uri = 'mongodb://' + DBHost Conn = MongoClient(uri) @@ -242,14 +243,15 @@ def insertDB(time, id, msg): mycollection.insert(post) Conn.close() + def main(): logger = logging.getLogger('main') field = { - 'minLngE6':119618783, - 'minLatE6':29912919, - 'maxLngE6':121018722, - 'maxLatE6':30573739, + 'minLngE6': 119618783, + 'minLatE6': 29912919, + 'maxLngE6': 121018722, + 'maxLatE6': 30573739, } mints = -1 @@ -259,7 +261,7 @@ def main(): while True: try: - if fetchCookie(): + if fetchCookie(): break except CookieException: time.sleep(3) @@ -290,25 +292,27 @@ def main(): break except CookieException: time.sleep(3) + except Exception: + pass for item in result[::-1]: message = ingrex.Message(item) if message.ptype == 'PLAYER_GENERATED': - logger.info(getTime() + str(item)) + # logger.info(getTime() + str(item)) msg = formatMessage(item) if msg == 'Blocked': logger.info(getTime() + " " + message.text) else: msg = message.time + " " + msg - logger.info(getTime() + " " + msg) - if FindRecord(message.guid) == False: + # logger.info(getTime() + " " + msg) + if FindRecord(message.guid) is False: insertDB(message.time, message.guid, msg) - sendMonitor(bot, msg) - #sendMessge(bot, msg) + # sendMonitor(bot, msg) + sendMessge(bot, msg) - time.sleep(5) + time.sleep(10) if __name__ == '__main__': readConfig() diff --git a/config.json.example b/config.json.example index 8d9b824..7e61d75 100755 --- a/config.json.example +++ b/config.json.example @@ -1,9 +1,9 @@ { - "Email": "Your Ingress Account Email", - "Passwd": "Your Ingress Account Password", - "BOT_TOKEN": "Your Telegram Bot Token", - "CHANNEL_NAME": "Your Channel ID", - "PhantomjsPath": "/usr/local/phantomjs/bin/phantomjs", + "Email": "Your Ingress Account Email", + "Passwd": "Your Ingress Account Password", + "BOT_TOKEN": "Your Telegram Bot Token", + "CHANNEL_NAME": "Your Channel ID", + "PhantomjsPath": "/usr/local/phantomjs/bin/phantomjs", "DBName": "", "DBUser": "", "DBPass": "", diff --git a/ingrex/intel.py b/ingrex/intel.py index f13b2a2..506c6bc 100755 --- a/ingrex/intel.py +++ b/ingrex/intel.py @@ -2,7 +2,7 @@ import requests import re import json -import os + class Intel(object): "main class with all Intel functions" @@ -43,7 +43,7 @@ class Intel(object): count = 0 while count < 3: try: - request = self.session.post(url, data=json.dumps(payload), headers=self.headers) + request = self.session.post(url, data=json.dumps(payload), headers=self.headers, timeout=600) return request.json()['result'] except requests.ConnectionError: raise IntelError @@ -132,10 +132,12 @@ class Intel(object): } return self.fetch(url, payload) + class IntelError(BaseException): """Intel Error""" pass + class CookieError(IntelError): """Intel Error""" pass diff --git a/ingrex/praser.py b/ingrex/praser.py index 732ab1b..a05ec1e 100755 --- a/ingrex/praser.py +++ b/ingrex/praser.py @@ -9,6 +9,7 @@ if osname == "Linux": os.environ['TZ'] = 'Asia/Shanghai' time.tzset() + class Message(object): "Message object" def __init__(self, raw_msg): diff --git a/ingrex/utils.py b/ingrex/utils.py index 8090f15..17d69e6 100755 --- a/ingrex/utils.py +++ b/ingrex/utils.py @@ -1,14 +1,16 @@ "Map Utils" from math import pi, sin, cos, tan, asin, radians, sqrt, log + def calc_tile(lng, lat, zoomlevel): - tilecounts = [1,1,1,40,40,80,80,320,1E3,2E3,2E3,4E3,8E3,16E3,16E3,32E3] + tilecounts = [1, 1, 1, 40, 40, 80, 80, 320, 1E3, 2E3, 2E3, 4E3, 8E3, 16E3, 16E3, 32E3] rlat = radians(lat) tilecount = tilecounts[zoomlevel] xtile = int((lng + 180.0) / 360.0 * tilecount) ytile = int((1.0 - log(tan(rlat) + (1 / cos(rlat))) / pi) / 2.0 * tilecount) return xtile, ytile + def calc_dist(lat1, lng1, lat2, lng2): lat1, lng1, lat2, lng2 = map(radians, [lat1, lng1, lat2, lng2]) dlat = lat1 - lat2 @@ -18,12 +20,13 @@ def calc_dist(lat1, lng1, lat2, lng2): m = 6367.0 * c * 1000 return m + def point_in_poly(x, y, poly): n = len(poly) inside = False - p1x,p1y = poly[0] + p1x, p1y = poly[0] for i in range(n+1): - p2x,p2y = poly[i % n] + p2x, p2y = poly[i % n] if y > min(p1y, p2y): if y <= max(p1y, p2y): if x <= max(p1x, p2x): @@ -31,9 +34,10 @@ def point_in_poly(x, y, poly): xints = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x if p1x == p2x or x <= xints: inside = not inside - p1x,p1y = p2x,p2y + p1x, p1y = p2x, p2y return inside + def transform(wgLat, wgLon): """ transform(latitude,longitude) , WGS84 @@ -44,7 +48,7 @@ def transform(wgLat, wgLon): if (outOfChina(wgLat, wgLon)): mgLat = wgLat mgLon = wgLon - return mgLat,mgLon + return mgLat, mgLon dLat = transformLat(wgLon - 105.0, wgLat - 35.0) dLon = transformLon(wgLon - 105.0, wgLat - 35.0) radLat = wgLat / 180.0 * pi @@ -55,7 +59,8 @@ def transform(wgLat, wgLon): dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi) mgLat = wgLat + dLat mgLon = wgLon + dLon - return mgLat,mgLon + return mgLat, mgLon + def outOfChina(lat, lon): if (lon < 72.004 or lon > 137.8347): @@ -64,6 +69,7 @@ def outOfChina(lat, lon): return True return False + def transformLat(x, y): ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x)) ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0 @@ -71,6 +77,7 @@ def transformLat(x, y): ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0 return ret + def transformLon(x, y): ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x)) ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0 diff --git a/searchbot.py b/searchbot.py index e18875f..ee504d5 100644 --- a/searchbot.py +++ b/searchbot.py @@ -3,8 +3,10 @@ from telegram.ext import Updater, CommandHandler import logging # Enable logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', - level=logging.DEBUG) + level=logging.DEBUG) logger = logging.getLogger(__name__) + + def search(bot, update, args): agentid = args[0] print(agentid) @@ -15,10 +17,16 @@ def search(bot, update, args): for i in res: print(i) bot.sendMessage(update.message.chat_id, text=str(i)) + + def start(bot, update): bot.sendMessage(update.message.chat_id, text="Hi! Use /search agentid to start, no need for @") + + def error(bot, update, error): logger.warn('Update "%s" caused error "%s"' % (update, error)) + + def main(): updater = Updater('Telegram Bot ID') dp = updater.dispatcher @@ -28,5 +36,6 @@ def main(): dp.add_error_handler(error) updater.start_polling() updater.idle() + if __name__ == "__main__": main() -- cgit v1.2.3