# script to get hourly counters from 2014-present
import glob
import gzip
import os
import shutil

import pandas as pd
import requests


def main():
    for year in range(2014, 2026):
        for month in range(1, 13):
            month = str(month) if len(str(month)) == 2 else f"0{month}"
            year_month = f"{year}{month}"
            url = f"https://mobidata-bw.de/daten/eco-counter/v2/fahrradzaehler_stundenwerten_{year_month}.csv.gz"

            os.makedirs("hourly_archives", exist_ok=True)
            filename = url.split("/")[-1]
            with open(f"hourly_archives/{filename}", "wb") as f:
                r = requests.get(url)
                f.write(r.content)
            os.makedirs("stundenwerte", exist_ok=True)
            with gzip.open(f"hourly_archives/{filename}", "rb") as f_in:
                f_name = "stundenwerte/" + filename.split(".")[0]
                with open(f"{f_name}.csv", "wb") as f_out:
                    shutil.copyfileobj(f_in, f_out)


if __name__ == "__main__":
    main()
