use of net.runelite.http.api.feed.FeedItem in project runelite by runelite.
the class TwitterService method getTweets.
private List<FeedItem> getTweets(boolean hasRetried) throws IOException {
if (token == null) {
updateToken();
}
HttpUrl url = LIST_STATUSES_URL.newBuilder().addQueryParameter("list_id", listId).addQueryParameter("count", "15").addQueryParameter("include_entities", "false").build();
Request request = new Request.Builder().url(url).header("Authorization", "Bearer " + token).build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) {
if (!response.isSuccessful()) {
switch(HttpStatus.valueOf(response.code())) {
case BAD_REQUEST:
case UNAUTHORIZED:
updateToken();
if (!hasRetried) {
return getTweets(true);
}
throw new InternalServerErrorException("Could not auth to Twitter after trying once: " + response.message());
default:
throw new IOException("Error getting Twitter list: " + response.message());
}
}
InputStream in = response.body().byteStream();
Type listType = new TypeToken<List<TwitterStatusesResponseItem>>() {
}.getType();
List<TwitterStatusesResponseItem> statusesResponse = RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), listType);
List<FeedItem> items = new ArrayList<>();
for (TwitterStatusesResponseItem i : statusesResponse) {
items.add(new FeedItem(FeedItemType.TWEET, i.getUser().getProfileImageUrl(), i.getUser().getScreenName(), i.getText().replace("\n\n", " ").replaceAll("\n", " "), "https://twitter.com/statuses/" + i.getId(), getTimestampFromSnowflake(i.getId())));
}
return items;
}
}
use of net.runelite.http.api.feed.FeedItem in project runelite by runelite.
the class OSRSNewsService method getNews.
public List<FeedItem> getNews() throws IOException {
Request request = new Request.Builder().url(RSS_URL).build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Error getting OSRS news: " + response.message());
}
try {
InputStream in = response.body().byteStream();
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
Element documentElement = document.getDocumentElement();
NodeList documentItems = documentElement.getElementsByTagName("item");
List<FeedItem> items = new ArrayList<>();
for (int i = 0; i < documentItems.getLength(); i++) {
Node item = documentItems.item(i);
NodeList children = item.getChildNodes();
String title = null;
String description = null;
String link = null;
long timestamp = -1;
for (int j = 0; j < children.getLength(); j++) {
Node childItem = children.item(j);
String nodeName = childItem.getNodeName();
switch(nodeName) {
case "title":
title = childItem.getTextContent();
break;
case "description":
description = childItem.getTextContent().replace("\n", "").trim();
break;
case "link":
link = childItem.getTextContent();
break;
case "pubDate":
timestamp = PUB_DATE_FORMAT.parse(childItem.getTextContent()).getTime();
break;
}
}
if (title == null || description == null || link == null || timestamp == -1) {
throw new InternalServerErrorException("Failed to find title, description, link and/or timestamp in the OSRS RSS feed");
}
items.add(new FeedItem(FeedItemType.OSRS_NEWS, title, description, link, timestamp));
}
return items;
} catch (ParserConfigurationException | SAXException | ParseException e) {
throw new InternalServerErrorException("Failed to parse OSRS news: " + e.getMessage());
}
}
}
use of net.runelite.http.api.feed.FeedItem in project runelite by runelite.
the class BlogService method getBlogPosts.
public List<FeedItem> getBlogPosts() throws IOException {
Request request = new Request.Builder().url(RSS_URL).build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Error getting blog posts: " + response.message());
}
try {
InputStream in = response.body().byteStream();
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
Element documentElement = document.getDocumentElement();
NodeList documentItems = documentElement.getElementsByTagName("entry");
List<FeedItem> items = new ArrayList<>();
for (int i = 0; i < Math.min(documentItems.getLength(), 3); i++) {
Node item = documentItems.item(i);
NodeList children = item.getChildNodes();
String title = null;
String summary = null;
String link = null;
long timestamp = -1;
for (int j = 0; j < children.getLength(); j++) {
Node childItem = children.item(j);
String nodeName = childItem.getNodeName();
switch(nodeName) {
case "title":
title = childItem.getTextContent();
break;
case "summary":
summary = childItem.getTextContent().replace("\n", "").trim();
break;
case "link":
link = childItem.getAttributes().getNamedItem("href").getTextContent();
break;
case "updated":
timestamp = DATE_FORMAT.parse(childItem.getTextContent()).getTime();
break;
}
}
if (title == null || summary == null || link == null || timestamp == -1) {
throw new InternalServerErrorException("Failed to find title, summary, link and/or timestamp in the blog post feed");
}
items.add(new FeedItem(FeedItemType.BLOG_POST, title, summary, link, timestamp));
}
return items;
} catch (ParserConfigurationException | SAXException | ParseException e) {
throw new InternalServerErrorException("Failed to parse blog posts: " + e.getMessage());
}
}
}
Aggregations