feat: add OpenWeatherMap support

This commit is contained in:
Nemo D'ACREMONT 2024-11-15 08:18:08 +01:00
parent 3cc14c19b1
commit 5fe57da57e
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276
4 changed files with 92 additions and 5 deletions

View File

@ -4,13 +4,16 @@ import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
String APIKey = "cef8e1b6ea364994b5072423240111";
WeatherAPI weatherAPI = new WeatherAPI(APIKey);
String WeatherAPIKey = "cef8e1b6ea364994b5072423240111";
String OpenWMapKey = "5719b35ddcb39846c228ea2e37357464";
WeatherAPI weatherAPI = new WeatherAPI(WeatherAPIKey);
OpenMeteo openMeteo = new OpenMeteo();
OpenWeatherMap openWeatherMap = new OpenWeatherMap(OpenWMapKey);
WeatherDisplay display = new WeatherDisplayBasic();
display.addAPI(weatherAPI);
display.addAPI(openMeteo);
display.addAPI(openWeatherMap);
display.display(5, "Bordeaux");
}

View File

@ -0,0 +1,82 @@
package eirb.pg203;
import org.json.JSONObject;
import org.json.JSONArray;
import eirb.pg203.utils.JSONFetcher;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
// https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m
public class OpenWeatherMap implements WeatherDataAPI {
private static final String forecastBaseURL = "https://api.openweathermap.org/data/2.5/forecast";
private String APIKey;
OpenWeatherMap(String APIKey) {
this.APIKey = APIKey;
}
private JSONObject fetchWeather(int days, City city) throws IOException {
URL url = URI.create(
String.format(forecastBaseURL + "?appid=%s&lat=%.2f&lon=%.2f&units=metric",
APIKey,
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));
JSONArray list = result.getJSONArray("list");
float temp_c = list.getJSONObject(0).getJSONObject("main").getFloat("temp");
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));
JSONArray list = result.getJSONArray("list");
ArrayList<Temperature> temperatures = new ArrayList<>();
int cnt = result.getInt("cnt");
DayOfWeek day = Instant.now().atZone(ZoneId.systemDefault()).getDayOfWeek();
for (int i = 0; i < cnt; ++i) {
JSONObject data = list.getJSONObject(i);
int timestamp = data.getInt("dt");
if (day == Instant.ofEpochSecond(timestamp).atZone(ZoneId.systemDefault()).getDayOfWeek())
{
float temp_c = data.getJSONObject("main").getFloat("temp");
temperatures.add(new Temperature(temp_c, new City(city), Instant.now()));
}
}
return temperatures;
}
@Override
public String getAPIName() {
return "OpenWeatherMap";
}
}

View File

@ -13,6 +13,7 @@ import java.util.ArrayList;
public class WeatherAPI implements WeatherDataAPI{
private final String weatherAPIKey;
private static final String forecastBaseURL = "https://api.weatherapi.com/v1/forecast.json";
WeatherAPI(String weatherAPIKey) {
this.weatherAPIKey = weatherAPIKey;
@ -20,7 +21,7 @@ public class WeatherAPI implements WeatherDataAPI{
private JSONObject fetchWeather(int days, String city) throws IOException {
URL url = URI.create(
String.format("https://api.weatherapi.com/v1/forecast.json?key=%s&q=%s&days=%d",
String.format(forecastBaseURL + "?key=%s&q=%s&days=%d",
this.weatherAPIKey,
city,
days

View File

@ -3,7 +3,7 @@ package eirb.pg203;
import java.util.ArrayList;
class WeatherDisplayBasic implements WeatherDisplay {
ArrayList<WeatherDataAPI> apis;
private ArrayList<WeatherDataAPI> apis;
WeatherDisplayBasic() {
this.apis = new ArrayList<WeatherDataAPI>();
@ -42,12 +42,13 @@ class WeatherDisplayBasic implements WeatherDisplay {
temperatures = w.getTemperatures(days, city);
displayTemperatures(w.getAPIName(), temperatures);
} catch (Exception e) {
System.err.println(e);
System.err.println("ntm++ martin");
}
}
}
public void addAPI(WeatherDataAPI w) {
public void addAPI(WeatherDataAPI w) {
this.apis.add(w);
}
}