Last active
June 13, 2022 04:14
-
-
Save zeroday0619/eb6eb36e7ab2da1940d30d7eb5239e06 to your computer and use it in GitHub Desktop.
Nintendo Switch discord presence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import sys | |
| import time | |
| import keyring | |
| from pypresence import Presence | |
| from nso_bridge.nsa import NintendoSwitchAccount | |
| from nso_bridge.nso import NintendoSwitchOnlineAPI | |
| import logging | |
| if len(sys.argv) > 1: | |
| if sys.argv[1] == "--debug": | |
| LEVEL = logging.DEBUG | |
| else: | |
| LEVEL = logging.INFO | |
| else: | |
| LEVEL = logging.INFO | |
| logging.basicConfig(level=LEVEL, format='[%(levelname)s] %(asctime)s - %(message)s') | |
| logger = logging.getLogger(__name__) | |
| app = NintendoSwitchAccount() | |
| # override the nso_app_version | |
| app.nso_app_version = "2.1.1" | |
| session_token = keyring.get_password("nso-bridge", "session_token") | |
| nso_res = NintendoSwitchOnlineAPI( | |
| nso_app_version=app.nso_app_version, | |
| session_token=session_token, | |
| ) | |
| user_name = input("Enter your Nintendo Switch Online username: ") | |
| rpc = Presence(client_id=984697454484144138) | |
| rpc.connect() | |
| def select_user(friends_presence): | |
| for friend in friends_presence: | |
| if friend['name'] == user_name: | |
| return friend | |
| logger.error("Could not find your friend in the friend list.") | |
| sys.exit(1) | |
| def json_process(): | |
| logger.info("Syncing...") | |
| nso_res.sync_login() | |
| logger.info("Fetching friend list...") | |
| friends = nso_res.getFriends() | |
| if friends['result']: | |
| friends: list[dict] = friends['result'] | |
| friends_presence = [ | |
| { | |
| 'name': friend['name'], | |
| **friend['presence'], | |
| } for friend in friends['friends'] | |
| ] | |
| logger.debug(f"DATA: {json.dumps(friends_presence, indent=4, ensure_ascii=False)}") | |
| [logger.info(f"{_friend['name']} is {_friend['state']}") for _friend in friends_presence] | |
| friend = select_user(friends_presence) | |
| logger.info(f"selected friend: {friend['name']}") | |
| match friend['state']: | |
| case 'ONLINE': | |
| sys_description = friend['game']['sysDescription'] | |
| if sys_description == '': | |
| total_play_time = int(int(friend['game']['totalPlayTime']) / 60) | |
| sys_description = f'Total play time for {total_play_time} hours' | |
| state = { | |
| 'state': sys_description, | |
| 'details': f'{friend["game"]["name"]}', | |
| 'large_text': f'{friend["game"]["name"]}', | |
| 'large_image': friend['game']['imageUri'], | |
| } | |
| rpc.update( | |
| **state, | |
| ) | |
| logger.info("Presence updated") | |
| case 'INACTIVE': | |
| state = { | |
| 'state': 'idling...', | |
| } | |
| rpc.update(**state) | |
| logger.info("Presence updated") | |
| case 'OFFLINE': | |
| if rpc.sock_writer is None: | |
| pass | |
| else: | |
| rpc.clear() | |
| logger.info("Presence cleared") | |
| case _: | |
| logger.info(f"Unknown state {friend['state']}") | |
| pass | |
| try: | |
| while 1: | |
| json_process() | |
| logger.info("Sleeping... 15 second") | |
| time.sleep(15) | |
| except KeyboardInterrupt: | |
| logger.info("Exiting...") | |
| exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment