Skip to content

Commit

Permalink
Migrate from deprecated Dark Sky API to OpenWeather API
Browse files Browse the repository at this point in the history
  • Loading branch information
yaseenmustapha committed Aug 8, 2023
1 parent c7a533f commit bdc3158
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 97 deletions.
4 changes: 2 additions & 2 deletions src/main/api/APIcaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

public abstract class APIcaller {

public static final String googleApiKey = "AIzaSyDQQdJfzFRriSqWDsik4B3gOfC09RUmIEY";
public static final String googleApiKey = System.getenv("GOOGLE_API_KEY");

public static final String darkSkyApiKey = "a44d0775e4a62974499471b90c274895";
public static final String darkSkyApiKey = System.getenv("OPENWEATHER_API_KEY");

public String errorMessage;

Expand Down
79 changes: 47 additions & 32 deletions src/main/api/DarkSky.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public DarkSky(Location location) {
this.location = location;
String units;
if (User.getCurrentUser().isMetric()) {
units = "ca";
units = "metric";
} else {
units = "us";
units = "imperial";
}
try {
setURL(new URL("https://api.darksky.net/forecast/" + darkSkyApiKey + "/"
+ this.location.getLatitude() + "," + this.location.getLongitude()
+ "?exclude=minutely,flags&units=" + units));
setURL(new URL("https://api.openweathermap.org/data/3.0/onecall?lat="
+ this.location.getLatitude() + "&lon=" + this.location.getLongitude()
+ "&exclude=minutely&units=" + units + "&appid=" + darkSkyApiKey));
} catch (MalformedURLException e) {
e.printStackTrace();
}
Expand All @@ -59,17 +59,17 @@ public void parse(String responseBody) throws ParseException {
throw new ParseException();
} else {
JSONObject obj = new JSONObject(responseBody);
currently = getConditions(obj.getJSONObject("currently"));
currently = getConditions(obj.getJSONObject("current"));

setHourly(obj.getJSONObject("hourly"));
setDaily(obj.getJSONObject("daily"));
setHourly(obj.getJSONArray("hourly"));
setDaily(obj.getJSONArray("daily"));

if (obj.has("alerts")) {
System.out.println("ALERT DETECTED");
JSONArray alerts = obj.getJSONArray("alerts");
JSONObject alert = alerts.getJSONObject(0);
Alert warning = new Alert(alert.getString("title"), alert.getInt("time"),
alert.getInt("expires"), alert.getString("description"), alert.getString("uri"));
Alert warning = new Alert(alert.getString("event"), alert.getInt("start"),
alert.getInt("end"), alert.getString("description"));
this.location.setAlert(warning);
} else {
System.out.println("NO ALERTS");
Expand All @@ -78,17 +78,18 @@ public void parse(String responseBody) throws ParseException {
}
}

private void setDaily(JSONObject daily) {
JSONArray data = daily.getJSONArray("data");
private void setDaily(JSONArray daily) {
DateFormat dateFormat = new SimpleDateFormat("E dd");

for (int i = 1; i < data.length(); i++) {
JSONObject day = data.getJSONObject(i);
dailyHigh.add(day.getDouble("temperatureHigh"));
dailyLow.add(day.getDouble("temperatureLow"));
dailyTimes.add(dateFormat.format(new Date((long)day.getInt("time") * 1000)));
dailyIcons.add(day.getString("icon"));
System.out.println(dateFormat.format(new Date((long)day.getInt("time") * 1000)));
for (int i = 1; i < daily.length(); i++) {
JSONObject day = daily.getJSONObject(i);
JSONObject temp = day.getJSONObject("temp");
dailyHigh.add(temp.getDouble("max"));
dailyLow.add(temp.getDouble("min"));
dailyTimes.add(dateFormat.format(new Date((long)day.getInt("dt") * 1000)));
JSONObject weatherObj = day.getJSONArray("weather").getJSONObject(0);
dailyIcons.add(weatherObj.getString("icon"));
System.out.println(dateFormat.format(new Date((long)day.getInt("dt") * 1000)));
}
}

Expand All @@ -108,16 +109,15 @@ public ArrayList<String> getDailyIcons() {
return dailyIcons;
}

private void setHourly(JSONObject hourly) {
JSONArray data = hourly.getJSONArray("data");
private void setHourly(JSONArray hourly) {
DateFormat dateFormat = new SimpleDateFormat("HH");

for (int i = 0; i < data.length(); i++) {
JSONObject hour = data.getJSONObject(i);
int hourInt = Integer.parseInt(dateFormat.format(new Date((long)hour.getInt("time") * 1000)));
for (int i = 0; i < hourly.length(); i++) {
JSONObject hour = hourly.getJSONObject(i);
int hourInt = Integer.parseInt(dateFormat.format(new Date((long)hour.getInt("dt") * 1000)));
if (!hourlyTimes.contains(hourInt)) {
hourlyTemps.add(hour.getDouble("temperature"));
hourlyApparentTemps.add(hour.getDouble("apparentTemperature"));
hourlyTemps.add(hour.getDouble("temp"));
hourlyApparentTemps.add(hour.getDouble("feels_like"));
hourlyTimes.add(hourInt);
}
}
Expand All @@ -136,12 +136,27 @@ public ArrayList<Integer> getHourlyTimes() {
}

private Conditions getConditions(JSONObject obj) {
return new Conditions(obj.getInt("time"), obj.getString("summary"),
obj.getString("icon"), obj.getDouble("precipIntensity"),
obj.getDouble("precipProbability"), obj.getDouble("temperature"),
obj.getDouble("apparentTemperature"), obj.getDouble("dewPoint"),
obj.getDouble("humidity"), obj.getDouble("pressure"),
obj.getDouble("windSpeed"), obj.getInt("windBearing"));
JSONObject weatherObj = obj.getJSONArray("weather").getJSONObject(0);
JSONObject rainObj = obj.optJSONObject("rain");
double rain1h = 0.0;

if (rainObj != null) {
rain1h = rainObj.optDouble("1h", 0.0);
}

return new Conditions(
obj.getInt("dt"),
weatherObj.getString("description"),
weatherObj.getString("icon"),
rain1h,
obj.getDouble("uvi"),
obj.getDouble("temp"),
obj.getDouble("feels_like"),
obj.getDouble("dew_point"),
obj.getInt("humidity"),
obj.getInt("pressure"),
obj.getDouble("wind_speed"),
obj.getInt("wind_deg"));
}

public Conditions getCurrently() {
Expand Down
5 changes: 2 additions & 3 deletions src/main/api/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// Reference: based on video by Genuine Coder (https://www.youtube.com/watch?v=A7HAB5whD6I)
public class Email {
private static String appEmail = "[email protected]";
private static String appPassword = "javaiscool";
private static String appPassword = System.getenv("APP_PASS");

public static void sendMail(User recipient, Location alertedLocation) throws Exception {
System.out.println("Preparing to send email...");
Expand Down Expand Up @@ -53,8 +53,7 @@ private static Message generateMessage(Session session, String recipientEmail, L
message.setText(alert.getTitle() + "\n"
+ "Time: " + alert.getTime() + "\n"
+ "Expires: " + alert.getExpires() + "\n\n"
+ alert.getDescription() + "\n"
+ "For more information: " + alert.getUri());
+ alert.getDescription() + "\n");
return message;
} catch (Exception e) {
Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, e);
Expand Down
8 changes: 1 addition & 7 deletions src/main/model/Alert.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ public class Alert {
private String time;
private String expires;
private String description;
private String uri;
private Boolean notified;

public Alert(String title, int time, int expires, String description, String uri) {
public Alert(String title, int time, int expires, String description) {
this.title = title;

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.time = dateFormat.format(new Date((long)time * 1000));
this.expires = dateFormat.format(new Date((long)expires * 1000));
this.description = description.replaceAll("\\*", "\n*");
this.uri = uri;
this.notified = false;
}

Expand All @@ -46,8 +44,4 @@ public String getExpires() {
public String getDescription() {
return description;
}

public String getUri() {
return uri;
}
}
22 changes: 11 additions & 11 deletions src/main/model/Conditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ public class Conditions {
private String summary;
private String icon;
private double precipIntensity;
private double precipProbability;
private double uvIndex;
private double temperature;
private double apparentTemperature;
private double dewPoint;
private double humidity;
private double pressure;
private int humidity;
private int pressure;
private double windSpeed;
private double windBearing;

public Conditions(int time, String summary, String icon, double precipIntensity, double precipProbability,
double temperature, double apparentTemperature, double dewPoint, double humidity,
double pressure, double windSpeed, int windBearing) {
public Conditions(int time, String summary, String icon, double precipIntensity, double uvIndex,
double temperature, double apparentTemperature, double dewPoint, int humidity,
int pressure, double windSpeed, int windBearing) {
this.timeUnix = time;

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Expand All @@ -30,7 +30,7 @@ public Conditions(int time, String summary, String icon, double precipIntensity,
this.summary = summary;
this.icon = icon;
this.precipIntensity = precipIntensity;
this.precipProbability = precipProbability;
this.uvIndex = uvIndex;
this.temperature = temperature;
this.apparentTemperature = apparentTemperature;
this.dewPoint = dewPoint;
Expand All @@ -52,8 +52,8 @@ public double getPrecipIntensity() {
return precipIntensity;
}

public double getPrecipProbability() {
return precipProbability;
public double getUvIndex() {
return uvIndex;
}

public double getTemperature() {
Expand All @@ -68,11 +68,11 @@ public double getDewPoint() {
return dewPoint;
}

public double getHumidity() {
public int getHumidity() {
return humidity;
}

public double getPressure() {
public int getPressure() {
return pressure;
}

Expand Down
Loading

0 comments on commit bdc3158

Please sign in to comment.