"""
converts a folder of telegram log files into a csv of router-to-router records with the following format
date, test, mbps
"""
import os
import datetime
import csv
import re
data_path = "/home/notplants/computer/projects/janastu-mesh/cowmesh-network-test/iruway-data/ChatExport_2023-06-16/files"

csv_output_path = "/home/notplants/computer/projects/janastu-mesh/cowmesh-network-test/iruway-data/results.csv"

with open(csv_output_path, 'w', newline='') as csvfile:
    output_writer = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    for file in os.listdir(data_path):
        if "router" in file:
            path = os.path.join(data_path, file)
            with open(path, "r") as f:
                match_date = re.match("moonlight-(\d+)-(\d+)-2023.*", file)
                if match_date:
                    date = datetime.date(month=int(match_date.group(1)), day=int(match_date.group(2)), year=2023)
                    contents = f.readlines()
                    for line in contents:
                        # jaaga -> redcottage: 44.8 mbps
                        match_line = re.match("(\S+) -> (\S+): (\S+) mbps", line)
                        if match_line:
                            a = match_line.group(1)
                            b = match_line.group(2)
                            test = "{}->{}".format(a, b)
                            speed = match_line.group(3)
                            if speed != "None":
                                output_writer.writerow([test, date, speed])