diff options
author | Jinwei Zhao <[email protected]> | 2017-01-02 22:05:44 +0800 |
---|---|---|
committer | Jinwei Zhao <[email protected]> | 2017-01-02 22:05:44 +0800 |
commit | cb52be3055b9ef05b5d1ead9431fdd69834ee3ba (patch) | |
tree | 82447e7e18dc3e2dfc7fa32a076ee5d016e64e2b /ingrex | |
parent | cff3f50819073668391465dd8121bf9e0b8a7c33 (diff) | |
download | COMM2TG-cb52be3055b9ef05b5d1ead9431fdd69834ee3ba.tar.gz |
init
Diffstat (limited to 'ingrex')
-rwxr-xr-x | ingrex/__init__.py | 4 | ||||
-rwxr-xr-x | ingrex/intel.py | 144 | ||||
-rwxr-xr-x | ingrex/praser.py | 25 | ||||
-rwxr-xr-x | ingrex/utils.py | 80 |
4 files changed, 253 insertions, 0 deletions
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 @@ | |||
1 | "Init" | ||
2 | from . intel import Intel | ||
3 | from . praser import Message | ||
4 | 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 @@ | |||
1 | "Ingrex is a python lib for ingress" | ||
2 | import requests | ||
3 | import re | ||
4 | import json | ||
5 | import os | ||
6 | |||
7 | class Intel(object): | ||
8 | "main class with all Intel functions" | ||
9 | |||
10 | def __init__(self, cookies, field): | ||
11 | self.DEBUG = True | ||
12 | token = re.findall(r'csrftoken=(\w*);', cookies)[0] | ||
13 | self.headers = { | ||
14 | 'accept-encoding': 'gzip, deflate', | ||
15 | 'content-type': 'application/json; charset=UTF-8', | ||
16 | 'cookie': cookies, | ||
17 | 'origin': 'https://www.ingress.com', | ||
18 | 'referer': 'https://www.ingress.com/intel', | ||
19 | 'user-agent': 'Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; Trident/5.0)', | ||
20 | 'x-csrftoken': token, | ||
21 | } | ||
22 | self.field = { | ||
23 | 'maxLatE6': field['maxLatE6'], | ||
24 | 'minLatE6': field['minLatE6'], | ||
25 | 'maxLngE6': field['maxLngE6'], | ||
26 | 'minLngE6': field['minLngE6'], | ||
27 | } | ||
28 | self.point = { | ||
29 | 'latE6': (field['maxLatE6'] + field['minLatE6']) >> 1, | ||
30 | 'lngE6': (field['maxLngE6'] + field['minLngE6']) >> 1, | ||
31 | } | ||
32 | self.session = requests.session() | ||
33 | self.refresh_version() | ||
34 | |||
35 | def refresh_version(self): | ||
36 | "refresh api version for request" | ||
37 | request = self.session.get('https://www.ingress.com/intel', headers=self.headers) | ||
38 | self.version = re.findall(r'gen_dashboard_(\w*)\.js', request.text)[0] | ||
39 | |||
40 | def fetch(self, url, payload): | ||
41 | "raw request with auto-retry and connection check function" | ||
42 | payload['v'] = self.version | ||
43 | count = 0 | ||
44 | while count < 3: | ||
45 | try: | ||
46 | request = self.session.post(url, data=json.dumps(payload), headers=self.headers) | ||
47 | return request.json()['result'] | ||
48 | except requests.ConnectionError: | ||
49 | raise IntelError | ||
50 | except Exception: | ||
51 | count += 1 | ||
52 | continue | ||
53 | raise CookieError | ||
54 | |||
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'" | ||
57 | url = 'https://www.ingress.com/r/getPlexts' | ||
58 | payload = { | ||
59 | 'maxLatE6': self.field['maxLatE6'], | ||
60 | 'minLatE6': self.field['minLatE6'], | ||
61 | 'maxLngE6': self.field['maxLngE6'], | ||
62 | 'minLngE6': self.field['minLngE6'], | ||
63 | 'maxTimestampMs': maxts, | ||
64 | 'minTimestampMs': mints, | ||
65 | 'tab': tab | ||
66 | } | ||
67 | if reverse: | ||
68 | payload['ascendingTimestampOrder'] = True | ||
69 | return self.fetch(url, payload) | ||
70 | |||
71 | def fetch_map(self, tilekeys): | ||
72 | "fetch game entities from Ingress map" | ||
73 | url = 'https://www.ingress.com/r/getEntities' | ||
74 | payload = { | ||
75 | 'tileKeys': tilekeys | ||
76 | } | ||
77 | return self.fetch(url, payload) | ||
78 | |||
79 | def fetch_portal(self, guid): | ||
80 | "fetch portal details from Ingress" | ||
81 | url = 'https://www.ingress.com/r/getPortalDetails' | ||
82 | payload = { | ||
83 | 'guid': guid | ||
84 | } | ||
85 | return self.fetch(url, payload) | ||
86 | |||
87 | def fetch_score(self): | ||
88 | "fetch the global score of RESISTANCE and ENLIGHTENED" | ||
89 | url = 'https://www.ingress.com/r/getGameScore' | ||
90 | payload = {} | ||
91 | return self.fetch(url, payload) | ||
92 | |||
93 | def fetch_region(self): | ||
94 | "fetch the region info of RESISTANCE and ENLIGHTENED" | ||
95 | url = 'https://www.ingress.com/r/getRegionScoreDetails' | ||
96 | payload = { | ||
97 | 'lngE6': self.point['lngE6'], | ||
98 | 'latE6': self.point['latE6'], | ||
99 | } | ||
100 | return self.fetch(url, payload) | ||
101 | |||
102 | def fetch_artifacts(self): | ||
103 | "fetch the artifacts details" | ||
104 | url = 'https://www.ingress.com/r/getArtifactPortals' | ||
105 | payload = {} | ||
106 | return self.fetch(url, payload) | ||
107 | |||
108 | def send_msg(self, msg, tab='all'): | ||
109 | "send a message to Ingress COMM, tab can be 'all', 'faction'" | ||
110 | url = 'https://www.ingress.com/r/sendPlext' | ||
111 | payload = { | ||
112 | 'message': msg, | ||
113 | 'latE6': self.point['latE6'], | ||
114 | 'lngE6': self.point['lngE6'], | ||
115 | 'tab': tab | ||
116 | } | ||
117 | return self.fetch(url, payload) | ||
118 | |||
119 | def send_invite(self, address): | ||
120 | "send a recruit to an email address" | ||
121 | url = 'https://www.ingress.com/r/sendInviteEmail' | ||
122 | payload = { | ||
123 | 'inviteeEmailAddress': address | ||
124 | } | ||
125 | return self.fetch(url, payload) | ||
126 | |||
127 | def redeem_code(self, passcode): | ||
128 | "redeem a passcode" | ||
129 | url = 'https://www.ingress.com/r/redeemReward' | ||
130 | payload = { | ||
131 | 'passcode': passcode | ||
132 | } | ||
133 | return self.fetch(url, payload) | ||
134 | |||
135 | class IntelError(BaseException): | ||
136 | """Intel Error""" | ||
137 | pass | ||
138 | |||
139 | class CookieError(IntelError): | ||
140 | """Intel Error""" | ||
141 | pass | ||
142 | |||
143 | if __name__ == '__main__': | ||
144 | 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 @@ | |||
1 | "Ingrex praser deal with message" | ||
2 | from datetime import datetime, timedelta | ||
3 | import platform | ||
4 | import os | ||
5 | import time | ||
6 | |||
7 | osname = platform.system() | ||
8 | if osname == "Linux": | ||
9 | os.environ['TZ'] = 'Asia/Shanghai' | ||
10 | time.tzset() | ||
11 | |||
12 | class Message(object): | ||
13 | "Message object" | ||
14 | def __init__(self, raw_msg): | ||
15 | self.raw = raw_msg | ||
16 | self.guid = raw_msg[0] | ||
17 | self.timestamp = raw_msg[1] | ||
18 | seconds, millis = divmod(raw_msg[1], 1000) | ||
19 | time = datetime.fromtimestamp(seconds) + timedelta(milliseconds=millis) | ||
20 | self.time = time.strftime('%Y/%m/%d %H:%M:%S:%f')[:-3] | ||
21 | self.text = raw_msg[2]['plext']['text'] | ||
22 | self.ptype = raw_msg[2]['plext']['plextType'] | ||
23 | self.team = raw_msg[2]['plext']['team'] | ||
24 | self.type = raw_msg[2]['plext']['markup'][1][1]['plain'] | ||
25 | |||
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 @@ | |||
1 | "Map Utils" | ||
2 | from math import pi, sin, cos, tan, asin, radians, sqrt, log | ||
3 | |||
4 | def calc_tile(lng, lat, zoomlevel): | ||
5 | tilecounts = [1,1,1,40,40,80,80,320,1E3,2E3,2E3,4E3,8E3,16E3,16E3,32E3] | ||
6 | rlat = radians(lat) | ||
7 | tilecount = tilecounts[zoomlevel] | ||
8 | xtile = int((lng + 180.0) / 360.0 * tilecount) | ||
9 | ytile = int((1.0 - log(tan(rlat) + (1 / cos(rlat))) / pi) / 2.0 * tilecount) | ||
10 | return xtile, ytile | ||
11 | |||
12 | def calc_dist(lat1, lng1, lat2, lng2): | ||
13 | lat1, lng1, lat2, lng2 = map(radians, [lat1, lng1, lat2, lng2]) | ||
14 | dlat = lat1 - lat2 | ||
15 | dlng = lng1 - lng2 | ||
16 | a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlng/2)**2 | ||
17 | c = 2* asin(sqrt(a)) | ||
18 | m = 6367.0 * c * 1000 | ||
19 | return m | ||
20 | |||
21 | def point_in_poly(x, y, poly): | ||
22 | n = len(poly) | ||
23 | inside = False | ||
24 | p1x,p1y = poly[0] | ||
25 | for i in range(n+1): | ||
26 | p2x,p2y = poly[i % n] | ||
27 | if y > min(p1y, p2y): | ||
28 | if y <= max(p1y, p2y): | ||
29 | if x <= max(p1x, p2x): | ||
30 | if p1y != p2y: | ||
31 | xints = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x | ||
32 | if p1x == p2x or x <= xints: | ||
33 | inside = not inside | ||
34 | p1x,p1y = p2x,p2y | ||
35 | return inside | ||
36 | |||
37 | def transform(wgLat, wgLon): | ||
38 | """ | ||
39 | transform(latitude,longitude) , WGS84 | ||
40 | return (latitude,longitude) , GCJ02 | ||
41 | """ | ||
42 | a = 6378245.0 | ||
43 | ee = 0.00669342162296594323 | ||
44 | if (outOfChina(wgLat, wgLon)): | ||
45 | mgLat = wgLat | ||
46 | mgLon = wgLon | ||
47 | return mgLat,mgLon | ||
48 | dLat = transformLat(wgLon - 105.0, wgLat - 35.0) | ||
49 | dLon = transformLon(wgLon - 105.0, wgLat - 35.0) | ||
50 | radLat = wgLat / 180.0 * pi | ||
51 | magic = sin(radLat) | ||
52 | magic = 1 - ee * magic * magic | ||
53 | sqrtMagic = sqrt(magic) | ||
54 | dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi) | ||
55 | dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi) | ||
56 | mgLat = wgLat + dLat | ||
57 | mgLon = wgLon + dLon | ||
58 | return mgLat,mgLon | ||
59 | |||
60 | def outOfChina(lat, lon): | ||
61 | if (lon < 72.004 or lon > 137.8347): | ||
62 | return True | ||
63 | if (lat < 0.8293 or lat > 55.8271): | ||
64 | return True | ||
65 | return False | ||
66 | |||
67 | def transformLat(x, y): | ||
68 | ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x)) | ||
69 | ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0 | ||
70 | ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0 | ||
71 | ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0 | ||
72 | return ret | ||
73 | |||
74 | def transformLon(x, y): | ||
75 | ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x)) | ||
76 | ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0 | ||
77 | ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0 | ||
78 | ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0 | ||
79 | return ret | ||
80 | |||