From cb52be3055b9ef05b5d1ead9431fdd69834ee3ba Mon Sep 17 00:00:00 2001 From: Jinwei Zhao Date: Mon, 2 Jan 2017 22:05:44 +0800 Subject: init --- ingrex/__init__.py | 4 ++ ingrex/intel.py | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ingrex/praser.py | 25 ++++++++++ ingrex/utils.py | 80 +++++++++++++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100755 ingrex/__init__.py create mode 100755 ingrex/intel.py create mode 100755 ingrex/praser.py create mode 100755 ingrex/utils.py (limited to 'ingrex') diff --git a/ingrex/__init__.py b/ingrex/__init__.py new file mode 100755 index 0000000..91876bf --- /dev/null +++ b/ingrex/__init__.py @@ -0,0 +1,4 @@ +"Init" +from . intel import Intel +from . praser import Message +from . import utils as Utils diff --git a/ingrex/intel.py b/ingrex/intel.py new file mode 100755 index 0000000..f13b2a2 --- /dev/null +++ b/ingrex/intel.py @@ -0,0 +1,144 @@ +"Ingrex is a python lib for ingress" +import requests +import re +import json +import os + +class Intel(object): + "main class with all Intel functions" + + def __init__(self, cookies, field): + self.DEBUG = True + token = re.findall(r'csrftoken=(\w*);', cookies)[0] + self.headers = { + 'accept-encoding': 'gzip, deflate', + 'content-type': 'application/json; charset=UTF-8', + 'cookie': cookies, + 'origin': 'https://www.ingress.com', + 'referer': 'https://www.ingress.com/intel', + 'user-agent': 'Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; Trident/5.0)', + 'x-csrftoken': token, + } + self.field = { + 'maxLatE6': field['maxLatE6'], + 'minLatE6': field['minLatE6'], + 'maxLngE6': field['maxLngE6'], + 'minLngE6': field['minLngE6'], + } + self.point = { + 'latE6': (field['maxLatE6'] + field['minLatE6']) >> 1, + 'lngE6': (field['maxLngE6'] + field['minLngE6']) >> 1, + } + self.session = requests.session() + self.refresh_version() + + def refresh_version(self): + "refresh api version for request" + request = self.session.get('https://www.ingress.com/intel', headers=self.headers) + self.version = re.findall(r'gen_dashboard_(\w*)\.js', request.text)[0] + + def fetch(self, url, payload): + "raw request with auto-retry and connection check function" + payload['v'] = self.version + count = 0 + while count < 3: + try: + request = self.session.post(url, data=json.dumps(payload), headers=self.headers) + return request.json()['result'] + except requests.ConnectionError: + raise IntelError + except Exception: + count += 1 + continue + raise CookieError + + def fetch_msg(self, mints=-1, maxts=-1, reverse=False, tab='all'): + "fetch message from Ingress COMM, tab can be 'all', 'faction', 'alerts'" + url = 'https://www.ingress.com/r/getPlexts' + payload = { + 'maxLatE6': self.field['maxLatE6'], + 'minLatE6': self.field['minLatE6'], + 'maxLngE6': self.field['maxLngE6'], + 'minLngE6': self.field['minLngE6'], + 'maxTimestampMs': maxts, + 'minTimestampMs': mints, + 'tab': tab + } + if reverse: + payload['ascendingTimestampOrder'] = True + return self.fetch(url, payload) + + def fetch_map(self, tilekeys): + "fetch game entities from Ingress map" + url = 'https://www.ingress.com/r/getEntities' + payload = { + 'tileKeys': tilekeys + } + return self.fetch(url, payload) + + def fetch_portal(self, guid): + "fetch portal details from Ingress" + url = 'https://www.ingress.com/r/getPortalDetails' + payload = { + 'guid': guid + } + return self.fetch(url, payload) + + def fetch_score(self): + "fetch the global score of RESISTANCE and ENLIGHTENED" + url = 'https://www.ingress.com/r/getGameScore' + payload = {} + return self.fetch(url, payload) + + def fetch_region(self): + "fetch the region info of RESISTANCE and ENLIGHTENED" + url = 'https://www.ingress.com/r/getRegionScoreDetails' + payload = { + 'lngE6': self.point['lngE6'], + 'latE6': self.point['latE6'], + } + return self.fetch(url, payload) + + def fetch_artifacts(self): + "fetch the artifacts details" + url = 'https://www.ingress.com/r/getArtifactPortals' + payload = {} + return self.fetch(url, payload) + + def send_msg(self, msg, tab='all'): + "send a message to Ingress COMM, tab can be 'all', 'faction'" + url = 'https://www.ingress.com/r/sendPlext' + payload = { + 'message': msg, + 'latE6': self.point['latE6'], + 'lngE6': self.point['lngE6'], + 'tab': tab + } + return self.fetch(url, payload) + + def send_invite(self, address): + "send a recruit to an email address" + url = 'https://www.ingress.com/r/sendInviteEmail' + payload = { + 'inviteeEmailAddress': address + } + return self.fetch(url, payload) + + def redeem_code(self, passcode): + "redeem a passcode" + url = 'https://www.ingress.com/r/redeemReward' + payload = { + 'passcode': passcode + } + return self.fetch(url, payload) + +class IntelError(BaseException): + """Intel Error""" + pass + +class CookieError(IntelError): + """Intel Error""" + pass + +if __name__ == '__main__': + pass diff --git a/ingrex/praser.py b/ingrex/praser.py new file mode 100755 index 0000000..732ab1b --- /dev/null +++ b/ingrex/praser.py @@ -0,0 +1,25 @@ +"Ingrex praser deal with message" +from datetime import datetime, timedelta +import platform +import os +import time + +osname = platform.system() +if osname == "Linux": + os.environ['TZ'] = 'Asia/Shanghai' + time.tzset() + +class Message(object): + "Message object" + def __init__(self, raw_msg): + self.raw = raw_msg + self.guid = raw_msg[0] + self.timestamp = raw_msg[1] + seconds, millis = divmod(raw_msg[1], 1000) + time = datetime.fromtimestamp(seconds) + timedelta(milliseconds=millis) + self.time = time.strftime('%Y/%m/%d %H:%M:%S:%f')[:-3] + self.text = raw_msg[2]['plext']['text'] + self.ptype = raw_msg[2]['plext']['plextType'] + self.team = raw_msg[2]['plext']['team'] + self.type = raw_msg[2]['plext']['markup'][1][1]['plain'] + diff --git a/ingrex/utils.py b/ingrex/utils.py new file mode 100755 index 0000000..8090f15 --- /dev/null +++ b/ingrex/utils.py @@ -0,0 +1,80 @@ +"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] + 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 + dlng = lng1 - lng2 + a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlng/2)**2 + c = 2* asin(sqrt(a)) + m = 6367.0 * c * 1000 + return m + +def point_in_poly(x, y, poly): + n = len(poly) + inside = False + p1x,p1y = poly[0] + for i in range(n+1): + p2x,p2y = poly[i % n] + if y > min(p1y, p2y): + if y <= max(p1y, p2y): + if x <= max(p1x, p2x): + if p1y != p2y: + xints = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x + if p1x == p2x or x <= xints: + inside = not inside + p1x,p1y = p2x,p2y + return inside + +def transform(wgLat, wgLon): + """ + transform(latitude,longitude) , WGS84 + return (latitude,longitude) , GCJ02 + """ + a = 6378245.0 + ee = 0.00669342162296594323 + if (outOfChina(wgLat, wgLon)): + mgLat = wgLat + mgLon = wgLon + return mgLat,mgLon + dLat = transformLat(wgLon - 105.0, wgLat - 35.0) + dLon = transformLon(wgLon - 105.0, wgLat - 35.0) + radLat = wgLat / 180.0 * pi + magic = sin(radLat) + magic = 1 - ee * magic * magic + sqrtMagic = sqrt(magic) + dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi) + dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi) + mgLat = wgLat + dLat + mgLon = wgLon + dLon + return mgLat,mgLon + +def outOfChina(lat, lon): + if (lon < 72.004 or lon > 137.8347): + return True + if (lat < 0.8293 or lat > 55.8271): + 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 + ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0 + 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 + ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0 + ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0 + return ret + -- cgit v1.2.3