Add first implementation

This commit is contained in:
Julius Freudenberger 2023-05-15 09:30:56 +02:00
parent 92075e7e46
commit acdcf98896
4 changed files with 86 additions and 0 deletions

16
csv_exporter.py Normal file
View file

@ -0,0 +1,16 @@
from verein import Verein
TABLE_HEADING = "account_name,total_balance,available_balance,redeemed"
def export_vereine(vereine: [Verein], export_filename: str):
export_string: str = TABLE_HEADING + '\n'
for verein in vereine:
export_string += line_for_verein(verein) + '\n'
with open(export_filename, 'w') as export_file:
export_file.writelines(export_string)
def line_for_verein(verein: Verein):
return f'{verein.account_name},{verein.total_balance},{verein.available_balance},{verein.redeemed}'

33
main.py Normal file
View file

@ -0,0 +1,33 @@
import argparse
from csv_exporter import export_vereine
from verein import Verein
from verein_fetcher import get_verein_ids_near_location, get_verein_for_id
def main():
parser = argparse.ArgumentParser(
prog='Rewe Vereinsscheine',
description='Get the amount of Vereinsscheine for Vereine near a location')
parser.add_argument('latitude')
parser.add_argument('longitude')
parser.add_argument('-f', '--export-file', default='vereine.csv', required=False)
args = parser.parse_args()
print("Fetching Vereine")
verein_ids = get_verein_ids_near_location(args.latitude, args.longitude)
print("Getting Verein information")
vereine: [Verein] = []
for verein_id in verein_ids:
try:
vereine.append(get_verein_for_id(verein_id))
except RuntimeError:
pass
print("Exporting Verein information")
export_vereine(vereine, args.export_file)
if __name__ == "__main__":
main()

14
verein.py Normal file
View file

@ -0,0 +1,14 @@
class Verein:
account_name: str
total_balance: int
available_balance: int
redeemed: int
def __init__(self, account_name: str, total_balance: int, available_balance: int, redeemed: int):
self.account_name = account_name
self.total_balance = total_balance
self.available_balance = available_balance
self.redeemed = redeemed
def __str__(self):
return f'{self.account_name}: ({self.available_balance} + {self.redeemed} = {self.total_balance})'

View file

@ -0,0 +1,23 @@
import requests
from verein import Verein
def get_verein_ids_near_location(latitude: float, longitude: float, count: int = 10):
response = requests.get(
f"https://scheinefuervereine.rewe.de/consumer-api/customer.php?action=get_clubs_near_location&count={count}&lat={latitude}&long={longitude}")
if not response.ok:
return []
data = response.json()['data']
return [verein['partyID'] for verein in data]
def get_verein_for_id(verein_id: int) -> Verein:
response = requests.get(
f"https://scheinefuervereine.rewe.de/consumer-api/customer.php?action=get_club&id={verein_id}")
if not response.ok:
raise RuntimeError
data = response.json()['data']
return Verein(data['accountName'], data['totalBalance'], data['availableBalance'], data['redeemed'])