-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.py
237 lines (173 loc) · 7.71 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import xmltodict
import requests
import urllib.parse
import time
from .security import ChecksumCreateDevice, ChecksumTimeForDate, ChecksumPasswordWithString, ChecksumEmailAuthorize
from .dotnet import DotNet
class User(object):
id = 0
name = None
isAuthorized = False
lastHeartBeat = 0
def __init__(self, id, name, lastHeartBeat, isAuthorized):
self.id = id
self.name = name
self.lastHeartBeat = lastHeartBeat
self.isAuthorized = True if isAuthorized else False
class Client(object):
# device data
device = None
# configuration
salt = '5343'
headers = {
'Accept': '*/*',
'Accept-Encoding': 'identity',
'User-Agent': "UnityPlayer/5.6.0f3 (UnityWebRequest/1.0, libcurl/7.51.0-DEV)",
'X-Unity-Version': '5.6.0f3'
}
baseUrl = 'https://api.pixelstarships.com/UserService/'
# runtime data
user = None
accessToken = None
freeStarbuxToday = 0
freeStarbuxTodayTimestamp = 0
def __init__(self, device):
self.device = device
def parseUserLoginData(self, r):
d = xmltodict.parse(r.content, xml_attribs=True)
info = d['UserService']['UserLogin']
userId = d['UserService']['UserLogin']['@UserId']
if not self.device.refreshToken:
myName = 'guest'
else:
myName = d['UserService']['UserLogin']['User']['@Name']
LastHeartBeat = d['UserService']['UserLogin']['User']['@LastHeartBeatDate']
if 'FreeStarbuxReceivedToday' in r.text:
self.freeStarbuxToday = int(r.text.split('FreeStarbuxReceivedToday="')[1].split('"')[0])
# keep it
self.user = User(userId, myName, LastHeartBeat, self.device.refreshToken)
def getAccessToken(self, refreshToken=None):
if self.accessToken:
return self.accessToken
url = self.baseUrl + 'DeviceLogin8?deviceKey='+self.device.key+'&advertisingKey=&isJailBroken=False&checksum='+ChecksumCreateDevice(self.device.key, self.device.name)+'&deviceType=DeviceType'+self.device.name+'&signal=False&languageKey=' + self.device.languageKey
url += '&refreshToken='+(self.device.refreshToken if self.device.refreshToken else '')
r = self.request(url, 'POST')
if not r or r.status_code != 200:
print('[getAccessToken]', 'failed with data:', r.text)
return None
if 'errorCode' in r.text:
print('[getAccessToken]', 'got an error with data:', r.text)
return None
self.parseUserLoginData(r)
if 'accessToken' not in r.text:
print('[getAccessToken]', 'got no accessToken with data:', r.text)
return None
self.accessToken = r.text.split('accessToken="')[1].split('"')[0]
return True
def quickReload(self):
self.accessToken = None
return self.getAccessToken(self.device.refreshToken)
def login(self, email=None, password=None):
if not self.getAccessToken(self.device.refreshToken):
print('[login] failed to get access token')
return
# double check if something goes wrong
if not self.accessToken:
return
# authorization just fine with refreshToken, we're in da house
if self.device.refreshToken and self.accessToken:
return True
# accessToken is enough for guest to play a tutorial
if self.accessToken and not email:
return True
# login with credentials and accessToken
ts = '{0:%Y-%m-%dT%H:%M:%S}'.format(DotNet.validDateTime())
checksum = ChecksumEmailAuthorize(self.device.key, email, ts, self.accessToken, self.salt)
# if refreshToken was used we get acquire session without credentials
if self.device.refreshToken:
url = self.baseUrl + 'UserEmailPasswordAuthorize2?clientDateTime={}&checksum={}&deviceKey={}&accessToken={}&refreshToken={}'.format(
ts,
checksum,
self.device.key,
self.accessToken,
self.device.refreshToken
)
r = self.request(url, 'POST')
if 'Email=' not in r.text:
print('[login] failed to authenticate with refreshToken:', r.text)
return None
self.parseUserLoginData(r)
else:
email = urllib.parse.quote(email)
url = self.baseUrl + 'UserEmailPasswordAuthorize2?clientDateTime={}&checksum={}&deviceKey={}&email={}&password={}&accessToken={}'.format(
ts,
checksum,
self.device.key,
email,
password,
self.accessToken
)
r = self.request(url, 'POST')
if 'errorMessage=' in r.text:
print('[login] failed to authorize with credentials with the reason:', r.text)
return False
if 'refreshToken' not in r.text:
print('[login] failed to acquire refreshToken with th reason', r.text)
return False
self.device.refreshTokenAcquire(r.text.split('refreshToken="')[1].split('"')[0])
if 'RequireReload="True"' in r.text:
return self.quickReload()
if 'refreshToken' in r.text:
self.device.refreshTokenAcquire(r.text.split('refreshToken="')[1].split('"')[0])
return True
def loadShip(self):
url = 'https://api.pixelstarships.com/ShipService/GetShipByUserId?userId={}&accessToken={}&clientDateTime={}'.format(
self.user.id,
self.accessToken,
'{0:%Y-%m-%dT%H:%M:%S}'.format(DotNet.validDateTime()),
)
r = self.request(url, 'GET')
print('loadShip', r, r.text)
return r
def readyForFreeStabux(self):
if self.user.isAuthorized and self.freeStarbuxToday < 25 and self.freeStarbuxTodayTimestamp + 120 < time.time():
return True
return False
def grabFlyingStarbux(self, quantity):
t = DotNet.validDateTime()
url = self.baseUrl + 'AddStarbux2?quantity={}&clientDateTime={}&checksum={}&accessToken={}'.format(
quantity,
'{0:%Y-%m-%dT%H:%M:%S}'.format(t),
ChecksumTimeForDate(DotNet.get_time()) + ChecksumPasswordWithString(self.accessToken),
self.accessToken
)
r = self.request(url, 'POST')
if 'Email=' not in r.text:
print('[grabFlyingStarbux] failed with next issue:', r, r.text)
return False
self.freeStarbuxToday = int(r.text.split('FreeStarbuxReceivedToday="')[1].split('"')[0])
self.freeStarbuxTodayTimestamp = time.time()
return True
def heartbeat(self):
if self.user.lastHeartBeat:
hours = self.user.lastHeartBeat.split('T')[1]
seconds = hours.split(':')[-1]
if DotNet.validDateTime().second == int(seconds):
return
t = DotNet.validDateTime()
url = self.baseUrl + 'HeartBeat4?clientDateTime={}&checksum={}&accessToken={}'.format(
'{0:%Y-%m-%dT%H:%M:%S}'.format(t),
ChecksumTimeForDate(DotNet.get_time()) + ChecksumPasswordWithString(self.accessToken),
self.accessToken
)
r = self.request(url, 'POST')
success = False
if r.status_code == 200 and 'success="t' in r.text:
success = True
else:
print('heartbeat fail', r, r.text)
self.user.lastHeartBeat = '{0:%Y-%m-%dT%H:%M:%S}'.format(t)
return success
def request(self, url, method=None, data=None):
r = requests.request(method, url, headers=self.headers, data=data)
return r