diff --git a/csv_exporter.py b/csv_exporter.py new file mode 100644 index 0000000..dab9a77 --- /dev/null +++ b/csv_exporter.py @@ -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}' diff --git a/main.py b/main.py new file mode 100644 index 0000000..89e41af --- /dev/null +++ b/main.py @@ -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() diff --git a/verein.py b/verein.py new file mode 100644 index 0000000..a2e9cba --- /dev/null +++ b/verein.py @@ -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})' diff --git a/verein_fetcher.py b/verein_fetcher.py index e69de29..a05d2ff 100644 --- a/verein_fetcher.py +++ b/verein_fetcher.py @@ -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'])