Search in sources :

Example 1 with FeedItem

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;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Request(okhttp3.Request) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) FeedItemType(net.runelite.http.api.feed.FeedItemType) Type(java.lang.reflect.Type) FeedItem(net.runelite.http.api.feed.FeedItem) InternalServerErrorException(net.runelite.http.service.util.exception.InternalServerErrorException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with FeedItem

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());
        }
    }
}
Also used : InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Request(okhttp3.Request) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) Response(okhttp3.Response) FeedItem(net.runelite.http.api.feed.FeedItem) InternalServerErrorException(net.runelite.http.service.util.exception.InternalServerErrorException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ParseException(java.text.ParseException)

Example 3 with FeedItem

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());
        }
    }
}
Also used : InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Request(okhttp3.Request) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) Response(okhttp3.Response) FeedItem(net.runelite.http.api.feed.FeedItem) InternalServerErrorException(net.runelite.http.service.util.exception.InternalServerErrorException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ParseException(java.text.ParseException)

Aggregations

IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 FeedItem (net.runelite.http.api.feed.FeedItem)3 InternalServerErrorException (net.runelite.http.service.util.exception.InternalServerErrorException)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 ParseException (java.text.ParseException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 SAXException (org.xml.sax.SAXException)2 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 FeedItemType (net.runelite.http.api.feed.FeedItemType)1 HttpUrl (okhttp3.HttpUrl)1