feat: add OpenMeteo support
This commit is contained in:
parent
3a19820ffa
commit
3cc14c19b1
@ -6,9 +6,11 @@ public class Main {
|
|||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
String APIKey = "cef8e1b6ea364994b5072423240111";
|
String APIKey = "cef8e1b6ea364994b5072423240111";
|
||||||
WeatherAPI weatherAPI = new WeatherAPI(APIKey);
|
WeatherAPI weatherAPI = new WeatherAPI(APIKey);
|
||||||
|
OpenMeteo openMeteo = new OpenMeteo();
|
||||||
WeatherDisplay display = new WeatherDisplayBasic();
|
WeatherDisplay display = new WeatherDisplayBasic();
|
||||||
|
|
||||||
display.addAPI(weatherAPI);
|
display.addAPI(weatherAPI);
|
||||||
|
display.addAPI(openMeteo);
|
||||||
|
|
||||||
display.display(5, "Bordeaux");
|
display.display(5, "Bordeaux");
|
||||||
}
|
}
|
||||||
|
63
src/main/java/eirb/pg203/OpenMeteo.java
Normal file
63
src/main/java/eirb/pg203/OpenMeteo.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package eirb.pg203;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import eirb.pg203.utils.JSONFetcher;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
// https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m
|
||||||
|
|
||||||
|
public class OpenMeteo implements WeatherDataAPI {
|
||||||
|
private static final String forecastBaseURL = "https://api.open-meteo.com/v1/forecast";
|
||||||
|
|
||||||
|
private JSONObject fetchWeather(int days, City city) throws IOException {
|
||||||
|
URL url = URI.create(
|
||||||
|
String.format(forecastBaseURL + "?latitude=%.2f&longitude=%.2f&forecast_days=%d&daily=apparent_temperature_min,apparent_temperature_max",
|
||||||
|
city.getCityCoords().getLat(),
|
||||||
|
city.getCityCoords().getLon(),
|
||||||
|
days
|
||||||
|
)
|
||||||
|
).toURL();
|
||||||
|
|
||||||
|
return JSONFetcher.fetch(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param day Day, 0 ≤ day ≤ 14
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Temperature getTemperature(int day, String city) throws IOException {
|
||||||
|
JSONObject result = fetchWeather(day+1, new City(city));
|
||||||
|
JSONObject daily = result.getJSONObject("daily");
|
||||||
|
float temp_c = (daily.getJSONArray("apparent_temperature_max").getFloat(0) + daily.getJSONArray("apparent_temperature_min").getFloat(0)) / 2;
|
||||||
|
|
||||||
|
return new Temperature(temp_c, new City(city), Instant.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Temperature getTemperature(int day, int hour, String city) throws IOException{
|
||||||
|
return getTemperature(day, city);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ArrayList<Temperature> getTemperatures(int days, String city) throws IOException {
|
||||||
|
JSONObject result = fetchWeather(days, new City(city));
|
||||||
|
ArrayList<Temperature> temperatures = new ArrayList<>();
|
||||||
|
JSONObject daily = result.getJSONObject("daily");
|
||||||
|
for (int day = 0; day < days; ++day) {
|
||||||
|
float temp_c = (daily.getJSONArray("apparent_temperature_max").getFloat(day) + daily.getJSONArray("apparent_temperature_min").getFloat(day)) / 2;
|
||||||
|
temperatures.add(new Temperature(temp_c, new City(city), Instant.now()));
|
||||||
|
}
|
||||||
|
return temperatures;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAPIName() {
|
||||||
|
return "OpenMeteo";
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user