Last active
February 6, 2022 21:38
-
-
Save louisguitton/c6c5d76b0c3f986589ef41a3c495e254 to your computer and use it in GitHub Desktop.
Collect air traffic data of french airports from aviation-civile.gouv.fr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unicodedata | |
import html | |
import datetime | |
from dateutil.relativedelta import relativedelta | |
import csv | |
# how to make a GET request in python? -> learn how to use the 'requests' library | |
import requests | |
# how to use an external library to create a progress bar? | |
from tqdm import tqdm | |
url = "https://salledelecture-ext.aviation-civile.gouv.fr/externe/mouvementsDavions/index.php" | |
# how to create a function in python? | |
def clean_name(name: str) -> str: | |
return ( | |
unicodedata.normalize("NFKD", html.unescape(name)).strip().replace("<br/>", " ") | |
) | |
base = datetime.datetime(year=2021, month=12, day=1) | |
months_in_past = 12 * 3 | |
date_list = [base - relativedelta(months=x) for x in range(months_in_past)] | |
csv_data = [] | |
# how to write a for loop in python? | |
for date in tqdm(date_list): | |
# make the GET request using the 'requests' library | |
month = date.strftime("%Y_%m") | |
res = requests.get( | |
url=url, | |
params=dict(a="requeteAJAX", mois=month), | |
headers={"X-Requested-With": "XMLHttpRequest"}, | |
) | |
data = res.json() | |
# how to parse a dictionary in python? | |
airports_of_interest = [ | |
"LFPG", | |
"LFPO", | |
"LFLL", | |
"LFMN", | |
"LFML", | |
"LFBO", | |
"LFSB", | |
"LFBD", | |
] | |
clean_data = [ | |
(date.isoformat(), clean_name(airport_data["nom"]), airport_data["chiffres"]) | |
for airport_id, airport_data in data["airport"].items() | |
if airport_id in airports_of_interest | |
] | |
# append to list of data | |
csv_data += clean_data | |
# how to write data to a CSV file in python? | |
with open("air_traffic_data.csv", "w", newline="") as csvfile: | |
writer = csv.writer( | |
csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL | |
) | |
writer.writerow(["date", "airport_name", "air_traffic_volume"]) | |
writer.writerows(csv_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tqdm | |
requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment