Skip to content

Commit

Permalink
unify quotation marks
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrtye committed Feb 6, 2024
1 parent fbdd57f commit 75c62fc
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions apiserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
stock_status = None

xui_session = requests.Session()
tickers = yfinance.Tickers('^IXIC ^GSPC 000001.SS ^HSI')
tickers = yfinance.Tickers("^IXIC ^GSPC 000001.SS ^HSI")


class apiHandler(http.server.BaseHTTPRequestHandler):
def log_message(self, format, *args):
Expand All @@ -51,9 +52,9 @@ def do_GET(self):
global stock_status
response = json.dumps(stock_status)
else:
message = {'message': 'Not Found'}
message = {"message": "Not Found"}
response = json.dumps(message)

self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
Expand All @@ -68,83 +69,86 @@ def start_api_server():

def formatBytes(bytes):
if bytes == 0:
return '0 Byte'
return "0 Byte"

k = 1024
sizes = ['Byte', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
sizes = ["Byte", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]

i = math.floor(math.log(bytes) / math.log(k))

return f'{bytes / math.pow(k, i):.2f}'.rstrip('0').rstrip('.') + ' ' + sizes[i]
return f"{bytes / math.pow(k, i):.2f}".rstrip("0").rstrip(".") + " " + sizes[i]


def bytesToSpeed(bytes):
return formatBytes(bytes) + '/s'
return formatBytes(bytes) + "/s"


def update_xui_status():
global xui_status
xui_status = {
'up': 0,
'down': 0,
'sent': 0,
'recv': 0,
"up": 0,
"down": 0,
"sent": 0,
"recv": 0,
}

response = xui_session.post(XUI_STATUS_URL)
if response.status_code == 404:
xui_session.post(XUI_LOGIN_URL, data={"username": XUI_USERNAME, "password": XUI_PASSWORD})
xui_session.post(
XUI_LOGIN_URL, data={"username": XUI_USERNAME, "password": XUI_PASSWORD}
)
response = xui_session.post(XUI_STATUS_URL)

if response.status_code == 200:
response = response.json()
xui_status['up'] = bytesToSpeed(response['obj']['netIO']['up'])
xui_status['down'] = bytesToSpeed(response['obj']['netIO']['down'])
xui_status['sent'] = formatBytes(response['obj']['netTraffic']['sent'])
xui_status['recv'] = formatBytes(response['obj']['netTraffic']['recv'])
xui_status["up"] = bytesToSpeed(response["obj"]["netIO"]["up"])
xui_status["down"] = bytesToSpeed(response["obj"]["netIO"]["down"])
xui_status["sent"] = formatBytes(response["obj"]["netTraffic"]["sent"])
xui_status["recv"] = formatBytes(response["obj"]["netTraffic"]["recv"])


def convert_exchange_rate(rate):
if rate == 0:
return '0'
return f'{1 / rate:.2f}'.rstrip('0').rstrip('.')
return "0"

return f"{1 / rate:.2f}".rstrip("0").rstrip(".")


def update_exchange_status():
global exchange_status

response = requests.get(EXCHANGE_RATE_URL)

if response.status_code == 200:
response = response.json()
exchange_status = {
'GBP': convert_exchange_rate(response['rates']['GBP']),
'EUR': convert_exchange_rate(response['rates']['EUR']),
'USD': convert_exchange_rate(response['rates']['USD']),
'CAD': convert_exchange_rate(response['rates']['CAD']),
"GBP": convert_exchange_rate(response["rates"]["GBP"]),
"EUR": convert_exchange_rate(response["rates"]["EUR"]),
"USD": convert_exchange_rate(response["rates"]["USD"]),
"CAD": convert_exchange_rate(response["rates"]["CAD"]),
}


def convert_stock_price(price):
return f'{price:.2f}'.rstrip('0').rstrip('.')
return f"{price:.2f}".rstrip("0").rstrip(".")


def update_stock_status():
global stock_status
IXIC = tickers.tickers['^IXIC'].history(period="1d", interval="60m")
GSPC = tickers.tickers['^GSPC'].history(period="1d", interval="60m")
SS = tickers.tickers['000001.SS'].history(period="1d", interval="60m")
HSI = tickers.tickers['^HSI'].history(period="1d", interval="60m")

IXIC = tickers.tickers["^IXIC"].history(period="1d", interval="60m")
GSPC = tickers.tickers["^GSPC"].history(period="1d", interval="60m")
SS = tickers.tickers["000001.SS"].history(period="1d", interval="60m")
HSI = tickers.tickers["^HSI"].history(period="1d", interval="60m")

stock_status = {
'IXIC': convert_stock_price(IXIC['Close'][IXIC['Close'].keys().max()]),
'GSPC': convert_stock_price(GSPC['Close'][GSPC['Close'].keys().max()]),
'SS': convert_stock_price(SS['Close'][SS['Close'].keys().max()]),
'HSI': convert_stock_price(HSI['Close'][HSI['Close'].keys().max()]),
"IXIC": convert_stock_price(IXIC["Close"][IXIC["Close"].keys().max()]),
"GSPC": convert_stock_price(GSPC["Close"][GSPC["Close"].keys().max()]),
"SS": convert_stock_price(SS["Close"][SS["Close"].keys().max()]),
"HSI": convert_stock_price(HSI["Close"][HSI["Close"].keys().max()]),
}


if __name__ == "__main__":
api_thread = threading.Thread(target=start_api_server)
api_thread.daemon = True
Expand Down

0 comments on commit 75c62fc

Please sign in to comment.