public void run() {
   Manager manager = Manager.getInstance();
   if (manager.getFormatFile() == 1) { // if file format =1, start XML Parser
     xmlFile.downloadFile();
     synchronized (xmlParseThread) {
       xmlParseThread.notify();
     }
   }
   if (manager.getFormatFile() == 2) { // if file format = 2, start JSON Parser
     jsonFile.downloadFile();
     synchronized (jsonParseThread) {
       jsonParseThread.notify();
     }
   }
 }
示例#2
0
  public static void main(String[] args) {
    List<String> feeds = new ArrayList<String>();
    IPreferences pref = Manager.getPreferences();
    String[] rssFeeds = pref.getRssFeeds();
    for (String url : rssFeeds) {
      feeds.add(url);
    }
    RSSMonitor monitor = new RSSMonitor(feeds);

    long timeSplit = pref.getTimeBetweenRssChecksMs();
    long nextCheck = System.currentTimeMillis() + timeSplit;

    while (true) {
      List<String> links = monitor.getNewLinks();
      Runnable analyzer = monitor.new LinkAnalyzerRunnable(links);
      Thread thread = new Thread(analyzer);
      thread.start();
      //			System.out.println("New
      // Links\n-------------------------------------------------------------------------");
      //			for (String link: links){
      //				System.out.println(link);
      //			}

      while (System.currentTimeMillis() < nextCheck) {}
      nextCheck += timeSplit;
    }
  }
示例#3
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;
  }
示例#4
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);
     //				}
   }
 }
示例#5
0
  public void jsonParser() {
    int iterator = 0;
    Manager manager = Manager.getInstance(); // Create the object of singleton
    Element jacksonElement = new Element();

    JsonFactory jsonFactory = new JsonFactory();
    try {
      File f = new File(FileDownload.getFileName());
      JsonParser parser = jsonFactory.createParser(f);

      // Parse the file  by Jackson parser
      while (!parser.isClosed()) {
        JsonToken jsonToken = parser.nextToken();

        if (JsonToken.FIELD_NAME.equals(jsonToken)) {
          String fieldName = parser.getCurrentName();
          jsonToken = parser.nextToken();

          // The JSON file has the next fields:
          // location,date,name and object stock (this object
          // includes:name,bid,id,visible,minPrice,maxPrice)

          if ("location".equals(fieldName)) {
            manager.setLocation(parser.getValueAsString());
          }
          if ("date".equals(fieldName)) {
            manager.setDate(parser.getValueAsString());
          }
          if ("name".equals(fieldName) && (parser.getValueAsString().equals("Moscow Stock")))
            manager.setName(parser.getValueAsString());

          if ("bid".equals(fieldName)) {
            jacksonElement.setBid(parser.getValueAsDouble());
            iterator++;
          } else if ("id".equals(fieldName)) {
            jacksonElement.setId(parser.getValueAsInt());
            iterator++;
          } else if ("name".equals(fieldName)
              && (!parser.getValueAsString().equals("Moscow Stock"))) {
            jacksonElement.setName(
                parser.getValueAsString().substring(0, parser.getValueAsString().length() - 4));
            iterator++;
          } else if ("minPrice".equals(fieldName)) {
            jacksonElement.setMinPrice(parser.getValueAsDouble());
            iterator++;
          } else if ("maxPrice".equals(fieldName)) {
            jacksonElement.setMaxPrice(parser.getValueAsDouble());
            iterator++;
          } else if ("visible".equals(fieldName)) {
            jacksonElement.setVisible(parser.getValueAsBoolean());
            iterator++;
          }
          if (iterator == 6) {
            manager.setJsonList(jacksonElement); // add the data to the list
            jacksonElement = new Element();
            iterator = 0;
          }
        }
      }
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
示例#6
0
 public FirstCon() {
   IPreferences pref = Manager.getPreferences();
   uri = pref.getDatabaseHost();
   username = pref.getDatabaseUsername();
   password = pref.getDatabasePassword();
 }