@SuppressWarnings("unused")
  private void fetchTweets(String company) {
    Twitter twitter = new TwitterFactory().getInstance();

    Query query = new Query(company);
    query.setLang("en");
    query.setRpp(100);

    try {
      List<TweetDTO> tweets = new ArrayList<TweetDTO>();

      for (int i = 1; i < 10; i++) {
        query.setPage(i);
        QueryResult result = twitter.search(query);
        for (Tweet tweet : result.getTweets()) {
          TweetDTO dto = new TweetDTO();
          dto.setTwitterId(Long.toString(tweet.getId()));
          dto.setText(tweet.getText());
          dto.setDate(tweet.getCreatedAt());
          dto.setCompanies(company);
          tweets.add(dto);
        }
      }

      new GoogleTweetDAO().storeTweet(tweets);
    } catch (TwitterException e) {
    }
  }
Пример #2
0
  @Override
  public List<Status> loadInBackground() {

    try {
      // 大阪のWOEID
      // int osaka = 15015370;

      // トレンドを取得する
      // Trend[] trend = this.twitter.getPlaceTrends(osaka).getTrends();

      // 取得したトレンドから、ランダムで1つを選択する
      // Random rnd = new Random();
      // String q = trend[rnd.nextInt(trend.length)].getQuery();

      // 検索文字列を設定する
      Query query = new Query(keyword);
      query.setLocale("ja"); // 日本語のtweetに限定する
      query.setCount(100); // 最大20tweetにする(デフォルトは15)
      query.resultType(Query.RECENT);

      QueryResult result = null;

      // 最大1500件(15ページ)なので15回ループ
      for (int i = 1; i <= 15; i++) {
        result = twitter.search(query);
        System.out.println("ヒット数 : " + result.getTweets().size());
        System.out.println("ページ数 : " + new Integer(i).toString());

        /*
        // 検索結果を見てみる
        for (Status tweet : result.getTweets()) {
            // 本文
            String str = tweet.getText();
            java.util.Date hiduke = tweet.getCreatedAt();
            System.out.println(hiduke + str);
            // ハッシュタグとURLの削除

        }
        if (result.hasNext()) {
            query = result.nextQuery();
        } else {
            break;
        }
        */
        if (result.getTweets().size() < 100) {
          break;
        }
      }

      // 検索の実行
      // QueryResult result = this.twitter.search(query);

      return result.getTweets();

    } catch (TwitterException e) {
      Log.d("twitter", e.getMessage());
    }

    return null;
  }
  public static void printTweets(
      String keywords, String location, Integer limit, Boolean hideRetweets)
      throws TwitterException, URLs.ConnectionException, GeolocationSearch.SearchLocationException,
          MalformedURLException, GeolocationSearch.NoKeyException, URLs.HTTPQueryException {
    Twitter twitter = new TwitterFactory().getInstance();
    Query query = new Query(keywords);
    query.setGeoCode(getLocation(location), RADIUS, Query.KILOMETERS);
    Integer needTweets;
    Integer numberTweets = 0;
    if (limit == Integer.MAX_VALUE) {
      needTweets = NUMBER_TWEETS;
    } else {
      needTweets = limit;
    }
    QueryResult result;
    List<Status> tweets;

    do {
      result = twitter.search(query);
      tweets = result.getTweets();
      for (Status tweet : tweets) {
        if (!(hideRetweets && tweet.isRetweet()) && (numberTweets < needTweets)) {
          printTweet(tweet, false);
          numberTweets++;
        }
      }
      query = result.nextQuery();
    } while ((numberTweets < needTweets) && (query != null));

    if (numberTweets == 0) {
      System.out.println("Твитов по заданному запросу не найдено.");
    } else {
      System.out.println(SEPARATOR);
    }
  }
  /**
   * It returns MAX(available_tweets,200,limit) tweets from the search of the specified query. In
   * case of error, returns null.
   */
  public QueryResult searchTweets(String queryTerm, int limit) {

    try {
      Query query = new Query(queryTerm);
      query.setCount(limit);
      QueryResult result = twitter.search(query);
      LoggerClass.log(
          Level.INFO,
          "OK. Searching tweets for term "
              + queryTerm
              + " and limit "
              + limit
              + ". "
              + result.getCount()
              + " results.");
      return result;
    } catch (Exception ex) {
      LoggerClass.log(
          Level.SEVERE,
          "ERROR searching tweets for term " + queryTerm + " and limit " + limit + ".");
      System.err.println("[ERROR] Can't search tweets");
      ex.printStackTrace();
      return null;
    }
  }
  /**
   * Metodo que permite crear la consulta en tweeter en base al texto ingresado en el query.
   *
   * @param textoConsultaEjecutar
   * @return
   */
  public List<Status> ejecutarQueyEnBaseAConsulta(String textoConsultaEjecutar) {
    logger.info("<====  armaQueyEnBaseAConsulta ====>");
    logger.debug("textoConsultaEjecutar: " + textoConsultaEjecutar);

    Twitter twitter = TwitterFactory.getSingleton();
    Query query = new Query(textoConsultaEjecutar);
    query.setLang(COD_LENGUAJE_CONSULTA_ESPANOL);

    return ejecutaQueryEnTwitter(twitter, query);
  }
