diff options
Diffstat (limited to 'ingrex/intel.py')
-rwxr-xr-x | ingrex/intel.py | 144 |
1 files changed, 144 insertions, 0 deletions
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 | ||