List of usage examples for twitter4j FilterQuery FilterQuery
public FilterQuery()
From source file:twitterapp.TwitterApp.java
public static void streamTweets() throws TwitterException { /*getting the trends */ ConfigurationBuilder cb2 = new ConfigurationBuilder(); cb2.setDebugEnabled(true).setOAuthConsumerKey("S01GsVwuCAwZFp5BLg5C4k8PT") .setOAuthConsumerSecret("6jo0jo4b05Ec5ZJcf74v5yGUQu5v8DryUwypOBjPD6jaItRNzd") .setOAuthAccessToken("794259549297446912-Z3AWruBmLa7QmCO6BnybCSj1tZXNqbB") .setOAuthAccessTokenSecret("6ezMQPQVziW9yxyTITZA8Wc2RJWjcBKvbXZU4dOjo4bge"); TwitterFactory tf = new TwitterFactory(cb2.build()); Twitter twitter = tf.getInstance();/* ww w.j av a 2s .co m*/ Trends trends = twitter.getPlaceTrends(23424977); String top_trend = ""; int top = 0; for (Trend trend : trends.getTrends()) { if (top < 1) { top_trend = trend.getName(); top++; } } System.out.println("top trend : " + top_trend); //Using the Streaming API to get real time tweets based on the trending topics as keywords /* configurating twiter4j */ ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("S01GsVwuCAwZFp5BLg5C4k8PT") .setOAuthConsumerSecret("6jo0jo4b05Ec5ZJcf74v5yGUQu5v8DryUwypOBjPD6jaItRNzd") .setOAuthAccessToken("794259549297446912-Z3AWruBmLa7QmCO6BnybCSj1tZXNqbB") .setOAuthAccessTokenSecret("6ezMQPQVziW9yxyTITZA8Wc2RJWjcBKvbXZU4dOjo4bge") .setJSONStoreEnabled(true); /* end of configuration */ MongoClient mongo = new MongoClient("localhost", 27017); MongoDatabase database = mongo.getDatabase("myTweetdb2"); MongoCollection<Document> collection = database.getCollection("myTweetCol5"); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener; listener = new StatusListener() { @Override public void onStatus(Status status) { String rawJSON = TwitterObjectFactory.getRawJSON(status); Document doc = Document.parse(rawJSON); collection.insertOne(doc); } @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.sample(); twitterStream.addListener(listener); FilterQuery fq = new FilterQuery(); String keywords[] = { top_trend }; fq.track(keywords); twitterStream.filter(fq); }
From source file:TwitterCrawler.PrintSampleStream.java
License:Apache License
public static void main(String[] args) throws TwitterException { System.getProperties().put("http.proxyHost", "127.0.0.1"); System.getProperties().put("http.proxyPort", "8580"); //final PrintSampleStream pr = new PrintSampleStream(); try {// www.ja va2s .co m pr.LinkMongodb(); } catch (Exception e) { e.printStackTrace(); } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("7ZVgfKiOvBDcDFpytRWSA") .setOAuthConsumerSecret("JmeJVeym78arzmGthrDUshQyhkq6nWA9tWLUKxc") .setOAuthAccessToken("321341780-Zy7LptVYBZBVvAeQ5GFJ4aKFw8sdqhWBnvA3pDuO") .setOAuthAccessTokenSecret("foi8FnQCeN0J5cdwad05Q6d7dbytFayQn1ZOvmhF6Qc"); cb.setJSONStoreEnabled(true); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { @Override public void onException(Exception ex) { // TODO Auto-generated method stub ex.printStackTrace(); } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // TODO Auto-generated method stub System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onScrubGeo(long userId, long upToStatusId) { // TODO Auto-generated method stub System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStatus(Status status) { //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); //System.out.println(status); String str = DataObjectFactory.getRawJSON(status); try { //JSONObject nnstr = new JSONObject(newstr); DBObject dbObject = (DBObject) JSON.parse(str); pr.collection.insert(dbObject); System.out.println(dbObject); pr.count++; /* if(pr.count>300000) { pr.mongo.close(); System.exit(0); } */ } catch (Exception e) { e.printStackTrace(); } } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { // TODO Auto-generated method stub System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; twitterStream.addListener(listener); //String[] Track = {"why Obama look like he about to drop the best album of 2013?"}; String[] trackArray = { "Mac", "App", "Store" }; //trackArray[1] = ; // Create a FilterQuery object and then set the items to track FilterQuery filter = new FilterQuery(); // Create an array of items to track //String[] itemsToTrack = {""}; // Set the items to track using FilterQuerys' track method. //filter.track(Track); filter.track(trackArray); // Assuming you have already created Twitter/TwitterStream object, // use the filter method to start streaming using the FilterQuery object just created. twitterStream.filter(filter); /* String[] username = {"katyperry"}; twitterStream.addListener(userlistener); twitterStream.user();*/ //pr.mongo.close(); }
From source file:twittermongodbapp.CollectTweets.java
static public void getTopTrends(twitter4j.Twitter twitter, TwitterStream twitterStream, StatusListener listener) {//ww w.ja v a 2s . co m FilterQuery filterQuery = new FilterQuery(); Timer t = new Timer(); t.scheduleAtFixedRate(new TimerTask() { @Override public void run() { Trends trends; try { trends = twitter.getPlaceTrends(23424833);//trends = twitter.getPlaceTrends(1);(23424833) String[] keywords = new String[trends.getTrends().length]; System.out.println("Top Trends in Greece"); for (int i = 0; i < trends.getTrends().length; i++) { keywords[i] = trends.getTrends()[i].getName(); System.out.println(keywords[i]); } filterQuery.track(keywords); filterQuery.language(new String[] { "el" }); twitterStream.addListener(listener); twitterStream.filter(filterQuery); } catch (TwitterException ex) { Logger.getLogger(TwitterMongoDBApp.class.getName()).log(Level.SEVERE, null, ex); } } }, 1000, 360000); }
From source file:twitter_app_p1.Twitter_app_p1.java
public static void GetTweets() { twitterStream.addListener(listener); FilterQuery tweet_filter = new FilterQuery(); double[][] location = { { -124, 24 }, { -66, 49 } }; //continental US tweet_filter.locations(location);/*from ww w . j a va2 s. c o m*/ twitterStream.filter(tweet_filter); }
From source file:twittynumnum.FutureListener.java
public void listen(String[] keywords) { StatusListener listener = new StatusListener() { @Override/*from w ww.java 2s .c o m*/ public void onException(Exception arg0) { } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onStatus(Status status) { User user = status.getUser(); String username = user.getScreenName(); String profileLocation = user.getLocation(); long tweetId = status.getId(); String content = status.getText(); Date tweetDate = status.getCreatedAt(); out.print(tweetId + "\t" + tweetDate + "\t" + username + "\t" + profileLocation + "\t" + content.replaceAll("\n", "/n") + "\n"); } @Override public void onTrackLimitationNotice(int arg0) { } @Override public void onStallWarning(StallWarning sw) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }; FilterQuery filter = new FilterQuery(); filter.track(keywords); twitterStream.addListener(listener); twitterStream.filter(filter); }
From source file:uk.ac.susx.tag.method51.twitter.params.FilterQueryParams.java
License:Apache License
public FilterQuery buildFilterQuery() { if (getKeywords().size() == 0 && getFollowIds().size() == 0 && getLocations().size() == 0) throw new RuntimeException("No keywords or user ids provided"); final FilterQuery query = new FilterQuery(); if (!getLanguages().isEmpty()) { LOG.info("Languages: {}", getLanguages()); query.language(getLanguages().toArray(new String[getLanguages().size()])); }/*from ww w. j a va2 s. c o m*/ if (!getKeywords().isEmpty()) { LOG.info("Tracking keywords: {}", getKeywords()); query.track(getKeywords().toArray(new String[getKeywords().size()])); } if (!getFollowIds().isEmpty()) { LOG.info("Tracking ids: {}", getFollowIds()); query.follow(ArrayUtil.unbox(getFollowIds().toArray(new Long[getFollowIds().size()]))); } if (!getLocations().isEmpty()) { LOG.info("Locations co-ords: {}", getLocations()); query.locations(toArray(getLocations())); } return query; }
From source file:uk.co.flax.ukmp.twitter.ManagedTwitterClient.java
License:Apache License
private void updateFilterIds(List<Long> updatedIds) { if (!updatedIds.equals(filterIds)) { LOGGER.debug("Updating filter IDs"); if (this.filterIds != null) { // Stop the stream listener stream.cleanUp();/* w w w. j a v a2 s. c o m*/ } // Convert filterIds to long[] long[] ids = new long[updatedIds.size()]; for (int i = 0; i < updatedIds.size(); i++) { ids[i] = updatedIds.get(i); } FilterQuery filterQuery = new FilterQuery().follow(ids); // Start filtering by the new query stream.filter(filterQuery); this.filterIds = updatedIds; } }
From source file:wordgame.WordGame.java
public static void initGame(String file) throws FileNotFoundException { Scanner s = new Scanner(new File(file)); //open the file ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true) //populate the twitter details with the proper API informatin .setOAuthConsumerKey(s.nextLine()).setOAuthConsumerSecret(s.nextLine()) .setOAuthAccessToken(s.nextLine()).setOAuthAccessTokenSecret(s.nextLine()); TwitterFactory tf = new TwitterFactory(cb.build()); t = tf.getInstance();//from w ww . j a v a 2 s . c o m TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory(t.getConfiguration()); TwitterStream twitterStream = twitterStreamFactory.getInstance(); FilterQuery filterQuery = new FilterQuery(); filterQuery.follow(new long[] { 731852008030916608L }); //Track our tweets twitterStream.addListener(new MentionListener()); //Set the listener to our MentionListener class twitterStream.filter(filterQuery); //begin listening }
From source file:xdsoft.cloudera.flume.twitter.source.TwitterSource.java
License:Apache License
/** * Start processing events. This uses the Twitter Streaming API to sample * Twitter, and process tweets.//from w w w . j av a 2s . c om */ @Override public void start() { // The channel is the piece of Flume that sits between the Source and // Sink, // and is used to process events. final ChannelProcessor channel = getChannelProcessor(); final Map<String, String> headers = new HashMap<String, String>(); // The StatusListener is a twitter4j API, which can be added to a // Twitter // stream, and will execute methods every time a message comes in // through // the stream. StatusListener listener = new StatusListener() { // The onStatus method is executed every time a new tweet comes in. public void onStatus(Status status) { // The EventBuilder is used to build an event using the headers // and // the raw JSON of a tweet logger.debug(status.getUser().getScreenName() + ": " + status.getText()); headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime())); Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status).getBytes(), headers); channel.processEvent(event); } // This listener will ignore everything except for new tweets public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onScrubGeo(long userId, long upToStatusId) { } public void onException(Exception ex) { } public void onStallWarning(StallWarning warning) { } }; logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}", new String[] { consumerKey, accessToken }); // Set up the stream's listener (defined above), twitterStream.addListener(listener); // Set up a filter to pull out industry-relevant tweets if (keywords.length == 0) { logger.debug("Starting up Twitter sampling..."); twitterStream.sample(); } else { logger.debug("Starting up Twitter filtering..."); FilterQuery query = new FilterQuery().track(keywords); twitterStream.filter(query); } super.start(); }