serverboerse/classes/api.py

42 lines
1.2 KiB
Python
Raw Permalink Normal View History

2021-05-02 10:19:17 +00:00
from urllib.request import Request, urlopen
import json
from .server import Server
from .cpu import CPU
from .hdd import HDD
API_URL = "https://www.hetzner.com/a_hz_serverboerse/live_data.json"
class SBAPI:
def __init__(self, endpoint=API_URL, user_agent="SBBot/git (https://kumig.it/kumitterer/serverboerse.git)"):
self.endpoint = endpoint
self.user_agent = user_agent
def fetch(self):
request = Request(self.endpoint, headers={"User-Agent": self.user_agent})
response = urlopen(request)
return json.load(response)
def get_servers(self):
data = self.fetch()
for server in data["server"]:
yield Server(
server["key"],
server["name"],
CPU(
server["cpu"],
server["cpu_benchmark"],
server["cpu_count"]
),
server["ram"],
server["price"],
HDD(
server["hdd_size"],
server["hdd_count"]
),
server["next_reduce_timestamp"],
server["datacenter"][0],
server["freetext"]
)