From 286d89ad3dd0b61c9d2d4e61ab8730a747f96be5 Mon Sep 17 00:00:00 2001 From: LucasJavaudin Date: Mon, 28 Jul 2025 15:53:50 +0200 Subject: [PATCH] Initial commit --- ods.csv | 31 ++++++++++++++++++++ requirements.txt | 3 ++ run.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 ods.csv create mode 100644 requirements.txt create mode 100644 run.py diff --git a/ods.csv b/ods.csv new file mode 100644 index 0000000..452c930 --- /dev/null +++ b/ods.csv @@ -0,0 +1,31 @@ +zone_origin,zone_destination,origin_x,origin_y,destination_x,destination_y +764,541,2.24530477352331,48.8347645451668,2.148600996275,48.7951221273305 +538,863,2.15386620557529,48.8060072829673,2.17181932343479,48.8450994726178 +104,763,2.40352283948468,48.8568662725772,2.24267475441837,48.8436374088146 +69,94,2.29798995583081,48.8328331333799,2.34041045806128,48.8985300765386 +84,322,2.29289082770489,48.877761174813,2.78702580000552,48.8501682092641 +84,1060,2.29289082770489,48.877761174813,2.40124544796948,48.813124151778 +1051,1079,2.33679614132317,48.7552477134484,2.53531897140623,48.785296409886 +1043,99,2.46544270108551,48.7811096806273,2.37741739152331,48.883136354461 +1089,959,2.5172675382388,48.7976520502832,2.50303350809214,48.9012679569104 +1081,1058,2.49893552227025,48.8422718400072,2.38297575764604,48.8141705283258 +131,163,2.61092156730628,48.6911283193282,2.57156207245643,48.6578262435715 +117,168,2.81956687450666,48.8556384232404,2.7827648723145,48.875203794514 +145,195,2.5711297857824,48.3262342138275,2.69636902647986,48.4048331329728 +269,195,2.69222756376317,48.2685639505921,2.69636902647986,48.4048331329728 +312,237,2.80768810571496,49.0532993347927,2.88088671518603,48.9653856246601 +245,270,2.59217655065731,49.0190938958613,2.86161354891991,48.973343967919 +242,254,2.66743336082088,48.5419210945901,2.6791224366718,48.6041248619187 +359,403,2.06782239538375,48.9608293536687,2.09850546417701,48.9981668838852 +364,442,1.85383323300484,48.9759591595761,1.74191912138972,48.9935476543489 +676,438,2.24192912937909,48.7165283047375,2.16634817232331,48.7705992556186 +574,458,2.29779994548061,48.594563798139,2.09709414508562,48.874752964087 +1246,493,2.10286847682099,49.0565790714291,2.04534579973676,48.935437592091 +655,532,2.26159705634266,48.7333075990665,2.21311703742605,48.7813323590488 +131,683,2.61092156730628,48.6911283193282,2.53735831258199,48.6770669040302 +822,756,2.22764878222806,48.7872435190096,2.30561487740419,48.8016856649222 +1282,1158,2.0245231049303,49.0288569472425,2.07523019873142,49.0350566131641 +718,676,2.26727381416908,48.7470611153692,2.24192912937909,48.7165283047375 +505,496,1.93949508967669,48.5713824819609,1.83108492303569,48.6422577894188 +69,811,2.29806065849271,48.8328038037464,2.26417781250754,48.8240833192653 +878,979,2.49855525523787,48.9435428398679,2.35618625519865,48.9190736969263 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e4a1c07 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +pandas==2.3.1 +pytz==2025.2 +requests==2.32.4 diff --git a/run.py b/run.py new file mode 100644 index 0000000..b2234c7 --- /dev/null +++ b/run.py @@ -0,0 +1,74 @@ +import requests +import pandas as pd +import os +from datetime import datetime +import time +import pytz + +OD_FILE = "./ods.csv" + +TOMTOM_OUTPUT_FILE = "./tomtom.csv" +TOMTOM_API_KEY = "TODO" + +def csv(file_path, dataframe): + if not os.path.exists(file_path): + dataframe.to_csv(file_path, index=False, header=True) + else: + dataframe.to_csv(file_path, mode="a", index=False, header=False) + + +def tomtom_requests(): + utc_now = datetime.utcnow().replace(tzinfo=pytz.utc) + paris_tz = pytz.timezone("Europe/Paris") + paris_now = utc_now.astimezone(paris_tz) + if not ( + (6 <= paris_now.hour < 10) + or (16 <= paris_now.hour < 20) + or (paris_now.hour == 10 and paris_now.minute == 0) + or (paris_now.hour == 20 and paris_now.minute == 0) + ): + # Run only the requests between 6 a.m. and 10 a.m. and between 4 p.m. and 8 p.m. + print("Skipping TomTom requests") + return + ods = pd.read_csv(OD_FILE) + data = list() + for _, row in ods.iterrows(): + url1 = "https://api.tomtom.com/routing/1/calculateRoute/" + orig_des = ( + str(row["origin_y"]) + + "," + + str(row["origin_x"]) + + ":" + + str(row["destination_y"]) + + "," + + str(row["destination_x"]) + + "/json?" + ) + apikey = f"key={TOMTOM_API_KEY}" + depart_time = "&departAt=now" + travelmode = "&travelMode=car" + url = url1 + orig_des + apikey + depart_time + travelmode + output = requests.request("GET", url, headers={}) + time.sleep(0.5) + output = output.json() + if "routes" in output.keys(): + duration = output["routes"][0]["summary"]["travelTimeInSeconds"] + distance = output["routes"][0]["summary"]["lengthInMeters"] + else: + duration = 0 + distance = 0 + row = { + "origin": row["zone_origin"], + "destination": row["zone_destination"], + "date": paris_now.strftime("%Y-%m-%d"), + "departure_time": paris_now.strftime("%H:%M"), + "travel_time": time.strftime("%H:%M:%S", time.gmtime(duration)), + "distance": distance, + } + data.append(row) + df = pd.DataFrame(data) + csv(TOMTOM_OUTPUT_FILE, df) + print("Done") + + +tomtom_requests()