Пример #6
0
 @Override
 public List<Status> getStatuses(final Twitter twitter, final Paging paging)
     throws TwitterException {
   if (twitter == null) return null;
   final Query query = new Query(processQuery(mQuery));
   query.setRpp(paging.getCount());
   if (paging.getMaxId() > 0) {
     query.setMaxId(paging.getMaxId());
   }
   return Arrays.asList(twitter.search(query).getStatuses());
 }
Пример #7
0
 private static List<Status> buscarTag(String tag) throws TwitterException {
   TwitterFactory factory = new TwitterFactory();
   AccessToken accessToken = loadAccessToken();
   Twitter twitter = factory.getInstance();
   twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
   twitter.setOAuthAccessToken(accessToken);
   Query query = new Query(tag);
   query.setCount(100);
   QueryResult result = twitter.search(query);
   // printResultadosConsulta(result);
   return result.getTweets();
 }
 @Test
 public void simpleSetQueryTest() throws Exception {
   Parameters param = new Parameters();
   param.setQuery("cook");
   param.setStream(true);
   param.setLimit(40);
   param.setHideRetweets(true);
   param.setHelp(false);
   param.setPlace("Moscow");
   Query query = search.setQuery(param);
   assertEquals(query.getQuery(), "cook");
 }
Пример #9
0
  /**
   * Search tweets with the keyword "word"
   *
   * @param word : keyword
   */
  public void doSearch(String word) {
    List<Status> listStatus;
    listCleanTweets = new ArrayList<Tweet>();
    listDirtyTweets = new ArrayList<Tweet>();

    try {
      Query query = new Query(word);
      query.resultType(ResultType.mixed);
      query.setLang("fr");
      query.count(30);
      QueryResult result = InterfaceG.twitter.search(query);

      listStatus = result.getTweets();

      for (Status status : listStatus) {
        Tweet tclean =
            new Tweet(
                status.getId(),
                status.getUser().getName(),
                cleanTweet(status.getText()),
                status.getCreatedAt().toString(),
                -1);
        listCleanTweets.add(tclean);

        Tweet tdirty =
            new Tweet(
                status.getId(),
                status.getUser().getName(),
                status.getText(),
                status.getCreatedAt().toString(),
                -1);
        listDirtyTweets.add(tdirty);
      }

      setChanged();
      notifyObservers();
    } catch (TwitterException te) {
      System.out.println("doSearch:TwitterExc");
      System.out.println(te.getMessage());
      System.exit(-1);
    } catch (IOException e) {
      System.out.println("doSearch:IOExc");
      System.out.println(e.getMessage());
    }
  }
Пример #10
0
  /**
   * Usage: java twitter4j.examples.search.SearchTweets [query]
   *
   * @param args
   */
  public static void main(String[] args) {

    Twitter twitter = new TwitterFactory().getInstance();
    try {
      Query q = new Query("War");
      q.setRpp(100);
      QueryResult result = twitter.search(q);

      List<Tweet> tweets = result.getTweets();
      for (Tweet tweet : tweets) {
        System.out.println("@" + tweet.getFromUser() + " - " + tweet.getText());
      }
      System.exit(0);
    } catch (TwitterException te) {
      te.printStackTrace();
      System.out.println("Failed to search tweets: " + te.getMessage());
      System.exit(-1);
    }
  }
Пример #11
0
 public Set<Tweet> twFind(String term) {
   Set<Tweet> foundTweets = new HashSet<Tweet>();
   try {
     Query query = new Query(term);
     query.setRpp(RPP);
     for (int i = 1; i <= MAX_PAGES; i++) {
       query.setPage(i); // loads result page by page
       QueryResult result = twitter.search(query);
       List<Tweet> tweets = result.getTweets();
       for (Tweet tweet : tweets) {
         foundTweets.add(tweet);
       }
     }
   } catch (TwitterException te) {
     te.printStackTrace();
     System.out.println("Failed to search tweets: " + te.getMessage());
     System.exit(-1);
   }
   return foundTweets;
 }
