http-service: add services for getting blog posts, tweets and osrs news

This commit is contained in:
Lotto
2018-03-02 23:27:18 +01:00
committed by Adam
parent f9cae8416a
commit 8259410178
7 changed files with 556 additions and 0 deletions

View File

@@ -0,0 +1,131 @@
/*
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service.feed.blog;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.feed.FeedItem;
import net.runelite.http.api.feed.FeedItemType;
import net.runelite.http.service.util.exception.InternalServerErrorException;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
@Service
@Slf4j
public class BlogService
{
private static final HttpUrl RSS_URL = HttpUrl.parse("https://runelite.net/atom.xml");
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
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());
}
}
}
}

View File

@@ -0,0 +1,131 @@
/*
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service.feed.osrsnews;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.feed.FeedItem;
import net.runelite.http.api.feed.FeedItemType;
import net.runelite.http.service.util.exception.InternalServerErrorException;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
@Service
@Slf4j
public class OSRSNewsService
{
private static final HttpUrl RSS_URL = HttpUrl.parse("http://services.runescape.com/m=news/latest_news.rss?oldschool=true");
private static final SimpleDateFormat PUB_DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy '00:00:00 GMT'", Locale.US);
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());
}
}
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service.feed.twitter;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
@Data
class TwitterOAuth2TokenResponse
{
@SerializedName("token_type")
private String tokenType;
@SerializedName("access_token")
private String token;
}

View File

@@ -0,0 +1,180 @@
/*
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service.feed.twitter;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.feed.FeedItem;
import net.runelite.http.api.feed.FeedItemType;
import net.runelite.http.service.util.exception.InternalServerErrorException;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class TwitterService
{
private static final HttpUrl AUTH_URL = HttpUrl.parse("https://api.twitter.com/oauth2/token");
private static final HttpUrl LIST_STATUSES_URL = HttpUrl.parse("https://api.twitter.com/1.1/lists/statuses.json");
private final String credentials;
private final String listId;
private String token;
@Autowired
public TwitterService(
@Value("${runelite.twitter.consumerkey}") String consumerKey,
@Value("${runelite.twitter.secretkey}") String consumerSecret,
@Value("${runelite.twitter.listid}") String listId
)
{
this.credentials = consumerKey + ":" + consumerSecret;
this.listId = listId;
}
public List<FeedItem> getTweets() throws IOException
{
return getTweets(false);
}
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;
}
}
private void updateToken() throws IOException
{
String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes());
Request request = new Request.Builder()
.url(AUTH_URL)
.header("Authorization", "Basic " + encodedCredentials)
.post(new FormBody.Builder().add("grant_type", "client_credentials").build())
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
{
if (!response.isSuccessful())
{
throw new IOException("Error authing to Twitter: " + response.message());
}
InputStream in = response.body().byteStream();
TwitterOAuth2TokenResponse tokenResponse = RuneLiteAPI.GSON
.fromJson(new InputStreamReader(in), TwitterOAuth2TokenResponse.class);
if (!tokenResponse.getTokenType().equals("bearer"))
{
throw new InternalServerErrorException("Returned token was not a bearer token");
}
if (tokenResponse.getToken() == null)
{
throw new InternalServerErrorException("Returned token was null");
}
token = tokenResponse.getToken();
}
}
/**
* Extracts the UTC timestamp from a Twitter snowflake as per
* https://github.com/client9/snowflake2time/blob/master/python/snowflake.py#L24
*/
private long getTimestampFromSnowflake(long snowflake)
{
return (snowflake >> 22) + 1288834974657L;
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service.feed.twitter;
import lombok.Data;
@Data
class TwitterStatusesResponseItem
{
private long id;
private String text;
private TwitterStatusesResponseItemUser user;
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2018, Lotto <https://github.com/devLotto>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service.feed.twitter;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
@Data
class TwitterStatusesResponseItemUser
{
@SerializedName("screen_name")
private String screenName;
@SerializedName("profile_image_url_https")
private String profileImageUrl;
}