Skip to content

Commit

Permalink
Merge pull request #2 from rootabeta/main
Browse files Browse the repository at this point in the history
Modified misc, polls, and prep to comply with rules change
  • Loading branch information
audreyreal authored Mar 2, 2023
2 parents 8e09db6 + 31d4a9f commit 9dfab43
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
1 change: 1 addition & 0 deletions components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand Down
12 changes: 6 additions & 6 deletions components/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -25,15 +25,15 @@ 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:
return f"Failed to login to {nation}."
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:
Expand All @@ -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:
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions components/polls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
from bs4 import BeautifulSoup


def login(nation, password, headers, poll_id):
def login(nation, password, headers, poll_id, userclick):
params = (
("nation", nation),
("password", password),
("logging_in", "1"),
)

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,
)
Expand All @@ -34,15 +34,15 @@ 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,
}

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,
Expand Down
16 changes: 8 additions & 8 deletions components/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from bs4 import BeautifulSoup


def login(nation, password, headers):
def login(nation, password, headers, userclick):
try:
params = (
("nation", nation),
Expand All @@ -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,
)
Expand All @@ -37,28 +37,28 @@ def login(nation, password, headers):
return (pin, chk)


def apply_wa(pin, chk, headers):
def apply_wa(pin, chk, headers, userclick):
cookies = {
"pin": pin,
}

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,
)
Expand All @@ -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,
}
Expand All @@ -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,
Expand Down
31 changes: 21 additions & 10 deletions main.pyw
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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-"]
Expand All @@ -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!
Expand Down Expand Up @@ -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()
Expand All @@ -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!
Expand Down

0 comments on commit 9dfab43

Please sign in to comment.