Search in sources :

Example 1 with MagicNewsContent

use of org.magic.api.beans.MagicNewsContent in project MtgDesktopCompanion by nicho92.

the class TwitterNewsProvider method listNews.

@Override
public List<MagicNewsContent> listNews(MagicNews n) throws IOException {
    Twitter twitter = tf.getInstance();
    Query query = new Query(n.getName());
    query.setCount(Integer.parseInt(getString("MAX_RESULT")));
    List<MagicNewsContent> ret = new ArrayList<>();
    QueryResult result;
    try {
        result = twitter.search(query);
        for (Status status : result.getTweets()) {
            MagicNewsContent content = new MagicNewsContent();
            content.setAuthor(status.getUser().getScreenName());
            content.setDate(status.getCreatedAt());
            content.setContent(status.getText());
            content.setLink(new URL("https://twitter.com/" + status.getUser().getScreenName() + "/status/" + status.getId()));
            content.setTitle(status.getText());
            ret.add(content);
        }
    } catch (TwitterException e) {
        throw new IOException(e);
    }
    return ret;
}
Also used : Status(twitter4j.Status) QueryResult(twitter4j.QueryResult) Query(twitter4j.Query) MagicNewsContent(org.magic.api.beans.MagicNewsContent) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) IOException(java.io.IOException) TwitterException(twitter4j.TwitterException) URL(java.net.URL)

Example 2 with MagicNewsContent

use of org.magic.api.beans.MagicNewsContent in project MtgDesktopCompanion by nicho92.

the class RSSNewsProvider method listNews.

@Override
public List<MagicNewsContent> listNews(MagicNews rssBean) throws IOException {
    InputStream is = null;
    SyndFeed feed;
    List<MagicNewsContent> ret = new ArrayList<>();
    try {
        HttpURLConnection openConnection = (HttpURLConnection) new URL(rssBean.getUrl()).openConnection();
        logger.debug("reading " + rssBean.getUrl());
        openConnection.setRequestProperty("User-Agent", MTGConstants.USER_AGENT);
        openConnection.setInstanceFollowRedirects(true);
        is = openConnection.getInputStream();
        InputSource source = new InputSource(is);
        feed = input.build(source);
        String baseURI = feed.getLink();
        for (SyndEntry s : feed.getEntries()) {
            MagicNewsContent content = new MagicNewsContent();
            content.setTitle(s.getTitle());
            content.setAuthor(s.getAuthor());
            content.setDate(s.getPublishedDate());
            URL link;
            if (!s.getLink().startsWith(baseURI))
                link = new URL(baseURI + s.getLink());
            else
                link = new URL(s.getLink());
            content.setLink(link);
            ret.add(content);
        }
        return ret;
    } catch (IllegalArgumentException | FeedException e) {
        throw new IOException(e);
    } finally {
        if (is != null)
            is.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) MagicNewsContent(org.magic.api.beans.MagicNewsContent) FeedException(com.rometools.rome.io.FeedException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) HttpURLConnection(java.net.HttpURLConnection)

Aggregations

IOException (java.io.IOException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 MagicNewsContent (org.magic.api.beans.MagicNewsContent)2 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)1 SyndFeed (com.rometools.rome.feed.synd.SyndFeed)1 FeedException (com.rometools.rome.io.FeedException)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 InputSource (org.xml.sax.InputSource)1 Query (twitter4j.Query)1 QueryResult (twitter4j.QueryResult)1 Status (twitter4j.Status)1 Twitter (twitter4j.Twitter)1 TwitterException (twitter4j.TwitterException)1