Example usage for org.apache.commons.net.nntp NewsgroupInfo getArticleCount

List of usage examples for org.apache.commons.net.nntp NewsgroupInfo getArticleCount

Introduction

In this page you can find the example usage for org.apache.commons.net.nntp NewsgroupInfo getArticleCount.

Prototype

@Deprecated
    public int getArticleCount() 

Source Link

Usage

From source file:io.dimitris.newsgroupwatcher.NewsgroupWatcher.java

protected void watch() throws IOException {

    while (watching) {
        try {// w  ww. j a va  2s.  co  m
            //System.err.println("Watching ");
            client = new NNTPClient();
            client.connect(server);
            if (isAuthenticationRequired()) {
                client.authenticate(username, password);
            }

            client.selectNewsgroup(newsgroup);

            NewsgroupInfo newsgroupInfo = null;

            for (NewsgroupInfo ni : client.listNewsgroups()) {

                if (ni.getNewsgroup().equals(newsgroup)) {
                    newsgroupInfo = ni;
                    break;
                }
            }

            int newArticleCount = newsgroupInfo.getArticleCount();

            //if (articleCount == -1) {
            //   articleCount = newArticleCount;
            //}
            //else 
            if (articleCount < newArticleCount) {
                Reader articleReader = client.retrieveArticleHeader(newArticleCount);
                notifyNewArticleListeners(new ArticleHeader(articleReader));
                articleCount = newArticleCount;
            } else {
                //System.err.println("Still " + newArticleCount);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            sleep();
        }
    }
}

From source file:org.ossmeter.platform.communicationchannel.nntp.local.NNTPDownloader.java

public void downloadMessages(NntpNewsGroup newsgroup) throws Exception {
    final long startTime = System.currentTimeMillis();
    long previousTime = startTime;
    previousTime = printTimeMessage(startTime, previousTime, "Download started");

    NNTPClient nntpClient = NntpUtil.connectToNntpServer(newsgroup);

    NewsgroupInfo newsgroupInfo = NntpUtil.selectNewsgroup(nntpClient, newsgroup);

    int lastArticleChecked = newsgroupInfo.getFirstArticle();
    previousTime = printTimeMessage(startTime, previousTime,
            "First message in newsgroup:\t" + lastArticleChecked);

    int lastArticle = newsgroupInfo.getLastArticle();
    previousTime = printTimeMessage(startTime, previousTime, "Last message in newsgroup:\t" + lastArticle);

    previousTime = printTimeMessage(startTime, previousTime,
            "Articles in newsgroup:\t" + newsgroupInfo.getArticleCount());
    System.err.println();/*from   w ww . j  av a  2 s  .  c o m*/

    Mongo mongo = new Mongo();
    DB db = mongo.getDB(newsgroup.getName() + "LocalStorage");
    Messages dbMessages = new Messages(db);

    NewsgroupData newsgroupData = dbMessages.getNewsgroup().findOneByName(newsgroup.getName());
    if (newsgroupData != null) {
        int newsgroupLastArticleChecked = Integer.parseInt(newsgroupData.getLastArticleChecked());
        if (newsgroupLastArticleChecked > lastArticleChecked) {
            lastArticleChecked = newsgroupLastArticleChecked;
        }
        previousTime = printTimeMessage(startTime, previousTime,
                "Last article checked set to:\t" + lastArticleChecked);
    } else {
        newsgroupData = new NewsgroupData();
        newsgroupData.setName(newsgroup.getName());
        newsgroupData.setUrl(newsgroup.getUrl());
        newsgroupData.setAuthenticationRequired(newsgroup.getAuthenticationRequired());
        newsgroupData.setUsername(newsgroup.getUsername());
        newsgroupData.setPassword(newsgroup.getPassword());
        newsgroupData.setPort(newsgroup.getPort());
        newsgroupData.setInterval(newsgroup.getInterval());
        newsgroupData.setFirstArticle(lastArticleChecked + "");
        dbMessages.getNewsgroup().add(newsgroupData);
    }

    int retrievalStep = RETRIEVAL_STEP;
    while (lastArticleChecked < lastArticle) {
        if (lastArticleChecked + retrievalStep > lastArticle) {
            retrievalStep = lastArticle - lastArticleChecked;
        }
        Article[] articles = NntpUtil.getArticleInfo(nntpClient, lastArticleChecked + 1,
                lastArticleChecked + retrievalStep);
        if (articles.length > 0) {
            Article lastArticleRetrieved = articles[articles.length - 1];
            lastArticleChecked = lastArticleRetrieved.getArticleNumber();
            newsgroupData.setLastArticleChecked(lastArticleChecked + "");
        }
        previousTime = printTimeMessage(startTime, previousTime,
                "downloaded:\t" + articles.length + " nntp articles");
        previousTime = printTimeMessage(startTime, previousTime, "downloading contents\t");

        for (Article article : articles) {
            ArticleData articleData = new ArticleData();
            articleData.setUrl(newsgroup.getUrl());
            articleData.setArticleNumber(article.getArticleNumber());
            articleData.setArticleId(article.getArticleId());
            articleData.setDate(article.getDate());
            articleData.setFrom(article.getFrom());
            articleData.setSubject(article.getSubject());
            for (String referenceId : article.getReferences())
                articleData.getReferences().add(referenceId);
            articleData.setBody(NntpUtil.getArticleBody(nntpClient, article.getArticleNumber()));
            dbMessages.getArticles().add(articleData);
        }
        dbMessages.sync();
        previousTime = printTimeMessage(startTime, previousTime, "stored:\t" + dbMessages.getArticles().size()
                + " / " + newsgroupInfo.getArticleCount() + " nntp articles sofar");
        System.err.println();
    }
    nntpClient.disconnect();
    dbMessages.sync();
    previousTime = printTimeMessage(startTime, previousTime, "stored:\t" + dbMessages.getArticles().size()
            + " / " + newsgroupInfo.getArticleCount() + " nntp articles");
}