Skip to content

Commit

Permalink
If the api key is invalid, prompt the user to enter a new one. Also, …
Browse files Browse the repository at this point in the history
…always return the latest api key in `api-key.txt`, rather than a random one if there are multiple.
  • Loading branch information
johndoknjas committed Sep 22, 2024
1 parent 00e841a commit ede7d7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
7 changes: 7 additions & 0 deletions hypickle/Files.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,10 @@ def apply_aliases(lst: list[str]) -> list[str]:

def modified_secs_ago(p: Path) -> float:
return time.time() - os.path.getmtime(p)

def get_nth_line(filepath: str, i: int) -> str:
"""Returns the ith line (not including whitespace-only lines). The line returned will have trailing
whitespace removed, if it contains any.
Note that i = 0 would mean the first line, i = -1 would mean the last."""
with open(filepath, 'r') as f:
return [line.rstrip() for line in f if line.strip()][i]
21 changes: 13 additions & 8 deletions hypickle/hypixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

TIME_STARTED: float = time()
num_api_calls_made: int = 0
_api_key: str | None = None

def make_request_url(typeOfRequest: str, uuid_or_ign: str | None) -> str:
assert (typeOfRequest == 'leaderboards') == (uuid_or_ign is None)
Expand Down Expand Up @@ -73,17 +74,21 @@ def getJSON(typeOfRequest: str, uuid_or_ign: Optional[str], specific_api_key: Op
return responseJSON

def _get_api_key() -> str:
""" This function returns one of the key(s) in `api-key.txt`. If the file doesn't exist, it will ask the
user for an api key and write it to the new file. """
"""This function returns the latest api key in `api-key.txt`. If the file doesn't exist, it will ask the
user for an api key and write it to the new file."""
global _api_key
if _api_key is not None:
return _api_key
FILENAME = 'api-key.txt'
if not os.path.isfile(FILENAME) or not Utils.content_in_file(FILENAME):
print("The script needs a hypixel api key. To get one, you can follow the first part of this guide: https://gist.github.com/camnwalter/c0156c68b1e2a21ec0b084c6f04b63f0#how-to-get-a-new-api-key-after-the-hypixel-api-changes")
if (not os.path.isfile(FILENAME) or not Utils.content_in_file(FILENAME) or
not _validate_api_key(Files.get_nth_line(FILENAME, -1))):
print("The script needs a hypixel api key. You can get one from hypixel here: ", end='')
print("https://developer.hypixel.net/dashboard")
while not _validate_api_key(api_key := input("Please enter your api key: ").strip()):
print('Sorry, that api key is invalid.')
with open(FILENAME, 'w') as file:
file.write(api_key)
with open(FILENAME) as file:
return choice([x for line in file if (x := line.strip())])
with open(FILENAME, 'a') as file:
file.write(f'\n{api_key}')
return (_api_key := Files.get_nth_line(FILENAME, -1))

def _validate_api_key(key: str) -> bool:
try:
Expand Down

0 comments on commit ede7d7c

Please sign in to comment.