From 01c661c9aa2616f07d231e93970288a005e93d21 Mon Sep 17 00:00:00 2001 From: johndoknjas Date: Sun, 24 Mar 2024 23:26:08 -0700 Subject: [PATCH] If the `api-key.txt` file does not exist yet, ask the user to enter an api key. --- README.md | 6 ++++-- hypickle/hypixel.py | 12 ++++++++++-- ideas.txt | 2 ++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9bffed1..f495a5e 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,9 @@ Besides installing the script, this will also automatically install its dependen Then, create a new folder that will be used to store persistent information (such as friends lists you create). In this folder, make a textfile called 'api-key.txt', and paste your hypixel api key as the first line. - - To get this api key, make a developer account with hypixel: https://developer.hypixel.net/ + - To get this api key, make a [hypixel developer account](https://developer.hypixel.net/). + The first part of [this](https://gist.github.com/camnwalter/c0156c68b1e2a21ec0b084c6f04b63f0#how-to-get-a-new-api-key-after-the-hypixel-api-changes) + guide is helpful to do this. After setting this up, you will be able to open a terminal window in this directory and run various commands with the `hypickle` script (explained below). @@ -46,4 +48,4 @@ Open the root directory of the project in the terminal, and then: ### Acknowledgements: -The `hypixel.py` and `leveling.py` files were originally from https://github.com/Snuggle/hypixel.py/. Since then I have made a number of modifications to them. \ No newline at end of file +The `hypixel.py` and `leveling.py` files were originally from [Snuggle's repo](https://github.com/Snuggle/hypixel.py/). Since then I have made a number of modifications to them. \ No newline at end of file diff --git a/hypickle/hypixel.py b/hypickle/hypixel.py index 02249ae..fe836e4 100644 --- a/hypickle/hypixel.py +++ b/hypickle/hypixel.py @@ -9,6 +9,7 @@ from typing import List, Optional, Tuple import re from copy import deepcopy +import os.path import requests import urllib3 @@ -20,8 +21,15 @@ from .Rank import Rank def _get_api_keys_from_file() -> List[str]: - """ This function gets the api key(s) from `api-key.txt`, and returns them in a list. """ - with open('api-key.txt') as file: + """ This function gets the api key(s) from `api-key.txt`, and returns them in a list. If the file doesn't + exist, it will ask the user for an api key and write it to the new file.""" + FILENAME = 'api-key.txt' + if not os.path.isfile(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") + api_key = input("Please enter your api key: ") + with open(FILENAME, 'w') as file: + file.write(api_key) + with open(FILENAME) as file: return [line.rstrip() for line in file] API_KEYS: List[str] = _get_api_keys_from_file() diff --git a/ideas.txt b/ideas.txt index 24e72b5..97b2c2c 100644 --- a/ideas.txt +++ b/ideas.txt @@ -6,6 +6,8 @@ # Consider refactoring to not need the HEX.UNKNOWN hacky stuff used in hypixel.py. + # Check whether the api key is valid, when the user enters it if it doesn't exist yet in `hypixel.py`. + # Test the program when there are no `aliases.txt` or `uuids.txt` files. Program should handle it, not raise an error.