def update(self): try: Log("Iksm Session regenarating") self.iksm_session = iksm.get_cookie(self.session_token) self.output() except: raise ValueError("Invalid session_token")
def setConfig(self): session_token = iksm.log_in(VERSION) iksm_session = iksm.get_cookie(session_token, LANG, VERSION) if FLAG_SALMON_STATS: webbrowser.open(URL) print(datetime.now().strftime("%H:%M:%S ") + "Login and Paste API token.") while True: # Waiting Input session_token & api-token try: token = input("") if len(token) == 64: # Simple Validation of api-token length try: int(token, 16) # Convert to Hex print(datetime.now().strftime("%H:%M:%S ") + "Valid token.") api_token = token break except ValueError: print(datetime.now().strftime("%H:%M:%S ") + "Paste API token again.") else: print(datetime.now().strftime("%H:%M:%S ") + "Paste API token again.") except KeyboardInterrupt: print("\nBye!") sys.exit(1) else: api_token = "none" self.param.setup(iksm_session, session_token, api_token)
def gen_new_cookie(reason): '''Attempts to generate new cookie in case provided one is invalid.''' if reason == "blank": print "Blank cookie. Trying to generate one given your session_token..." elif reason == "auth": # authentication error print "Bad cookie. Trying to generate a new one given your session_token..." else: # server error or player hasn't battled before print "Cannot access SplatNet 2 without having played at least one battle online." exit(1) if SESSION_TOKEN == "": print "session_token is blank. Please log in to your Nintendo Account to obtain your session_token." new_token = iksm.log_in() if new_token == None: print "There was a problem logging you in. Please try again later." else: config_data["session_token"] = new_token write_config(config_data) print "\nWrote session_token to config.txt." new_cookie = iksm.get_cookie( SESSION_TOKEN, USER_LANG) # error handling in get_cookie() config_data["cookie"] = new_cookie write_config(config_data) print "Wrote iksm_session cookie to config.txt.\nYour cookie: " + new_cookie
def call_splatoon_api(path, user): url = f'https://app.splatoon2.nintendo.net{path}' cookie = user['cookie'] req = requests.get(url, cookies={'iksm_session': cookie}) if 'code' in req.json().keys(): # cookie expired, get a new one nickname, new_cookie = iksm.get_cookie(user['session_token']) iksm.update_users({user['uid']: {'cookie': new_cookie}}) req = requests.get(url, cookies={'iksm_session': new_cookie}) return req.json()
def login(self): Log("Log in, right click the \"Select this account\" button, copy the link address, and paste it below:" ) webbrowser.open(iksm.log_in()) while True: try: url_scheme = input("") session_token_code = re.search("de=(.*)&", url_scheme).group(1) self.session_token = iksm.get_session_token(session_token_code) self.iksm_session = iksm.get_cookie(self.session_token) Log("Success") break except KeyboardInterrupt: CLog("Keyboard Interrupt") sys.exit(1) except AttributeError: CLog("Invalid URL") except KeyError: CLog("Invalid URL") except ValueError as error: CLog(f"{error}") sys.exit(1) webbrowser.open(URL) Log("Login and Paste API token") while True: try: api_token = input("") if len(api_token) == 64: try: int(api_token, 16) self.api_token = api_token Log("Success") break except ValueError: Log("Paste API token again") else: Log("Paste API token again") except KeyboardInterrupt: Log("Bye Bye") sys.exit(1) except Exception as error: Log(f"{error}") sys.exit(1) self.output() # 設定ファイル書き込み
def gen_new_cookie(reason): '''Attempts to generate a new cookie in case the provided one is invalid.''' manual = False if reason == "blank": print("Blank cookie.") elif reason == "auth": # authentication error print("The stored cookie has expired.") else: # server error or player hasn't battled before print("Cannot access SplatNet 2 without having played at least one battle online.") sys.exit(1) if SESSION_TOKEN == "": print("session_token is blank. Please log in to your Nintendo Account to obtain your session_token.") new_token = iksm.log_in(A_VERSION) if new_token == None: print("There was a problem logging you in. Please try again later.") else: if new_token == "skip": # user has opted to manually enter cookie manual = True print("\nYou have opted against automatic cookie generation and must manually input your iksm_session cookie.\n") else: print("\nWrote session_token to config.txt.") config_data["session_token"] = new_token write_config(config_data) elif SESSION_TOKEN == "skip": manual = True print("\nYou have opted against automatic cookie generation and must manually input your iksm_session cookie. You may clear this setting by removing \"skip\" from the session_token field in config.txt.\n") if manual: new_cookie = iksm.enter_cookie() else: print("Attempting to generate new cookie...") acc_name, new_cookie = iksm.get_cookie(SESSION_TOKEN, USER_LANG, A_VERSION) config_data["cookie"] = new_cookie write_config(config_data) if manual: print("Wrote iksm_session cookie to config.txt.") else: print("Wrote iksm_session cookie for {} to config.txt.".format(acc_name))
def gen_new_cookie(reason): '''Attempts to generate new cookie in case provided one is invalid.''' manual = False if reason == "blank": print("Blank cookie.") elif reason == "auth": #authentication error print("The stored cookie has expired.") else: #server error or player hasn't battled before print("Cannnot access SplatNet 2 without having played at least one") exit(1) if SESSION_TOKEN == "": print("session_token is blank.Please log in to your Nintendo Account") new_token = iksm.log_in(A_VERSION) if new_token == None: print("There was a problem logging you in. Please try again later") else: if new_token == "skip": #user has opted to manually enter cookie manual = True print("\nYou have opted against automatic cookie generation\n") else: print("\nWrote session_token to config.txt.") config_data["session_token"] = new_token write_config(config_data) elif SESSION_TOKEN == "skip": manual = True print("\nYOu have opted against automatic") if manual: new_cookie = iksm.enter_cookie() else: new_cookie = iksm.get_cookie(SESSION_TOKEN, USER_LANG, A_VERSION) config_data["cookie"] = new_cookie write_config(config_data) print("Wrote iksm_session cookie to config.txt.")
def __init__(self): print(datetime.now().strftime("%H:%M:%S ") + "Salmonia version " + VERSION) print(datetime.now().strftime("%H:%M:%S ") + "Thanks @Yukinkling and @barley_ural!") # os.path.dirname(path) # パス文字列からフォルダ名(ディレクトリ名)を取得する # ./dir/subdir/file.txt -> ./dir/subdir # # os.path.abspath(path) # 相対パス文字列から絶対パス文字列を取得 # ./dir/subdir/file.txt -> C:\Uses\gungee\Documents\dir\subdir\file.txt # # sys.argv[0] # スクリプトのパス文字列 # # config.jsonのパスを決定する path = os.path.dirname(os.path.abspath(sys.argv[0])) + "/config.json" self.param = Param() # 既存のconfig.jsonがあったら try: with open(path) as f: # Exist # 正常にロードできたら try: # json.load(file) # ファイルオブジェクトをJSONとして解釈して辞書を取得 df = json.load(f) # setup self.param.setup(df["iksm_session"], df["session_token"], df["api-token"], df["job_id"]["salmonstats"], df["api_errors"]) # 正常にロードできなかったら except json.decoder.JSONDecodeError: print(datetime.now().strftime("%H:%M:%S ") + "config.json is broken.") print(datetime.now().strftime("%H:%M:%S ") + "Regenerate config.json.") # 既存のconfig.jsonがなかったら except FileNotFoundError: # None print(datetime.now().strftime("%H:%M:%S ") + "config.json is not found.") # config.jsonのテンプレートを書き出す self.param.setup() # setConfig self.setConfig() # このスクリプトと同階層にあるファイルとフォルダのリストを取得する dir = os.listdir() # Directory Checking # jsonフォルダが存在しないならば if "json" not in dir: print(datetime.now().strftime("%H:%M:%S ") + "Make directory...") # 作る os.mkdir("json") # jsonフォルダが存在するならば else: # jsonフォルダ内にあるファイルのリストを取得する file = [] dir = os.listdir("json") for p in dir: # 後ろの5文字".json"を除いたファイル名をfileに入れていく file.append(p[0:-5]) # 大きい整数から小さい整数に向けて並び替え file.sort(key=int, reverse=True) # その先頭の要素をlocalにセット try: self.param.local = int(file[0]) # 要素がなければlocalは0 except IndexError: self.param.local = 0 # iksm_sessionが有効かどうかを確かめる url = "https://app.splatoon2.nintendo.net" print(datetime.now().strftime("%H:%M:%S ") + "Checking iksm_session's validation.") res = requests.get(url, cookies=dict(iksm_session=self.param.iksm_session)) # 200: リクエストが成功 if res.status_code == 200: print(datetime.now().strftime("%H:%M:%S ") + "Your iksm_session is valid.") # 403: リクエストが失敗 if res.status_code == 403: # 認証エラーならば if res.text == "Forbidden": print(datetime.now().strftime("%H:%M:%S ") + "Your iksm_session is expired.") if self.param.session_token != "": # iksm_sessionの再生成を行う print(datetime.now().strftime("%H:%M:%S ") + "Regenerate iksm_session.") try: self.param.iksm_session = iksm.get_cookie(self.param.session_token, LANG, VERSION) self.param.api_errors = 0 print(datetime.now().strftime("%H:%M:%S ") + "Done.") except: self.param.api_errors += 1 self.param.output() input('iksm_sessionの再生成に失敗しました。終了するにはエンターキーを押してください。'); sys.exit(1) # それ以外ならば else: print(datetime.now().strftime("%H:%M:%S ") + "Unknown error.") message = datetime.now().strftime("%H:%M:%S Unknown error.\n") self.writeLog(message) input('不明なエラーが発生しました。終了するにはエンターキーを押してください。'); sys.exit(1) url = "https://app.splatoon2.nintendo.net/api/coop_results" print(datetime.now().strftime("%H:%M:%S ") + "Getting latest job id from SplatNet2.") res = requests.get(url, cookies=dict(iksm_session=self.param.iksm_session)).json() print(datetime.now().strftime("%H:%M:%S ") + str(res["summary"]["card"]["job_num"]) + '.') self.param.splatnet2 = int(res["summary"]["card"]["job_num"]) self.param.output()
def __init__(self): # Initialize print(datetime.now().strftime("%H:%M:%S ") + "Salmonia version " + VERSION) print(datetime.now().strftime("%H:%M:%S ") + "Thanks @Yukinkling and @barley_ural!") path = os.path.dirname(os.path.abspath(sys.argv[0])) + "/config.json" self.param = Param() # Setup Parameters try: with open(path) as f: # Exist try: df = json.load(f) self.param.setup(df["iksm_session"], df["session_token"], df["api-token"], df["job_id"]["salmonstats"], df["api_errors"]) except json.decoder.JSONDecodeError: print(datetime.now().strftime("%H:%M:%S ") + "config.json is broken.") print(datetime.now().strftime("%H:%M:%S ") + "Regenerate config.json.") except FileNotFoundError: # None print(datetime.now().strftime("%H:%M:%S ") + "config.json is not found.") self.param.setup() # Generate temporary config.json self.setConfig() dir = os.listdir() # Directory Checking if "json" not in dir: print(datetime.now().strftime("%H:%M:%S ") + "Make directory...") os.mkdir("json") else: file = [] dir = os.listdir("json") for p in dir: file.append(p[0:-5]) file.sort(key=int, reverse=True) try: self.param.local = int(file[0]) # Latest Job_Id on Local except IndexError: self.param.local = 0 # Checking iksm_session Validation url = "https://app.splatoon2.nintendo.net" print(datetime.now().strftime("%H:%M:%S ") + "Checking iksm_session's validation.") res = requests.get(url, cookies=dict(iksm_session=self.param.iksm_session)) if res.status_code == 200: print(datetime.now().strftime("%H:%M:%S ") + "Your iksm_session is valid.") if res.status_code == 403: if res.text == "Forbidden": print(datetime.now().strftime("%H:%M:%S ") + "Your iksm_session is expired.") if self.param.session_token != "": print(datetime.now().strftime("%H:%M:%S ") + "Regenerate iksm_session.") # Regenerate iksm_session with session_token try: self.param.iksm_session = iksm.get_cookie( self.param.session_token, LANG, VERSION) self.param.api_errors = 0 print(datetime.now().strftime("%H:%M:%S ") + "Done.") except: self.param.api_errors += 1 self.param.output() sys.exit(1) else: print(datetime.now().strftime("%H:%M:%S ") + "Unknown error.") message = datetime.now().strftime("%H:%M:%S Unknown error.\n") self.writeLog(message) sys.exit(1) url = "https://app.splatoon2.nintendo.net/api/coop_results" # print(datetime.now().strftime("%H:%M:%S ") + "Getting latest job id from SplatNet2.") res = requests.get( url, cookies=dict(iksm_session=self.param.iksm_session)).json() self.param.splatnet2 = int(res["summary"]["card"]["job_num"]) self.param.output()