Skip to content

Commit

Permalink
Updated to collect BBC News' top story. Also fixed issue with weather…
Browse files Browse the repository at this point in the history
… parsing where inline ',' would cause errors.
  • Loading branch information
followingell committed Jun 5, 2020
1 parent 4b4ae6c commit 1d9289e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
5 changes: 0 additions & 5 deletions CurtainsFunction/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>7.51.0</version>
Expand Down
4 changes: 1 addition & 3 deletions CurtainsFunction/src/main/java/curtains/Affirmation.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ public String getAffirmation() throws IOException {
.execute()
.returnContent().asString();

String affirmationText = Config.MAPPER
return Config.MAPPER
.readTree(affirmationJson)
.get("affirmation")
.asText();

return affirmationText;
}

}
12 changes: 7 additions & 5 deletions CurtainsFunction/src/main/java/curtains/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
public class Config {

public static final ZoneId MY_TIMEZONE = ZoneId.of("Europe/London");
public static final String WTTR_LOCATION = "Stourbridge,+United+Kingdom";
public static final String WTTR_LOCATION = "<LOCATION>";
public static final ObjectMapper MAPPER = new ObjectMapper();

public static final String NEWSAPI_KEY = "<NEWSAPI_KEY>";
public static final String GUARDIAN_OPEN_PLATFORM_API_KEY = "<GUARDIAN_OPEN_PLATFORM_API_KEY>";

public static final String TWILIO_ACCOUNT_SID = "<ACCOUNT_SID>";
public static final String TWILIO_AUTH_TOKEN = "<AUTH_TOKEN>";
public static final String TWILIO_PHONE_NUMBER = "<TWILIO PHONE NUMBER>";
public static final String MY_PHONE_NUMBER = "<OWN MOBILE PHONE NUMBER>";
public static final String TWILIO_ACCOUNT_SID = "<TWILIO_ACCOUNT_SID>";
public static final String TWILIO_AUTH_TOKEN = "<TWILIO_AUTH_TOKEN>";

public static final String TWILIO_PHONE_NUMBER = "<TWILIO_PHONE_NUMBER>";
public static final String MY_PHONE_NUMBER = "<MY_PHONE_NUMBER>";
}
4 changes: 3 additions & 1 deletion CurtainsFunction/src/main/java/curtains/DailyMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ public String getTodaysMessage() throws IOException, ParseException {
+ "The 🌡 is %s, and for the weather, currently, we have %s %s\n"
+ "The humidity is %s with %s of ☔ and %s 🌬\n"
+ "The sun sets at %s & the moon's phase is %s\n"
+ "Today's News 🗞: %s\n"
+ "Remember: %s 💆‍‍\n",
todaysDate, wttr.temperature, wttr.weather, wttr.weatherEmoji,
wttr.humidity, wttr.precipitationMM, wttr.wind, wttr.sunset, wttr.moonPhaseEmoji, affirmation
wttr.humidity, wttr.precipitationMM, wttr.wind, wttr.sunset, wttr.moonPhaseEmoji,
news, affirmation
);
}

Expand Down
27 changes: 25 additions & 2 deletions CurtainsFunction/src/main/java/curtains/News.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
package curtains;

import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.net.URI;
import org.apache.http.client.fluent.Request;

public class News {

public String getNews() throws IOException {

String newsPage = "http://newsapi.org/v2/top-headlines?" + "pageSize=1&"
// today's news
String todaysNewsPage = "https://newsapi.org/v2/top-headlines?" + "pageSize=1&"
+ "sources=bbc-news&" + "apiKey=" + Config.NEWSAPI_KEY;

return "Test";
String todaysNewsJson = Request
.Get(URI.create(todaysNewsPage))
.execute()
.returnContent().asString();

JsonNode todaysNewsObj = Config.MAPPER.readTree(todaysNewsJson);

// historical news
// https://content.guardianapis.com/search?q=12%20years%20a%20slave&format=json&tag=film/film,tone/reviews&from-date=2010-01-01&show-tags=contributor&show-fields=starRating,headline,thumbnail,short-url&order-by=relevance&api-key=test
String historicalNewsPage = "https://content.guardianapis.com/search?" + "pageSize=1&"
+ "api-key=" + Config.GUARDIAN_OPEN_PLATFORM_API_KEY;

if (todaysNewsObj.findValue("status").asText().equals("ok")) {

JsonNode topArticle = todaysNewsObj.findValues("articles").get(0);
return topArticle.findValue("title").asText() + ": " + topArticle.findValue("url").asText();

} else {
return "Error retrieving news";
}
}
}
2 changes: 1 addition & 1 deletion CurtainsFunction/src/main/java/curtains/Wttr.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public WttrResult getTodaysWeather() throws IOException, ParseException {
.execute()
.returnContent()
.asString()
.split(",");
.split(",(?! )");

response[1] = response[1].toLowerCase();

Expand Down

0 comments on commit 1d9289e

Please sign in to comment.