1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| procedure WeatherAPI(City: String);
var
Http: THTTPClient;
Resp: IHTTPResponse;
JsonRoot, JsonCW, JsonHourly: TJSONObject;
Results, Times, HumArr, PresArr, CloudArr: TJSONArray;
Lat, Lon: Double;
FS: TFormatSettings;
Url, CurrTime: string;
Index: Integer;
Humidity, Pressure, CloudCover, WindSpeed: Double;
WindDir, WCode: Integer;
Msg: TStringList;
begin
Http := THTTPClient.Create;
try
// --- Géocodage : obtenir latitude et longitude depuis le nom de la ville ---
Resp := Http.Get(
'https://geocoding-api.open-meteo.com/v1/search?name='
+ TNetEncoding.Url.Encode(City) + '&count=1');
if Resp.StatusCode <> 200 then
raise Exception.Create('Erreur géocodage : ' + Resp.StatusText);
JsonRoot := TJSONObject.ParseJSONValue(Resp.ContentAsString) as TJSONObject;
try
Results := JsonRoot.GetValue<TJSONArray>('results');
if (Results = nil) or (Results.Count = 0) then
raise Exception.Create('Ville non trouvée');
Lat := Results.Items[0].GetValue<Double>('latitude');
Lon := Results.Items[0].GetValue<Double>('longitude');
finally
JsonRoot.Free;
end;
// --- Construction de l'URL pour l'API météo ---
FS := TFormatSettings.Create;
FS.DecimalSeparator := '.';
Url := Format(
'https://api.open-meteo.com/v1/forecast?latitude=%.4f&longitude=%.4f'
+ '¤t_weather=true'
+ '&hourly=relativehumidity_2m,pressure_msl,cloudcover'
+ '&timezone=auto',
[Lat, Lon], FS);
// --- Requête pour les données météo ---
Resp := Http.Get(Url);
if Resp.StatusCode <> 200 then
raise Exception.Create('Erreur météo : ' + Resp.StatusText);
JsonRoot := TJSONObject.ParseJSONValue(Resp.ContentAsString) as TJSONObject;
try
// Extraction des objets JSON
JsonCW := JsonRoot.GetValue<TJSONObject>('current_weather');
JsonHourly := JsonRoot.Values['hourly'] as TJSONObject;
Times := JsonHourly.Values['time'] as TJSONArray;
HumArr := JsonHourly.Values['relativehumidity_2m'] as TJSONArray;
PresArr := JsonHourly.Values['pressure_msl'] as TJSONArray;
CloudArr := JsonHourly.Values['cloudcover'] as TJSONArray;
CurrTime := JsonCW.GetValue<string>('time');
// Trouver l'index d'heure le plus proche de l'heure courante
Index := FindBestTimeIndex(Times, CurrTime);
if Index < 0 then
raise Exception.CreateFmt(
'Aucun créneau horaire valide trouvé pour %s', [CurrTime]);
// Lire les valeurs correspondantes dans les tableaux
Humidity := (HumArr.Items[Index] as TJSONNumber).AsDouble;
Pressure := (PresArr.Items[Index] as TJSONNumber).AsDouble;
CloudCover := (CloudArr.Items[Index] as TJSONNumber).AsDouble;
WindSpeed := JsonCW.GetValue<Double>('windspeed');
WindDir := JsonCW.GetValue<Integer>('winddirection');
WCode := JsonCW.GetValue<Integer>('weathercode');
// --- Préparation du message à afficher ---
Msg := TStringList.Create;
try
Msg.Add(Format(
'Température : %.1f °C',
[JsonCW.GetValue<Double>('temperature')]));
Msg.Add(Format('Humidité : %.0f %%', [Humidity]));
Msg.Add(Format('Pression : %.1f hPa', [Pressure]));
Msg.Add(Format('Nébulosité : %.0f %%', [CloudCover]));
Msg.Add(Format('Vent : %.1f km/h', [WindSpeed]));
Msg.Add(Format('Direction du vent : %d°', [WindDir]));
Msg.Add('Conditions : ' + WeatherCodeToString(WCode));
Msg.Add('Dernière maj : ' + CurrTime);
// Affichage de la météo dans une boîte de dialogue
ShowMessage(Msg.Text);
finally
Msg.Free;
end;
finally
JsonRoot.Free;
end;
finally
Http.Free;
end;
end; |
Partager