diff --git a/components/__init__.py b/components/__init__.py index c6a7e5f..f5abadc 100644 --- a/components/__init__.py +++ b/components/__init__.py @@ -11,6 +11,7 @@ # shamelessly stolen from https://stackoverflow.com/questions/1057431/how-to-load-all-modules-in-a-folder from os.path import dirname, basename, isfile, join import glob +import time modules = glob.glob(join(dirname(__file__), "*.py")) __all__ = [ diff --git a/components/misc.py b/components/misc.py index 5f0ccf7..26e9f7b 100644 --- a/components/misc.py +++ b/components/misc.py @@ -12,7 +12,7 @@ import requests -def login(nation: str, password: str, headers: dict) -> str: +def login(nation: str, password: str, headers: dict, userclick: int) -> str: """Logs into a nation via the API Args: @@ -25,7 +25,7 @@ def login(nation: str, password: str, headers: dict) -> str: """ headers["X-Password"] = password - url = f"https://www.nationstates.net/cgi-bin/api.cgi?nation={nation}&q=ping" + url = f"https://www.nationstates.net/cgi-bin/api.cgi?nation={nation}&q=ping&userclick={userclick}" try: requests.get(url, headers=headers).raise_for_status() except: @@ -33,7 +33,7 @@ def login(nation: str, password: str, headers: dict) -> str: return f"Successfully logged in to {nation}." -def login_loop(nation_dictionary: dict, headers: dict, window) -> str: +def login_loop(nation_dictionary: dict, headers: dict, window, userclick: int) -> str: """Logs into all of your nations via the API Args: @@ -45,13 +45,13 @@ def login_loop(nation_dictionary: dict, headers: dict, window) -> str: str: When you're done logging into all the nations. """ for nation, password in nation_dictionary.items(): - status = login(nation, password, headers) + status = login(nation, password, headers, userclick) # Pass-through userclick window["-MISCOUT-"].update(status) time.sleep(0.6) return "Done logging into nations" # End of login section -def find_wa(nations: list, headers: dict) -> str or None: +def find_wa(nations: list, headers: dict, userclick: int) -> str or None: """Finds the WA nation in a list of nations. Args: @@ -61,7 +61,7 @@ def find_wa(nations: list, headers: dict) -> str or None: Returns: str or None: str is the WA nation if it's found, None if it isn't. """ - url = "https://www.nationstates.net/cgi-bin/api.cgi?wa=1&q=members" + url = f"https://www.nationstates.net/cgi-bin/api.cgi?wa=1&q=members&userclick={userclick}" wa_list = requests.get(url, headers=headers).text for nation in nations: nation = nation.replace(" ", "_").lower() diff --git a/components/polls.py b/components/polls.py index facf9a4..a579dc6 100644 --- a/components/polls.py +++ b/components/polls.py @@ -12,7 +12,7 @@ from bs4 import BeautifulSoup -def login(nation, password, headers, poll_id): +def login(nation, password, headers, poll_id, userclick): params = ( ("nation", nation), ("password", password), @@ -20,7 +20,7 @@ def login(nation, password, headers, poll_id): ) response = requests.get( - f"https://www.nationstates.net/template-overall=none/page=poll/p={poll_id}", + f"https://www.nationstates.net/template-overall=none/page=poll/p={poll_id}&userclick={userclick}", headers=headers, params=params, ) @@ -34,7 +34,7 @@ def login(nation, password, headers, poll_id): return (pin, chk) -def vote(pin, chk, poll_id, choice, headers): +def vote(pin, chk, poll_id, choice, headers, userclick): cookies = { "pin": pin, } @@ -42,7 +42,7 @@ def vote(pin, chk, poll_id, choice, headers): data = {"pollid": poll_id, "chk": chk, "q1": choice, "poll_submit": "1"} requests.post( - "https://www.nationstates.net/template-overall=none/page=poll/p=181445", + f"https://www.nationstates.net/template-overall=none/page=poll/p=181445&userclick={userclick}", headers=headers, cookies=cookies, data=data, diff --git a/components/prep.py b/components/prep.py index f26f160..fcc5068 100644 --- a/components/prep.py +++ b/components/prep.py @@ -12,7 +12,7 @@ from bs4 import BeautifulSoup -def login(nation, password, headers): +def login(nation, password, headers, userclick): try: params = ( ("nation", nation), @@ -23,7 +23,7 @@ def login(nation, password, headers): return "Out of nations!" response = requests.get( - "https://www.nationstates.net/template-overall=none/page=un/", + f"https://www.nationstates.net/template-overall=none/page=un/?userclick={userclick}", headers=headers, params=params, ) @@ -37,7 +37,7 @@ def login(nation, password, headers): return (pin, chk) -def apply_wa(pin, chk, headers): +def apply_wa(pin, chk, headers, userclick): cookies = { "pin": pin, } @@ -45,20 +45,20 @@ def apply_wa(pin, chk, headers): data = {"action": "join_UN", "chk": chk, "submit": "1"} requests.post( - "https://www.nationstates.net/template-overall=none/page=UN_status", + f"https://www.nationstates.net/template-overall=none/page=UN_status?userclick={userclick}", headers=headers, cookies=cookies, data=data, ) -def get_local_id(pin, headers): +def get_local_id(pin, headers, userclick): cookies = { "pin": pin, } response = requests.get( - "https://www.nationstates.net/template-overall=none/page=settings", + f"https://www.nationstates.net/template-overall=none/page=settings?userclick={userclick}", headers=headers, cookies=cookies, ) @@ -67,7 +67,7 @@ def get_local_id(pin, headers): return soup.find("input", {"name": "localid"}).attrs["value"] -def move_to_jp(jp, pin, local_id, headers): +def move_to_jp(jp, pin, local_id, headers, userclick): cookies = { "pin": pin, } @@ -79,7 +79,7 @@ def move_to_jp(jp, pin, local_id, headers): } requests.post( - "https://www.nationstates.net/template-overall=none/page=change_region", + f"https://www.nationstates.net/template-overall=none/page=change_region?userclick={userclick}", headers=headers, cookies=cookies, data=data, diff --git a/main.pyw b/main.pyw index ee3f2c0..6bcf16c 100644 --- a/main.pyw +++ b/main.pyw @@ -16,8 +16,15 @@ from components import ( polls, prep, ) +from time import time -VERSION = "1.1.1" # VERY IMPORTANT TO CHANGE EVERY UPDATE! +#Calculated every time we click the Big Red Button, passed to functions to include with requests in compliance with 1 Mar 2023 rules change +def userclick(): + timestamp = int(time() * 1000) #Unix time in millis + print(f"DEBUG: {timestamp}") + return timestamp + +VERSION = "1.1.2" # VERY IMPORTANT TO CHANGE EVERY UPDATE! def gui(): @@ -166,15 +173,17 @@ def misc_thread(nation_dict, window): return case "Login to Nations": + timestamp = userclick() disable_misc_buttons(window) window.perform_long_operation( - lambda: misc.login_loop(nation_dict, headers, window), + lambda: misc.login_loop(nation_dict, headers, window, timestamp), "-DONE LOGGING IN-", ) case "Find my WA": + timestamp = userclick() disable_misc_buttons(window) window.perform_long_operation( - lambda: misc.find_wa(nation_dict.keys(), headers), + lambda: misc.find_wa(nation_dict.keys(), headers, timestamp), "-DONE FINDING WA-", ) # respond to threads @@ -196,7 +205,6 @@ def tagging_thread(nation_dict, window): while True: event, values = window.read() - def polls_thread(nation_dict, nations, window, nation_index): while True: event, values = window.read() @@ -205,6 +213,7 @@ def polls_thread(nation_dict, nations, window, nation_index): break if event == "-POLLACTION-": # did u click the button to do the things + timestamp = userclick() #Get current unix timestamp for the current click main_nation = values["-POLLMAIN-"] poll_id = values["-POLL-"] choice = values["-POLLOPTION-"] @@ -222,17 +231,18 @@ def polls_thread(nation_dict, nations, window, nation_index): headers = { "User-Agent": f"Swarm (puppet manager) v{VERSION} devved by nation=sweeze in use by nation={main_nation}", } + match current_action: # lets go python 3.10 i love switch statements case "Login": window.perform_long_operation( lambda: polls.login( - current_nation, current_password, headers, poll_id + current_nation, current_password, headers, poll_id, timestamp ), "-LOGIN DONE-", ) case "Vote": window.perform_long_operation( - lambda: polls.vote(pin, chk, poll_id, choice, headers), + lambda: polls.vote(pin, chk, poll_id, choice, headers, timestamp), "-VOTE-", ) # respond to threads! @@ -275,6 +285,7 @@ def prep_thread(nation_dict, nations, window, nation_index): break if event == "-ACTION-": # did u click the button to do the things + timestamp = userclick() main_nation = values["-MAIN-"] jp = values["-JP-"] current_action = window["-ACTION-"].get_text() @@ -295,21 +306,21 @@ def prep_thread(nation_dict, nations, window, nation_index): case "Login": window.perform_long_operation( lambda: prep.login( - current_nation, current_password, headers + current_nation, current_password, headers, timestamp ), "-LOGIN DONE-", ) case "Apply WA": window.perform_long_operation( - lambda: prep.apply_wa(pin, chk, headers), "-WA DONE-" + lambda: prep.apply_wa(pin, chk, headers, timestamp), "-WA DONE-" ) case "Get Local ID": window.perform_long_operation( - lambda: prep.get_local_id(pin, headers), "-LOCALID DONE-" + lambda: prep.get_local_id(pin, headers, timestamp), "-LOCALID DONE-" ) case "Move to JP": window.perform_long_operation( - lambda: prep.move_to_jp(jp, pin, local_id, headers), + lambda: prep.move_to_jp(jp, pin, local_id, headers, timestamp), "-MOVED TO JP-", ) # respond to threads!