@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) {
    }
  }
Exemplo n.º 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 main(String[] args) throws TwitterException, IOException {
   Twitter twitter = TwitterFactory.getSingleton();
   JLanguageTool langTool = new JLanguageTool(new AmericanEnglish());
   List<String> twts = new ArrayList<String>();
   for (String arg : args) {
     Query query = new Query(arg);
     QueryResult result;
     int counter = 0;
     do {
       result = twitter.search(query);
       List<Status> tweets = result.getTweets();
       for (Status tweet : tweets) {
         if (isEligible(tweet)) {
           System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
           System.out.println(tweet.getLang());
           twts.add(tweet.getText());
           counter++;
         }
       }
     } while ((query = result.nextQuery()) != null && counter < 5);
   }
   for (String str : twts) {
     List<RuleMatch> matches = langTool.check(str);
     for (RuleMatch match : matches) {
       System.out.println(
           "Potential error at line "
               + match.getLine()
               + ", column "
               + match.getColumn()
               + ": "
               + match.getMessage());
       System.out.println("Suggested correction: " + match.getSuggestedReplacements());
     }
   }
 }
  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;
    }
  }
Exemplo n.º 6
0
  public void search() throws TwitterException {

    Twitter twitter = TwitterFactory.getSingleton();
    Query query = new Query("Cruyff");
    QueryResult result = twitter.search(query);
    for (Status status : result.getTweets()) {
      System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }
  }
  @Before
  public void setUp() throws Exception {
    when(geoResolver.resolvePlaceLocation("Moscow")).thenReturn(MoscowLocation);

    aMoscowSettings = new JCommanderSetting();

    new JCommander(aMoscowSettings, "-q", "a", "-p", "Moscow");

    when(twitter.search(argThat(hasProperty("query", equalTo("a")))))
        .thenThrow(new TwitterException("connection error"));
  }
Exemplo n.º 8
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());
 }
Exemplo n.º 9
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();
 }
  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();
    }
  }
Exemplo n.º 11
0
  public static void main(String[] args) throws TwitterException {
    // The factory instance is re-useable and thread safe.
    Twitter twitter = TwitterFactory.getSingleton();
    Query query = new Query("ionic2");
    QueryResult result = twitter.search(query);
    for (Status status : result.getTweets()) {
      System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }

    List<Status> statuses = twitter.getUserTimeline("Ionicframework");
    System.out.println("Showing Ionicframework timeline.");
    for (Status status : statuses) {
      System.out.println(status.getUser().getName() + ":" + status.getText());
    }
  }
  /**
   * Method for fetching tweets from Twitter
   *
   * @param search
   * @return List<String>
   */
  public static List<String> getTweets(String search) {

    ArrayList<String> tweetString = new ArrayList<String>();
    try {
      Query query = new Query(search);
      List<Status> list = twitter.search(query).getTweets();
      for (int i = 0; i < list.size(); i++) {
        twitter4j.Status each = (twitter4j.Status) list.get(i);
        tweetString.add(each.getUser().getName() + ":" + "\n" + each.getText());
      }
    } catch (Exception ex) {
      Log.d("SN", ex.getMessage());
    }
    return tweetString;
  }
  /**
   * Metodo que permite ejecutar la sentencia de consulta a tweeter
   *
   * @param twitter
   * @param polaridadIndicada
   */
  private List<Status> ejecutaQueryEnTwitter(Twitter twitter, Query query) {
    logger.debug("<====  Inicio Method ejecutaQueryEnTwitter ====>");
    List<Status> tweets = null;
    try {

      QueryResult result = twitter.search(query);
      logger.debug("Count : " + result.getTweets().size());

      tweets = result.getTweets();

    } catch (TwitterException e) {
      logger.error("Error al procesar la consulta a tweeter: ", e);
      throw new RuntimeException("Error al procesar la consulta a tweeter: ", e);
    }
    return tweets;
  }
Exemplo n.º 14
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);
    }
  }
Exemplo n.º 15
0
  private String getLatestTweet(String searchingFor) {

    Twitter twitter = new TwitterFactory().getInstance();
    AccessToken accessToken =
        new AccessToken(
            "2453751158-IVD2VGZsvwZiRKxNe3Gs2lMjg30nvSkV1xSuPFf",
            "vBa5PjKfuMTK1LLBG51nCUI9r5d6Ph5cAHrS73spg6Nvu");
    twitter.setOAuthConsumer(
        "YqeZdD2hYxOKv4QOkmp0i2djN", "6XUB1r8KXBvd8Pk9HHW3NgphMxHvHWBLAr5TihnckMU0ttyGST");
    twitter.setOAuthAccessToken(accessToken);

    Query query = new Query(searchingFor);
    try {
      QueryResult result = twitter.search(query);
      return result.getTweets().get(0).getText();
    } catch (Exception e) {
      System.err.print(e.getMessage());
      return "What the heck is that?";
    }
  }
