A login script which also outputs nations which got ejected and which nations failed to login.
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

89 righe
2.9 KiB

  1. #!/usr/bin/python3
  2. # NationStates Login Script
  3. import requests
  4. import time
  5. import xml.etree.cElementTree as ET
  6. import json
  7. # Config
  8. with open('config.json', 'r') as f:
  9. config = json.load(f)
  10. my_nation = config['main-nation']
  11. my_password = config['password']
  12. # End Config
  13. def main():
  14. """Logs into into all nations in the login.txt file, with the format nation;password, one per line."""
  15. user_agent = my_nation
  16. main_password = my_password
  17. ejected_nations = get_ejected_nations()
  18. canonicalized_ejected = [fix(e_nation) for e_nation in ejected_nations]
  19. # open the file with nation and passwords
  20. with open('login.txt', 'r') as f:
  21. for line in f:
  22. # Check if the line is a comment
  23. if line[0] != "#":
  24. # Assuming the password is the usual password
  25. if line.find(';') == -1:
  26. nation = line.strip()
  27. password = main_password
  28. # Otherwise, a unique password is specified
  29. else:
  30. # get nation and password
  31. nation, password = line.strip().split(';')
  32. # make request
  33. login(nation, password, user_agent)
  34. if fix(nation) in canonicalized_ejected:
  35. write_out(nation, 0)
  36. else:
  37. continue
  38. def fix(s):
  39. """Formats a string the NS way, lowercase underscore."""
  40. return s.lower().replace(' ', '_')
  41. def get_ejected_nations():
  42. """"Return all ejected nations."""
  43. time.sleep(0.7)
  44. r = requests.get("https://www.nationstates.net/cgi-bin/api.cgi?region=the_rejected_realms&q=nations",
  45. headers={"User-Agent": f"<Nation: {my_nation}> Login Script"})
  46. root = ET.fromstring(r.text)
  47. return root[0].text.split(':')
  48. def write_out(nation, status):
  49. """Write login failures and ejected nations to out."""
  50. # Ejected Nations
  51. if status == 0:
  52. with open("output/ejected.txt", "a") as f:
  53. f.write('{}\n'.format(nation))
  54. # Failed to login
  55. elif status == 1:
  56. with open("output/failed_login.txt", "a") as f:
  57. f.write('{}\n'.format(nation))
  58. def login(nation, password, agent):
  59. """Given the nation and password, query to the NS API with a ping request."""
  60. # take a break first
  61. rate = 0.7
  62. time.sleep(rate)
  63. # set the user agent to abide by rules, and password to ping properly
  64. r_headers = {'User-Agent': 'login2.py in use by <Nation: {}>'.format(agent),
  65. 'X-Password': password}
  66. r = requests.get('https://www.nationstates.net/cgi-bin/api.cgi?nation={}&q=ping'.format(fix(nation)),
  67. headers=r_headers)
  68. try:
  69. r.raise_for_status()
  70. print("Logged into {}.".format(nation))
  71. except requests.exceptions.HTTPError:
  72. print("Error logging into {}.".format(nation))
  73. write_out(nation, 1)
  74. if __name__ == '__main__':
  75. main()