diff --git a/src/main/java/eirb/pg203/Main.java b/src/main/java/eirb/pg203/Main.java index b22b187..2b3ff42 100644 --- a/src/main/java/eirb/pg203/Main.java +++ b/src/main/java/eirb/pg203/Main.java @@ -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"); } diff --git a/src/main/java/eirb/pg203/OpenWeatherMap.java b/src/main/java/eirb/pg203/OpenWeatherMap.java new file mode 100644 index 0000000..ed5b8bb --- /dev/null +++ b/src/main/java/eirb/pg203/OpenWeatherMap.java @@ -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 getTemperatures(int days, String city) throws IOException { + JSONObject result = fetchWeather(days, new City(city)); + JSONArray list = result.getJSONArray("list"); + + ArrayList 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"; + } +} diff --git a/src/main/java/eirb/pg203/WeatherAPI.java b/src/main/java/eirb/pg203/WeatherAPI.java index 6c6f2f1..8bffa02 100644 --- a/src/main/java/eirb/pg203/WeatherAPI.java +++ b/src/main/java/eirb/pg203/WeatherAPI.java @@ -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 diff --git a/src/main/java/eirb/pg203/WeatherDisplayBasic.java b/src/main/java/eirb/pg203/WeatherDisplayBasic.java index f6ce3e7..cf2eb9c 100644 --- a/src/main/java/eirb/pg203/WeatherDisplayBasic.java +++ b/src/main/java/eirb/pg203/WeatherDisplayBasic.java @@ -3,7 +3,7 @@ package eirb.pg203; import java.util.ArrayList; class WeatherDisplayBasic implements WeatherDisplay { - ArrayList apis; + private ArrayList apis; WeatherDisplayBasic() { this.apis = new ArrayList(); @@ -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); } }