Exemplo n.º 16
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;
 }
  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.");
  }
Exemplo n.º 18
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;
  }
Exemplo n.º 19
0
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // Get the query and resultLimit parameters we are going to use for fetching the tweets.
    Object queryObj = "google";
    Object resultsLimit = "3";

    //				if(queryObj == null) {
    //					return;
    //				}

    int limit = 5;

    if (resultsLimit != null) {
      try {
        limit = Integer.parseInt((String) resultsLimit);
      } catch (NumberFormatException e) {
        // no need for action
      }
    }

    String query = (String) queryObj;

    Twitter twitter = new TwitterFactory().getInstance();
    try {
      // Search for the result tweets using the parameter query.
      Query queryTweet = new Query(query);
      QueryResult searchResult = twitter.search(queryTweet);

      List<Tweet> tweets = searchResult.getTweets();

      if (tweets.size() >= limit) {
        tweets = tweets.subList(0, limit);
      }

      Long lastTweetId = null;
      try {
        lastTweetId = Long.parseLong((String) request.getParameter("lastTweetId"));
      } catch (NumberFormatException e) {
        lastTweetId = 0L;
      }

      // Iterate over the tweets and extract the information from each of them.
      List<TwitterHolder> tweetTexts = new ArrayList<TwitterHolder>();
      for (Tweet tweet : tweets) {

        if (lastTweetId > 0 && tweet.getId() <= lastTweetId.longValue()) {
          continue;
        }

        String text = tweet.getText();

        // Find URLs in the tweet text and replace them with a html link to the founded URL.
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
          String group = matcher.group();
          StringBuilder replacedUrl =
              new StringBuilder("<a href=\"")
                  .append(group)
                  .append("\">")
                  .append(group)
                  .append("</a>");
          text = text.replaceAll(Pattern.quote(group), replacedUrl.toString());
        }

        Date publishedDate = tweet.getCreatedAt();
        StringBuffer authorLink =
            new StringBuffer("http://twitter.com/#!/").append(tweet.getFromUser());

        /* Create TweetHolder instance and add it to the result list of TweetHolder instances.
         * That's what is going to the template for rendering. */
        tweetTexts.add(
            new TwitterHolder(
                text,
                tweet.getFromUser(),
                authorLink.toString(),
                new PrettyTime(new Locale("en")).format(publishedDate),
                tweet.getProfileImageUrl(),
                tweet.getSource(),
                String.valueOf(tweet.getId())));
      }

      Collections.reverse(tweetTexts);

      String json = new Gson().toJson(tweetTexts);

      // Add the result collection of TweetHolder instances to the response.
      response.setCharacterEncoding("UTF-8");
      response.setContentType("application/json");
      response.getWriter().append(json);
    } catch (TwitterException e) {
      response.setCharacterEncoding("UTF-8");
      response.setContentType("application/json");
      response.getWriter().append("[]");

      e.printStackTrace();

      //					return true;
      return;
    }
    //				return true;
  }
Exemplo n.º 20
0
  public static int searchForTwits(int id, String text, Connection conn, String getDate)
      throws TwitterException, SQLException, LangDetectException, ParseException {
    Twitter twitter = new TwitterFactory().getInstance();
    int countTweets = 0;
    int pageNumber = 1;
    int n = 0;

    do {
      Query query = new Query(text).rpp(100).page(pageNumber);
      QueryResult result = twitter.search(query);
      for (Tweet tweet : result.getTweets()) {

        java.util.Date date = tweet.getCreatedAt();
        Format formatter;
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDate = formatter.format(date);

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date oldDate = df.parse(getDate);

        Statement input = conn.createStatement();

        countTweets++;

        if (date.after(oldDate)) {
          Detector detector = DetectorFactory.create();
          detector.append(tweet.getText());
          String lang;
          try {
            lang = detector.detect();

            if (lang.equals("lv") || lang.equals("ru")) {
              try {
                input.executeUpdate(
                    "INSERT INTO tweet "
                        + "VALUES (null, '"
                        + tweet.getId()
                        + "', '"
                        + tweet.getFromUser()
                        + "', '"
                        + tweet.getText().replace("'", "&rsquo;")
                        + "', '"
                        + newDate
                        + "', null, null, null)");
                input.executeUpdate(
                    "INSERT INTO tweet_brand "
                        + "VALUES (null, '"
                        + tweet.getId()
                        + "', '"
                        + id
                        + "')");
              } catch (SQLException ex) {
              }
              n++;
            } else continue;

          } catch (LangDetectException ex) {
          }
        } else continue;
      }
      pageNumber++;
      if (countTweets == 100) countTweets = 0;
      else break;
    } while (pageNumber != 16);

    System.out.print(text + " ");
    return n;
  }
  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();
    }
  }