Last active
January 19, 2024 17:02
-
-
Save CtrlAltDefeat94/e064c0fb0e6abda3f691e464b5574402 to your computer and use it in GitHub Desktop.
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
from json import JSONDecodeError, load, dumps | |
from requests import get, patch | |
from requests.auth import HTTPBasicAuth | |
from os.path import abspath, dirname, exists | |
from os import chdir | |
BLOCKCONVERSION = 4320 | |
CONFIGFILE = 'HostdManager.json' | |
abspath = abspath(__file__) | |
dname = dirname(abspath) | |
chdir(dname) | |
def fiattohastings(fiat): | |
sc = fiat / SIAPRICE | |
hastings = sc * 10 ** 12 | |
return int(hastings) | |
if not exists(CONFIGFILE): | |
f = open(CONFIGFILE, "w+") | |
f.write("""{ | |
"Hostname": "localhost", | |
"Port": 9980, | |
"Password": "", | |
"StoragePrice": 0.00, | |
"DownloadPrice": 0.00, | |
"UploadPrice": 0.00, | |
"Currency": "eur" | |
}""") | |
f = open(CONFIGFILE) | |
settings = load(f) | |
try: | |
EXCHANGERATES = get("https://api.siacentral.com/v2/market/exchange-rate").json() | |
if not settings['Currency'].lower() in EXCHANGERATES['price'].keys(): | |
print(f"Currency unsupported. Please use any of:\n{EXCHANGERATES['price'].keys()}") | |
exit() | |
SIAPRICE = EXCHANGERATES['price'][settings['Currency'].lower()] | |
except JSONDecodeError: | |
exit(1) | |
# Settings | |
basic = HTTPBasicAuth('', settings['Password']) | |
STORAGEPRICE = fiattohastings(settings['StoragePrice']) | |
DOWNLOADPRICE = fiattohastings(settings['DownloadPrice']) | |
UPLOADPRICE = fiattohastings(settings['UploadPrice']) | |
url = f"http://{settings['Hostname']}:{settings['Port']}/api/settings" | |
payload = dumps({ | |
"storagePrice": str(int(int(STORAGEPRICE) / BLOCKCONVERSION)), | |
"egressPrice": str(int(DOWNLOADPRICE)), | |
"ingressPrice": str(int(UPLOADPRICE)), | |
}) | |
headers = { | |
'Content-Type': 'application/json' | |
} | |
response = patch(url, headers=headers, data=payload, auth=basic) | |
if not response.status_code == 200: | |
if response.status_code == 401: | |
print(f'Please configure password in {CONFIGFILE}') | |
else: | |
print(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment