コード例 #1
0
 public void updateTwitterStream(Location location, int radius) {
   Log.d(TAG, "updateTwitterStream()");
   double[][] mapRegion = GeoCalculationsHelper.getMapRegion(location, radius);
   FilterQuery filterQuery = new FilterQuery();
   filterQuery.locations(mapRegion);
   twitterStream.filter(filterQuery);
 }
コード例 #2
0
ファイル: TweetsRetriever.java プロジェクト: Lefteris008/EvS
  /**
   * Method that handles the Twitter streaming API. <br>
   * <b>WARNING:</b> Method does not terminate by itself, due to the fact that the streamer runs in
   * a different thread.
   *
   * @param keywords The keywords for which the streamer searches for tweets.
   * @param mongoDB A handler for the MongoDB database.
   * @param config A configuration object.
   */
  public final void retrieveTweetsWithStreamingAPI(
      String[] keywords, MongoHandler mongoDB, Config config) {

    ConfigurationBuilder cb = getAuthorization();
    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    final StatusListener listener;
    listener =
        new StatusListener() {

          @Override
          public final void onStatus(Status status) {
            // Insert tweet to MongoDB
            mongoDB.insertSingleTweetIntoMongoDB(status, "NULL");
          }

          @Override
          public final void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            PrintUtilities.printInfoMessageln(
                "Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
          }

          @Override
          public final void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            PrintUtilities.printInfoMessageln(
                "Got track limitation notice:" + numberOfLimitedStatuses);
          }

          @Override
          public final void onScrubGeo(long userId, long upToStatusId) {
            PrintUtilities.printInfoMessageln(
                "Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
          }

          @Override
          public final void onStallWarning(StallWarning warning) {
            PrintUtilities.printInfoMessageln("Got stall warning:" + warning);
          }

          @Override
          public final void onException(Exception ex) {
            ex.printStackTrace(System.out);
          }
        };

    FilterQuery fq = new FilterQuery();
    fq.language("en"); // Set language of tweets to "English"
    fq.track(keywords); // Load the search terms

    twitterStream.addListener(listener); // Start listening to the stream
    twitterStream.filter(fq); // Apply the search filters
  }
コード例 #3
0
  public static void main(String[] args) throws TwitterException {

    FilterQuery query = new FilterQuery();

    // wfrank 7897072
    // ponja 38299186

    String[] tracks = {"diegoram", "cfkargentina"};
    // query.track(tracks);

    long[] ids = {7897072L, 38299186L, 17760769L};

    query.follow(ids);

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    StatusListener listener =
        new StatusListener() {
          @Override
          public void onStatus(Status status) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
          }

          @Override
          public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println(
                "Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
          }

          @Override
          public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
          }

          @Override
          public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println(
                "Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
          }

          // @Override
          // public void onStallWarning(StallWarning warning) {
          //    System.out.println("Got stall warning:" + warning);
          // }

          @Override
          public void onException(Exception ex) {
            ex.printStackTrace();
          }
        };
    twitterStream.addListener(listener);
    twitterStream.filter(query);
  }
コード例 #4
0
  @Override
  public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    statuses = new LinkedBlockingQueue<Status>(1000);
    this.spoutOutputCollector = collector;

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder
        .setOAuthConsumerKey(consumerKey)
        .setOAuthConsumerSecret(consumerSecret)
        .setOAuthAccessToken(accessToken)
        .setOAuthAccessTokenSecret(accessTokenSecret);

    OAuthAuthorization authAuthorization = new OAuthAuthorization(configurationBuilder.build());

    twitterStream = new TwitterStreamFactory().getInstance(authAuthorization);
    twitterStream.addListener(
        new StatusListener() {
          @Override
          public void onStatus(Status status) {
            statuses.offer(status);
          }

          @Override
          public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}

          @Override
          public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}

          @Override
          public void onScrubGeo(long userId, long upToStatusId) {}

          @Override
          public void onStallWarning(StallWarning warning) {}

          @Override
          public void onException(Exception ex) {}
        });

    twitter = new TwitterFactory().getInstance(authAuthorization);
    filterQuery = new FilterQuery();

    if (filterQuery == null) {
      twitterStream.sample();
      ;
    } else {
      twitterStream.filter(filterQuery.track(filterWords));
      twitterStream.filter(filterQuery.language(filterLanguages));
    }
  }
コード例 #5
0
  /**
   * This is where the automagic happens We are listening on tweets through the streaming api,
   * whenever we get a tweet "fireStatusEvent" is called
   */
  @PostConstruct
  public void startTweetStream() {
    TwitterStream twitterStream =
        new TwitterStreamFactory(TwitterConfigBuilder.getConfig()).getInstance();

    StatusListener statusListener =
        new StatusListener() {
          @Override
          public void onStatus(Status status) {
            logger.info("Received a status from " + status.getUser().getScreenName());
            Tweet tweet = new Tweet(status);
            fireStatusEvent(tweet);
          }

          @Override
          public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}

          @Override
          public void onTrackLimitationNotice(int i) {}

          @Override
          public void onScrubGeo(long l, long l1) {}

          @Override
          public void onStallWarning(StallWarning stallWarning) {}

          @Override
          public void onException(Exception e) {
            e.printStackTrace();
          }
        };

    FilterQuery filter = new FilterQuery();
    String[] keywords = {"#testtestWOW"};
    filter.track(keywords);

    twitterStream.addListener(statusListener);
    twitterStream.filter(filter);
    logger.warn("Started to listen for " + keywords[0]);
  }
