Created
August 18, 2024 05:30
-
-
Save shibeta/37d71c075c22dc5e3b26733138ac527b to your computer and use it in GitHub Desktop.
reload all storage on alist, since alist does not support auto reload.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import sys | |
# Configuration | |
BASE_URL = "http://ip:8455" | |
USERNAME = "admin" # to manage storage, admin permission is required | |
PASSWORD = "{{PASSWORD}}" | |
MAX_LOGIN_ATTEMPTS = 5 | |
MAX_RELOAD_ATTEMPTS = 3 | |
def make_request(method, endpoint, json=None, headers=None): | |
try: | |
url = f"{BASE_URL}{endpoint}" | |
response = requests.request(method, url, json=json, headers=headers) | |
return response.json() | |
except requests.RequestException as e: | |
print(f"Network error: {e}") | |
sys.exit(1) | |
def login(): | |
for attempt in range(MAX_LOGIN_ATTEMPTS): | |
data = {"username": USERNAME, "password": PASSWORD} | |
response = make_request("POST", "/api/auth/login", json=data) | |
if response["code"] == 200: | |
return response["data"]["token"] | |
else: | |
print(f"Login failed: {response['message']}") | |
print(f"Failed to login after {MAX_LOGIN_ATTEMPTS} attempts.") | |
sys.exit(1) | |
def reload_storage(token): | |
headers = {"Authorization": token} | |
for attempt in range(MAX_RELOAD_ATTEMPTS): | |
response = make_request("POST", "/api/admin/storage/load_all", headers=headers) | |
if response["code"] == 200: | |
print("All storage has been reloaded.") | |
return | |
else: | |
print(f"Reload failed: {response['message']}") | |
print(f"Failed to reload storage after {MAX_RELOAD_ATTEMPTS} attempts.") | |
sys.exit(1) | |
def main(): | |
token = login() | |
reload_storage(token) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment