Package it.twiskex.services

Source Code of it.twiskex.services.SeachNStoreByTag

package it.twiskex.services;

import it.twiskex.dal.DALService;
import it.twiskex.dal.entities.SearchNTT;
import it.twiskex.dal.entities.TweetNTT;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;

public class SeachNStoreByTag extends TwiSKExHttpServlet {

  /**
   *
   */
  private static final long serialVersionUID = 8004488480163629327L;
  private static final Logger logger = Logger
      .getLogger(SeachNStoreByTag.class.getName());

  /**
   * Each time this service starts "pops" the oldest search stored executes
   * another burst of search "pushes" the new search with the updated status
   */
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    SearchNTT currentSearch = null;

    /*
     * if (req.getParameterMap().containsKey(CURRENT_SEARCH)) { Long id =
     * Long.parseLong(req.getParameter(CURRENT_SEARCH)); currentSearch =
     * DALService.ofy().get(SearchNTT.class, id); } else { String hashtag =
     * req.getParameter(HASHTAG); currentSearch = new SearchNTT(hashtag); }
     */
    currentSearch = popSearch();

    if (currentSearch != null) {
      logger.warning("-- Searching for " + currentSearch.getHashtag());
      Twitter twitter = TwitterFactory.getSingleton();
      Query query = new Query(currentSearch.getHashtag());
      query.setCount(100);
      //max o since?
      //that is
      //older than or newer than?
      if (currentSearch.isFirstScan())
        query.setMaxId(currentSearch.getMaxID());
      else
        query.setSinceId(currentSearch.getSinceID());
      QueryResult result;
      try {
        result = twitter.search(query);
        int size = result.getTweets().size();
        logger.warning("-- Going to iterate on "+ size + " tweets");
        if (size==0) {
          logger.warning("-- REACHED "+ size + " tweets");
          return;
        }
        else if(size==1)
        {
          logger.warning("-- REACHED "+ size + " tweets");
          currentSearch.setFirstScan(false);
          pushSearch(currentSearch);
          return;
        }
        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
         
          System.out.println("@" + tweet.getUser().getScreenName()
              + " - " + tweet.getText());
          logger.warning("--  "+ tweet.getText());
          TweetNTT t = new TweetNTT(tweet);
          DALService.ofy().put(t);

          // need to keep track of minimum and maximum id
          long tweetid = tweet.getId();
          if (tweetid < currentSearch.getMaxID())
            currentSearch.setMaxID(tweetid);
          if (tweetid >= currentSearch.getSinceID())
            currentSearch.setSinceID(tweetid);
        }
        logger.warning("-- Storing search "+currentSearch.getHashtag());
        pushSearch(currentSearch);

      } catch (TwitterException e) {
        e.printStackTrace();
      } catch (Exception e) {
        logger.severe(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> doGet");
        logger.severe(e.toString());
      }
    }
  }

  @SuppressWarnings("unused")
  private void enqueueNewSearch(SearchNTT currentSearch) {
    TaskOptions taskoptions = TaskOptions.Builder
        .withUrl("/search_n_store_by_tag")
        .param(CURRENT_SEARCH, "" + currentSearch.getId())
        .method(TaskOptions.Method.GET);
    Queue queue = QueueFactory.getQueue(SEARCH_QUEUE);
    queue.add(taskoptions);
  }

  private void pushSearch(SearchNTT currentSearch) {
   
    try {
      DALService.ofy().put(currentSearch);
    } catch (Exception e) {
      logger.severe(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> pushSearch");
      logger.severe(e.toString());
    }
  }

  private SearchNTT popSearch() {
    com.googlecode.objectify.Query<SearchNTT> s = DALService.ofy()
        .query(SearchNTT.class).order("-lastUpdate").limit(1);
    SearchNTT result = s.get();
    DALService.ofy().delete(result);
    return result;
  }
}
TOP

Related Classes of it.twiskex.services.SeachNStoreByTag

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.