Exemplo n.º 1
0
  public List<String> getNewLinks() {
    List<String> ret = new ArrayList<String>();

    Set<String> oldSet = lastCheckLinks;
    lastCheckLinks = new HashSet<String>();

    // check to see if it is the first time that this method has been called since construction of
    // the object
    boolean firstTime = false;
    if (oldSet == null) {
      firstTime = true;
    }

    for (String feed : feeds) {
      List<String> links = checkFeed(feed);
      // if it is the first time running check with database to see if a link has been added or not
      if (firstTime) {
        IDatabase db = Manager.getDatabase();
        for (String link : links) {
          if (!db.articleExists(link)) {
            ret.add(link);
            lastCheckLinks.add(link);
          }
        }
      }
      // if it isn't the first time running use the already cached set to determine repeats.
      else {
        for (String link : links) {
          // just add it to the new lastCheckLinks set don't add it to the return array since it's
          // already been returned before
          if (oldSet.contains(link)) {
            lastCheckLinks.add(link);
          }
          // add to both new lastCheckLinks set and the return List
          else {
            lastCheckLinks.add(link);
            ret.add(link);
          }
        }
      }
    }

    return ret;
  }
Exemplo n.º 2
0
 @Override
 public void run() {
   ArticleAggregator agg = new ArticleAggregator();
   IDatabase db = Manager.getDatabase();
   SourceParser parser = new SourceParser();
   int counter = 0;
   for (String link : links) {
     Article article = parser.parse(new Source(link));
     // if the article object is null that means an error occurred while parsing the link look in
     // error log file for reason
     if (article != null) {
       counter++;
       System.out.println(counter + " " + link);
       Story matchedStory = agg.matchArticle(article);
       // if null was returned from the aggregator a new Story entry must be made because there
       // is no good match in the database
       if (matchedStory == null) {
         // use the values of the article to set up the Story
         Story story = new Story();
         // TODO come up with better way of choosing category than taking the category from the
         // article
         story.setCategory(article.getCategory());
         story.setTitle(article.getTitle());
         story.setDate(article.getDate());
         // id will be set when the store method is carried out
         db.storeStory(story);
         article.setStoryID(story.getID());
       }
       // if matchedStory isn't null it was matched to an already existing Story entry
       else {
         article.setStoryID(matchedStory.getID());
       }
       db.storeArticle(article);
     }
     //				if (counter > ){
     ////					db.printArticleTable();
     //					System.exit(0);
     //				}
   }
 }