8000 Optimizing the server into an async server model by MayaChen350 · Pull Request #68 · 3DS-RPC/3DS-RPC · GitHub
[go: up one dir, main page]

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions api/love3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
from Cryptodome.Cipher import AES
import Cryptodome.Cipher.AES
from binascii import hexlify, unhexlify
import requests, io, math, json, os
import asyncio, io, math, json, os
from PIL import Image
from server import client
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little wary of this change, as api (and thus love3) is indirectly required for things such as backend.py and discord.py.

It'd be nice to ideally have them all merged together, but in the meantime, would it be possible to pass client as a parameter?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try this out tonight


def getTitleData(titleID:hex):
data = requests.get('https://idbe-ctr.cdn.nintendo.net/icondata/10/%s.idbe' % titleID.zfill(16), verify = False).content
async def getTitleData(titleID:hex):
data = (await client.get('https://idbe-ctr.cdn.nintendo.net/icondata/10/%s.idbe' % titleID.zfill(16), verify = False)).content

IV = unhexlify('A46987AE47D82BB4FA8ABC0450285FA4')

Expand All @@ -29,7 +30,7 @@ def getTitleData(titleID:hex):

return decipher.decrypt(data[2:])

def getTitleInfo(titleID:hex):
async def getTitleInfo(titleID:hex):
titleID = str(titleID)
try:
int(titleID, 16) # Errors if not HEX
Expand All @@ -48,7 +49,7 @@ def getTitleInfo(titleID:hex):
return json.loads(file.read())

try:
data = getTitleData(titleID)
data = await getTitleData(titleID)
except:
with open('cache/homebrew' + titleID + '.txt', 'w') as file:
file.write('')
Expand Down
8 changes: 4 additions & 4 deletions api/util.py
F440
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, width:int = terminalSize):
self.progress = 0
self.close = True

def update(self, fraction:float):
def update(self, fraction:float): # TODO: Async this
fraction = int(fraction * self.width)
self.progress += fraction
def loop(self):
Expand All @@ -43,7 +43,7 @@ def loop(self):
self.close = True
threading.Thread(target = loop, args = (self,)).start()

def end(self): # Can take up time on main thread to finish
def end(self): # Can take up time on main thread to finish TODO: Async this
for n in range(self.width - self.progress):
sys.stdout.write('#')
sys.stdout.flush()
Expand All @@ -53,7 +53,7 @@ def end(self): # Can take up time on main thread to finish
sys.stdout.write(']\n')

# Get image url from title ID
def getTitle(titleID, titlesToUID, titleDatabase):
async def getTitle(titleID, titlesToUID, titleDatabase):
_pass = None

uid = None
Expand Down Expand Up @@ -104,7 +104,7 @@ def getTitle(titleID, titlesToUID, titleDatabase):
game[key] = _template[key]

if game == _template:
response = getTitleInfo(titleID)
response = await getTitleInfo(titleID)
if response:
game['name'] = response['short']
game['publisher']['name'] = response['publisher']
Expand Down
16 changes: 8 additions & 8 deletions backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def main():
session = Session(engine)

while True:
time.sleep(1)
await asyncio.sleep(1)
print('Grabbing new friends...')

queried_friends = session.scalars(select(Friend).where(Friend.network == network)).all()
Expand Down Expand Up @@ -104,7 +104,7 @@ async def main():
except Exception as e:
print('An error occurred!\n%s' % e)
print(traceback.format_exc())
time.sleep(2)
await asyncio.sleep(2)

if scrape_only:
print('Done scraping.')
Expand All @@ -114,7 +114,7 @@ async def main():
async def main_friends_loop(friends_client: friends.FriendsClientV1, session: Session, current_rotation: list[QueriedFriend]):
# If we recently started, update our comment, and remove existing friends.
if time.time() - backend_start_time < 30:
time.sleep(delay)
await asyncio.sleep(delay)
await friends_client.update_comment('3dsrpc.com')

# Synchronize our current roster of friends.
Expand All @@ -129,20 +129,20 @@ async def main_friends_loop(friends_client: friends.FriendsClientV1, session: Se
# Clear our current, registered friends.
removables = await friends_client.get_all_friends()
for friend in removables:
time.sleep(delay)
await asyncio.sleep(delay)
await friends_client.remove_friend_by_principal_id(friend.pid)

# Individually add all pending friend PIDs.
for friend_pid in all_friend_pids:
time.sleep(delay)
await asyncio.sleep(delay)
await friends_client.add_friend_by_principal_id(0, friend_pid)
else:
# We expect the remote NEX implementation to remove all existing
# relationships, and replace them with the 100 PIDs specified.
# This path is currently only for Nintendo.
await friends_client.sync_friend(0, all_friend_pids, [])

time.sleep(delay)
await asyncio.sleep(delay)

# Query all successful friends.
current_friends_list: [friends.FriendRelationship] = await friends_client.get_all_friends()
Expand Down Expand Up @@ -170,7 +170,7 @@ async def main_friends_loop(friends_client: friends.FriendsClientV1, session: Se
# All of our friends removed us, so there's no more work to be done.
return

time.sleep(delay)
await asyncio.sleep(delay)

# Query the presences of all of our added friends.
# Only online users will have their presence returned.
Expand Down Expand Up @@ -229,7 +229,7 @@ async def main_friends_loop(friends_client: friends.FriendsClientV1, session: Se
if not work:
continue

time.sleep(delay)
await asyncio.sleep(delay)

try:
current_info = await friends_client.get_friend_persistent_info([current_friend.pid,])
Expand Down
Loading
0