コード例 #6
0
 public static void streamTweets(String keywords, String location, Boolean hideRetweets)
     throws URLs.ConnectionException, URLs.HTTPQueryException, GeolocationSearch.NoKeyException,
         GeolocationSearch.SearchLocationException, MalformedURLException {
   twitter4j.TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
   twitterStream.addListener(
       new StatusAdapter() {
         public void onStatus(Status status) {
           if (!(status.isRetweet() && hideRetweets)) {
             printTweet(status, true);
           }
         }
       });
   Double[] coordinates = getCoordinatesByQuery(location);
   double[][] doubleCoordinates = {
     {coordinates[1] - RADIUS, coordinates[0] - RADIUS},
     {coordinates[1] + RADIUS, coordinates[0] + RADIUS}
   };
   String[] arrayKeywords = {keywords};
   FilterQuery fq = new FilterQuery();
   fq.locations(doubleCoordinates);
   fq.track(arrayKeywords);
   twitterStream.filter(fq);
 }
コード例 #7
0
 public FilterQuery getFilterQuery() {
   FilterQuery result = new FilterQuery();
   String[] trackingStrings = new String[trackingFilter.size()];
   trackingStrings = trackingFilter.toArray(trackingStrings);
   return result.track(trackingStrings);
 }
コード例 #8
0
  public void loadMenu() throws InterruptedException {

    System.out.print("Please choose a name for your stream:\t");

    Scanner input = new Scanner(System.in);
    String keyword = input.nextLine();

    connectdb(keyword);

    cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);
    cb.setOAuthConsumerKey("XXX");
    cb.setOAuthConsumerSecret("XXX");
    cb.setOAuthAccessToken("XXX");
    cb.setOAuthAccessTokenSecret("XXX");

    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    StatusListener listener =
        new StatusListener() {

          public void onStatus(Status status) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_name", status.getUser().getScreenName());
            basicObj.put("retweet_count", status.getRetweetCount());
            basicObj.put("tweet_followers_count", status.getUser().getFollowersCount());
            basicObj.put("source", status.getSource());
            // basicObj.put("coordinates",tweet.getGeoLocation());

            UserMentionEntity[] mentioned = status.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", status.getId());
            basicObj.put("tweet_text", status.getText());

            try {
              items.insert(basicObj);
            } catch (Exception e) {
              System.out.println("MongoDB Connection Error : " + e.getMessage());
            }
          }

          public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println(
                "Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
          }

          public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
          }

          public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println(
                "Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
          }

          @Override
          public void onStallWarning(StallWarning stallWarning) {
            // To change body of implemented methods use File | Settings | File Templates.
          }

          public void onException(Exception ex) {
            ex.printStackTrace();
          }
        };

    FilterQuery fq = new FilterQuery();
    String keywords[] = {"Germany"};

    fq.track(keywords);

    twitterStream.addListener(listener);
    twitterStream.filter(fq);
  }
コード例 #9
0
  public static void main(String args[]) {
    FilterQuery tweetFilterQuery = new FilterQuery();
    String a[] = {"en"};
    tweetFilterQuery.language(a);
    ConfigurationBuilder _configurationBuilder = new ConfigurationBuilder();
    _configurationBuilder
        .setOAuthConsumerKey(consumerKey)
        .setOAuthConsumerSecret(consumerSecret)
        .setOAuthAccessToken(accessToken)
        .setOAuthAccessTokenSecret(accessTokenSecret)
        .setJSONStoreEnabled(true);

    twitterStream = new TwitterStreamFactory(_configurationBuilder.build()).getInstance();

    StatusListener listener =
        new StatusListener() {

          @Override
          public void onException(Exception arg0) {
            System.out.println("Error occured: " + arg0.getMessage());
            arg0.printStackTrace();
          }

          @Override
          public void onTrackLimitationNotice(int arg0) {
            System.out.println("Track limitation notice for " + arg0);
          }

          @Override
          public void onStatus(Status status) {
            // if(status.getLang().equals("en"))
            //	System.out.println(status.getText().replaceAll("[\n\r]", ""));
            String filename = totalCount / 100 + ".txt";
            File f = new File(filename);
            PrintWriter out = null;

            if (f.exists() && !f.isDirectory()) {
              // System.out.println("@" + status.getUser().getScreenName() + " - " +
              // status.getText() + " -> "+ status.getCreatedAt());
              // String mapstring = status.getUser().getScreenName() + " - " + status.getText() + "
              // -> "+ status.getCreatedAt() +"\n";
              String mapstring = status.getText().replace("\n", " ") + "\n";

              try {
                out = new PrintWriter(new FileOutputStream(new File(filename), true));
              } catch (FileNotFoundException e) {
                e.printStackTrace();
              }
              out.append(mapstring);
              out.close();
            } else {

              try {
                out = new PrintWriter(filename);
              } catch (FileNotFoundException e) {
                e.printStackTrace();
              }
              String mapstring = status.getText().replace("\n", " ") + "\n";
              out.println(mapstring);
              out.close();
            }
            totalCount++;
          }

          @Override
          public void onStallWarning(StallWarning arg0) {
            // TODO Auto-generated method stub

          }

          @Override
          public void onScrubGeo(long arg0, long arg1) {
            // TODO Auto-generated method stub

          }

          @Override
          public void onDeletionNotice(StatusDeletionNotice arg0) {
            // TODO Auto-generated method stub

          }
        };
    twitterStream.addListener(listener);

    twitterStream.sample();
  }