Skip to content

Commit

Permalink
Lower heavy usage of 3rd-party API (#3)
Browse files Browse the repository at this point in the history
(Sorry again, @frozenpandaman!)
This commit should make each ping to the `s2s` and `flapg` APIs an hour apart. It also speeds up update time as it shouldn't be as risky with an API call other than `/v3/Account/Login`
  • Loading branch information
MCMi460 committed Mar 30, 2022
1 parent b43a412 commit d85286d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 55 deletions.
76 changes: 27 additions & 49 deletions client/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,33 @@
import re

client_id = '71b963c1b7b6d119'
version = 0.1
version = 0.2
nsoAppVersion = '2.0.0'

class API():
def __init__(self, session_token):
self.headers = {
'X-ProductVersion': '2.0.0',
'X-Platform': 'iOS',
'User-Agent': 'Coral/2.0.0 (com.nintendo.znca; build:1489; iOS 15.3.1) Alamofire/5.4.4',
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'Host': 'api-lp1.znc.srv.nintendo.net',
'Connection': 'Keep-Alive',
'Accept-Encoding': 'gzip',
'User-Agent': 'Coral/2.0.0 (com.nintendo.znca; build:1489; iOS 15.3.1) Alamofire/5.4.4',
}

self.tokenResponse = Nintendo(session_token).getServiceToken()
self.id_token = self.tokenResponse['id_token']
self.accessToken = self.tokenResponse['access_token']
self.guid = str(uuid.uuid4())

self.headers['Authorization'] = 'Bearer %s' % self.accessToken # Add authorization token
self.url = 'https://api-lp1.znc.srv.nintendo.net'

self.userInfo = UsersMe(self.accessToken).get()
self.s2sHash = {
'hash': None,
'time': time.time(),
self.login = {
'login': None,
'time': 0,
}

path = os.path.expanduser('~/Documents/NSO-RPC')
Expand All @@ -49,13 +53,19 @@ def makeRequest(self, route):
return requests.post(self.url + route, headers = self.headers)

def updateLogin(self):
if time.time() - self.s2sHash['time'] >= 120:
self.s2sHash = {
'hash': None,
'time': time.time(),
}
self.login = Login(self.userInfo, self.accessToken, self.guid, self.s2sHash['hash'])
self.login.loginToAccount()
login = Login(self.userInfo, self.accessToken, self.guid)
login.loginToAccount()
self.headers['Authorization'] = 'Bearer %s' % login.account['result'].get('webApiServerCredential').get('accessToken') # Add authorization token
self.login = {
'login': login,
'time': time.time(),
}

def getSelf(self):
self.route = '/v3/User/ShowSelf'

response = self.makeRequest(self.route)
self.user = User(json.loads(response.text)['result'])

class Nintendo():
def __init__(self, sessionToken):
Expand Down Expand Up @@ -113,16 +123,12 @@ def getHash(self):
return json.loads(response.text)['hash']

class Flapg():
def __init__(self, id_token, timestamp, guid, s2sHash = None):
if s2sHash:
hash = s2sHash
else:
hash = s2s(id_token, timestamp).getHash()
def __init__(self, id_token, timestamp, guid):
self.headers = {
'x-token': id_token,
'x-time': str(timestamp),
'x-guid': guid,
'x-hash': hash,
'x-hash': s2s(id_token, timestamp).getHash(),
'x-ver': '3',
'x-iid': 'nso',
}
Expand All @@ -136,7 +142,7 @@ def get(self):
return json.loads(response.text)['result']

class Login():
def __init__(self, userInfo, accessToken, guid, s2sHash = None):
def __init__(self, userInfo, accessToken, guid):
self.headers = {
'Host': 'api-lp1.znc.srv.nintendo.net',
'Accept-Language': 'en-US',
Expand All @@ -157,7 +163,7 @@ def __init__(self, userInfo, accessToken, guid, s2sHash = None):
self.userInfo = userInfo
self.accessToken = accessToken

self.flapg = Flapg(self.accessToken, self.timestamp, self.guid, s2sHash).get()
self.flapg = Flapg(self.accessToken, self.timestamp, self.guid).get()

self.account = None

Expand All @@ -178,19 +184,6 @@ def loginToAccount(self):
self.account = json.loads(response.text)
return self.account

class FriendList(API):
def __init__(self):
super().__init__()

self.route = '/v3/Friend/List' # Define API route

self.friendList = [] # List of Friend object(s)

def populateList(self):
response = self.makeRequest(self.route)
arr = json.loads(response.text)['result']['friends']
self.friendList = [ Friend(friend) for friend in arr ]

class User():
def __init__(self, f):
self.id = f.get('id')
Expand All @@ -210,21 +203,6 @@ def description(self):
+ ' - Status: %s\n' % self.presence.description()
)

class Friend(User):
def __init__(self, f):
super().__init__(f)
self.isFriend = f.get('isFriend')
self.isFavoriteFriend = f.get('isFavoriteFriend')
self.isServiceUser = f.get('isServiceUser')
self.friendCreatedAt = f.get('friendCreatedAt')

def description(self):
return ('%s (id: %s, nsaId: %s):\n' % (self.name, self.id, self.nsaId)
+ ' - Profile Picture: %s\n' % self.imageUri
+ ' - Friend Creation Date: %s\n' % self.friendCreatedAt
+ ' - Status: %s\n' % self.presence.description()
)

class Presence():
def __init__(self, f):
self.state = f.get('state')
Expand Down
14 changes: 8 additions & 6 deletions client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ def connect(self):
continue

def update(self):
self.api.updateLogin()
if time.time() - self.api.login['time'] >= 3600:
self.api.updateLogin()
self.api.getSelf()
self.nickname = self.api.userInfo['nickname']
self.user = User(self.api.login.account['result'].get('user'))
self.user = self.api.user

presence = self.user.presence
if presence.state != 'INACTIVE':
if presence.state == 'ONLINE':
state = presence.game.sysDescription
if not state:
state = 'Played for %s hours or more' % (int(presence.game.totalPlayTime / 60 / 5) * 5)
Expand All @@ -48,18 +50,18 @@ def update(self):
self.rpc.clear()

def background(self):
second = 60
second = 30
while True:
if self.running:
if second == 60:
if second == 30:
try:
self.update()
except KeyError:
pass
second = 0
second += 1
else:
second = 55
second = 25
time.sleep(1)

if __name__ == '__main__':
Expand Down

0 comments on commit d85286d

Please sign in to comment.