From 3bd35607448f86116b3fcd8b9ee3ac463990c16d Mon Sep 17 00:00:00 2001 From: "hendrik.heine" Date: Mon, 9 May 2022 20:02:05 +0200 Subject: [PATCH] added some more stuff --- README.md | 2 +- apiHandler.py | 38 ++++++++++++++++++++++++-------------- classes.py | 24 ++++++++++++++++++++++++ tankApi.py | 8 +++++++- 4 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 classes.py diff --git a/README.md b/README.md index 10d4ad7..f3f668d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Python Tankstellen Api Wrapper -Dies ist ein API Wrapper für Tankstellen in Deutschland. Als Quelle wird [TankerKönig](https://creativecommons.tankerkoenig.de/) verwendet. +Dies ist ein API Wrapper für Tankstellen in Deutschland. Als Quelle wird [TankerKönig](https://tankerkoenig.de/) verwendet. ## Getting started 1. API Key:
diff --git a/apiHandler.py b/apiHandler.py index e3f8a13..ea2d2cd 100644 --- a/apiHandler.py +++ b/apiHandler.py @@ -1,33 +1,43 @@ +import pgeocode import requests as _requests from secretHandler import secret as _secret -import pgeocode +from classes import * -class city: - def __init__(self, lat, lng, name, postalCode, communityCode) -> None: - self.lat = lat - self.lng = lng - self.name = name - self.postalCode = postalCode - self.communityCode = communityCode class api: def __init__(self, postalCode:int, secret:_secret) -> None: '''postalCode: Die Postleitzahl\nsecret: Das secret Objekt''' - __geo = pgeocode.Nominatim(country="de") + __geo = pgeocode.Nominatim(country="de") # Dies ist ein fester Wert, da die API nur für Deutschland funktioniert location = __geo.query_postal_code(codes=postalCode) self.__city = city(lat=location["latitude"], lng=location["longitude"], name=location["place_name"], postalCode=location["postal_code"], communityCode=location["community_code"]) - self.__apiKey = secret.getApiKey() self.__baseURL = "https://creativecommons.tankerkoenig.de/json" self.__priceURL = f"{self.__baseURL}/prices.php" #Preisabfrage self.__listURL = f"{self.__baseURL}/list.php" # Umkreissuche self.__detailURL = f"{self.__baseURL}/detail.php" # Details zu einer bestimmten Tankstelle + self.__reportURL = f"{self.__baseURL}/complaint.php" # Zum Melden von falschen Angaben + + self.__stations = [] + - + def getGasStations(self, raduisInKM:float=10.0, spritType:str="all", onlyInThisPostCode:bool=False, sortedBy:str="postCode"): + '''Max Radius: 25KM\nSpritTypes: e5, e10, diesel, all\nOnlyInThisPostCode: Zeigt nur Tanketsllen mit der passenden Postleitzahl\nSortedBy: price, postCode, distanz''' + req = f"{self.__listURL}?lat={self.__city.lat}&lng={self.__city.lng}&rad={raduisInKM}&sort=dist&type={spritType}&apikey={self.__apiKey}" + response = _requests.api.get(req).json() + if response["ok"] and response["status"] == "ok": + for station in response["stations"]: + self.__stations.append(gasSation(value=station)) + listForReturn = [] + for station in self.__stations: + station:gasSation + if onlyInThisPostCode: + if station.postCode == self.__city.postalCode: + listForReturn.append(station) + else: + listForReturn.append(station) - def getLatAndLong(self): - print(self.__city.name) + return listForReturn - def getGasStations(self): + def getStaionDeteails(self): pass \ No newline at end of file diff --git a/classes.py b/classes.py new file mode 100644 index 0000000..6c301aa --- /dev/null +++ b/classes.py @@ -0,0 +1,24 @@ +class gasSation: + def __init__(self, value:dict) -> None: + self.id = value["id"] + self.name = value["name"] + self.brand = value["brand"] + self.street = value["street"] + self.houseNumber = value["houseNumber"] + self.postCode = value["postCode"] + self.place = value["place"] + self.lat = value["lat"] + self.lng = value["lng"] + self.dist = value["dist"] + self.diesel = value["diesel"] + self.e5 = value["e5"] + self.e10 = value["e10"] + self.isOpen = value["isOpen"] + +class city: + def __init__(self, lat, lng, name, postalCode, communityCode) -> None: + self.lat = lat + self.lng = lng + self.name = name + self.postalCode = int(postalCode) + self.communityCode = communityCode \ No newline at end of file diff --git a/tankApi.py b/tankApi.py index 97c8c7f..9d2dd23 100644 --- a/tankApi.py +++ b/tankApi.py @@ -1,10 +1,16 @@ import apiHandler import secretHandler as sh +from classes import * secret = sh.secret() value = secret.loadFromSecret() if value == 0: api = apiHandler.api(postalCode=26127, secret=secret) - api.getLatAndLong() + stations = api.getGasStations(onlyInThisPostCode=True, raduisInKM=5, spritType="all") + for station in stations: + station:gasSation + print(station.postCode) + print(station.brand) + print("") else: exit("Error reading secret") \ No newline at end of file