diff --git a/scripts/psn-account-id.py b/scripts/psn-account-id.py index 63c0af8..590249d 100755 --- a/scripts/psn-account-id.py +++ b/scripts/psn-account-id.py @@ -3,20 +3,45 @@ import sys if sys.version_info < (3, 0, 0): - print("DO NOT use Python 2.\nEVER.\nhttps://pythonclock.org") - exit(1) + print("DO NOT use Python 2.\nEVER.\nhttps://pythonclock.org") + exit(1) + +import platform +oldexit = exit +def exit(code): + if platform.system() == "Windows": + import atexit + input("Press Enter to exit.") + oldexit(code) if sys.stdout.encoding.lower() == "ascii": - import codecs - sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer) - sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer) + import codecs + sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer) + sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer) try: - import requests + import requests except ImportError as e: - print(e) - print("\"requests\" is not available. Install it with pip whatever.") - exit(1) + print(e) + if platform.system() == "Windows": + from distutils.util import strtobool + a = input("The requests module is not available. Should we try to install it automatically using pip? [y/n] ") + while True: + try: + a = strtobool(a) + break + except ValueError: + a = input("Please answer with y or n: ") + if a == 1: + import subprocess + subprocess.call([sys.executable, "-m", "pip", "install", "requests"]) + else: + exit(1) + else: + print("\"requests\" is not available. Install it with pip or your distribution's package manager.") + exit(1) + +import requests from urllib.parse import urlparse, parse_qs, quote, urljoin import pprint @@ -45,8 +70,8 @@ code_url_s = input("> ") code_url = urlparse(code_url_s) query = parse_qs(code_url.query) if "code" not in query or len(query["code"]) == 0 or len(query["code"][0]) == 0: - print("☠️ URL did not contain code parameter") - exit(1) + print("☠️ URL did not contain code parameter") + exit(1) code = query["code"][0] print("🌏 Requesting OAuth Token") @@ -55,38 +80,37 @@ api_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET) body = "grant_type=authorization_code&code={}&redirect_uri=https://remoteplay.dl.playstation.net/remoteplay/redirect&".format(code) token_request = requests.post(TOKEN_URL, - auth = api_auth, - headers = { "Content-Type": "application/x-www-form-urlencoded" }, - data = body.encode("ascii")) + auth = api_auth, + headers = { "Content-Type": "application/x-www-form-urlencoded" }, + data = body.encode("ascii")) print("⚠️ WARNING: From this point on, output might contain sensitive information in some cases!") if token_request.status_code != 200: - print("☠️ Request failed with code {}:\n{}".format(token_request.status_code, token_request.text)) - exit(1) + print("☠️ Request failed with code {}:\n{}".format(token_request.status_code, token_request.text)) + exit(1) token_json = token_request.json() if "access_token" not in token_json: - print("☠️ \"access_token\" is missing in response JSON:\n{}".format(token_request.text)) - exit(1) + print("☠️ \"access_token\" is missing in response JSON:\n{}".format(token_request.text)) + exit(1) token = token_json["access_token"] print("🌏 Requesting Account Info") -account_request = requests.get(TOKEN_URL + "/" + quote(token), - auth = api_auth) +account_request = requests.get(TOKEN_URL + "/" + quote(token), auth = api_auth) if account_request.status_code != 200: - print("☠️ Request failed with code {}:\n{}".format(account_request.status_code, account_request.text)) - exit(1) + print("☠️ Request failed with code {}:\n{}".format(account_request.status_code, account_request.text)) + exit(1) account_info = account_request.json() print("🥦 Received Account Info:") pprint.pprint(account_info) if "user_id" not in account_info: - print("☠️ \"user_id\" is missing in response or not a string") - exit(1) + print("☠️ \"user_id\" is missing in response or not a string") + exit(1) user_id = int(account_info["user_id"]) user_id_base64 = base64.b64encode(user_id.to_bytes(8, "little")).decode() @@ -94,4 +118,5 @@ user_id_base64 = base64.b64encode(user_id.to_bytes(8, "little")).decode() print() print("🍙 This is your AccountID:") print(user_id_base64) +exit(0)