Пример #12
0
  // Method to perform the actual query
  public List<Status> searchTweet(String inQuery) {
    Query query = new Query(inQuery);
    query.setCount(100); // Query Length
    List<Status> tweetList = new ArrayList<>();

    try {
      int count = 0;
      QueryResult queryResult;
      do {
        queryResult = twitter.search(query); // Perform Search

        tweetList = queryResult.getTweets(); // get the results and put in Array

        // keep printing as long as there are tweets in the Array
      } while ((query = queryResult.nextQuery()) != null && count <= LIMIT);
    }
    // if anything goes wrong print this line
    catch (TwitterException te) {
      System.out.println("Couldn't connect: " + te.getErrorMessage());
    }
    return tweetList;
  }
  public void doNext() {
    try {
      if (result == null) {

        Query query = new Query(this.keywords);
        query.setCount(tweetBatchCount);

        try {
          Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
        }

        result = twitter.search(query);

        i = 0;
      } else if (i >= tweetBatchCount - 1) {
        Query nextResult = result.nextQuery();

        if (nextResult == null) {
          finished = true;
        } else {
          result = twitter.search(nextResult);

          try {
            Thread.sleep(sleepTime);
          } catch (InterruptedException e) {

          }

          i = 0;
        }
      }

      activeResultSet = result.getTweets();

    } catch (TwitterException e) {
      e.printStackTrace();
    }
  }
  public void grab(Date timeLimit) {
    log.info("Twitter grabber started...");
    Twitter twitter = new TwitterFactory().getInstance();

    List<Brand> brandList = handler.getBrandProvider().getAllBrands();
    ArticleProvider articleProvider = handler.getArticleProvider();

    for (Brand b : brandList) {
      Query query = new Query(b.getName());
      query.setRpp(PAGE_SIZE);
      query.setLang("ru");
      query.setResultType(Query.MIXED);

      List<Tweet> resultTweets = new LinkedList<Tweet>();
      QueryResult queryResult;
      int pageNumber = 1;

      try {
        do {
          query.setPage(pageNumber);
          queryResult = twitter.search(query);
          resultTweets.addAll(queryResult.getTweets());
          pageNumber++;
          log.info(pageNumber);
        } while (ISSUANCE_SIZE > resultTweets.size());
      } catch (TwitterException e) {
        throw new RuntimeException(e);
      }
      log.info("tweets in da111y: " + resultTweets.size());
      twitter = null;
      twitter = new TwitterFactory().getInstance();

      for (Tweet t : resultTweets) {
        articleProvider.writeArticleToDataStore(
            new Article(-1, b.getId(), 2, "", t.getText(), "", getSimpleTime(t.getCreatedAt()), 1));
      }
    }
    log.info("twitter grabber finished succesful.");
  }
  private void getAndStoreTweetsOnTopic(String topic) {

    // Configuring twitter
    ConfigurationBuilder configBuilder = new ConfigurationBuilder();
    configBuilder.setOAuthAccessToken(ACCESS_TOKEN);
    configBuilder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
    configBuilder.setOAuthConsumerKey(CONSUMER_KEY);
    configBuilder.setOAuthConsumerSecret(CONSUMER_KEY_SECRET);

    Configuration configuration = configBuilder.build();

    TwitterFactory factory = new TwitterFactory(configuration);

    Twitter twitter = factory.getInstance();

    Query query = new Query(topic);

    // number of tweets to return per page
    query.setCount(100);

    try {

      // search for tweets on specifc topics
      QueryResult search = twitter.search(query);

      List<Status> tweets = search.getTweets();

      // initialize Mongo DB
      Mongo mongo = new Mongo(MONGO_DB_HOST, MONGO_DB_PORT);
      DB db = mongo.getDB(MONGO_DB_NAME);
      DBCollection collection = db.getCollection(MONGO_DB_COLLECTION);

      for (Status tweet : tweets) {

        System.out.println(getTweetInfo(tweet));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dateWithoutTime = sdf.parse(sdf.format(tweet.getCreatedAt()));

        // create a document
        BasicDBObject document = new BasicDBObject();
        document.put("createdAt", dateWithoutTime);
        document.put("user", tweet.getUser().getName());
        document.put("rTCount", tweet.getRetweetCount());
        document.put("tweet", tweet.getText());
        document.put("source", tweet.getSource());

        // store it in database
        collection.insert(document);
      }

      /**
       * // Find all records from collection DBCursor cursorDoc = collection.find();
       *
       * <p>// ... and display while (cursorDoc.hasNext()) { System.out.println(cursorDoc.next());
       * }*
       */
    } catch (TwitterException e) {
      e.printStackTrace();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }