List of usage examples for twitter4j FilterQuery FilterQuery
public FilterQuery()
From source file:com.twitter.tokyo.kucho.daemon.Daemon.java
License:Apache License
public static void main(String[] args) { TwitterStream stream = TwitterStreamFactory.getSingleton(); SeatingList seatingList = new SeatingList(); stream.addListener(new KuchoController(EHillsImpl.getInstance(), seatingList)); String[] trackTags = new String[COLD.length + HOT.length]; System.arraycopy(COLD, 0, trackTags, 0, COLD.length); System.arraycopy(HOT, 0, trackTags, COLD.length, HOT.length); FilterQuery query = new FilterQuery().track(trackTags); logger.info("Starting."); stream.filter(query);/*from ww w. j av a2s . c o m*/ logger.info("Started."); while (true) { try { Thread.sleep(1000000); } catch (InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }
From source file:com.utad.flume.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. jav a 2s. co m */ @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); List<Event> events = Arrays.asList(event); channel.processEventBatch(events); } // 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(); }
From source file:com.waves_rsp.ikb4stream.datasource.twitter.TwitterProducerConnector.java
License:Open Source License
/** * Listen tweets from twitter with a bounding box and load them with the data producer object * * @param dataProducer {@link IDataProducer} contains the data queue * @see TwitterProducerConnector#confBuilder * @see TwitterProducerConnector#boundingBox *///from www. j av a2 s .c o m @Override public void load(IDataProducer dataProducer) { Objects.requireNonNull(dataProducer); TwitterStream twitterStream = null; try { TwitterStreamListener streamListener = new TwitterStreamListener(dataProducer); twitterStream = new TwitterStreamFactory(confBuilder.build()).getInstance(); FilterQuery filterQuery = new FilterQuery(); filterQuery.locations(boundingBox); twitterStream.addListener(streamListener); twitterStream.filter(filterQuery); Thread.currentThread().join(); } catch (IllegalArgumentException | IllegalStateException err) { LOGGER.error("Error loading : " + err.getMessage()); throw new IllegalStateException(err.getMessage()); } catch (InterruptedException e) { LOGGER.info("Close twitter"); Thread.currentThread().interrupt(); } finally { if (twitterStream != null) { twitterStream.cleanUp(); twitterStream.shutdown(); } Thread.currentThread().interrupt(); } }
From source file:com.wso2.stream.connector.protocol.TwitterPollingConsumer.java
License:Open Source License
/** * Setting up a connection with Twitter Stream API with the given credentials *///from w w w .ja v a 2 s.c o m private void setupConnection() { StatusListener listener = new StatusListenerImpl(); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setDebugEnabled(true).setOAuthConsumerKey(consumerKey) .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken) .setOAuthAccessTokenSecret(accessSecret); TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(); twitterStream.addListener(listener); FilterQuery query = new FilterQuery(); query.language(new String[] { "en" }); query.track(filterTags); twitterStream.filter(query); }
From source file:com.yahoo.labs.samoa.sentinel.model.TwitterStreamAPIReader.java
License:Apache License
public FilterQuery getFilterQuery(String[] trackAll) { FilterQuery filterQuery = new FilterQuery(); filterQuery.track(trackAll); return filterQuery; }
From source file:com.yoshio3.javaee7.sampleapp.TwitterStreamImpl.java
public void initTwitterStream(Session session, String filterString) { this.session = session; //Twitter Stream ?? twStream = TwitterStreamFactory.getSingleton(); FilterQuery filter = new FilterQuery(); filter.track(new String[] { filterString }); filter.language(new String[] { "ja" }); twStream.addListener(this); twStream.filter(filter);/*from w ww. j a v a 2 s . c om*/ }
From source file:crawlertwitter.TwitterDataStream.java
public void startTwitter(String[] keywords) { for (int i = 0; i < keywords.length; i++) { this.label += "_" + keywords[i]; }/* w ww. j a v a 2 s . co m*/ twitterStream.addListener(listener); System.out.println("Starting down Twitter stream..."); FilterQuery query = new FilterQuery(); // query.track(keywords); query.language(this.lang); // by filter twitterStream.filter(query); // twitterStream.sample(); }
From source file:crawling.FoundUsersByStreamHashtag.java
License:Apache License
public static void main(String[] args) throws TwitterException { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { // my shutdown code here // Record the user status idOut.println("Final status -------------------:"); for (Long id : discoveredUsers.keySet()) { idOut.println(id + "," + discoveredUsers.get(id)); }/* w w w.ja v a2 s . c o m*/ idOut.close(); } }); if (args.length != 1) { System.out.println("Usage: java twitter4j.examples.PrintFilterStreamHashtag hashtag"); System.exit(-1); } /*if(args.length == 2){ // Preload the collected user IDs preloadID(args[1]); }*/ //buildStartTime(); File file = new File(fileName); InputStream is = null; try { if (file.exists()) { is = new FileInputStream(file); prop.load(is); } else { System.out.println(fileName + " doesn't exist!"); System.exit(-1); } } catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); } StatusListener listener = new StatusListener() { public void onStatus(Status status) { //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); CheckUser(status); /*storeATweet(status); tweets += 1; if (tweets % 1000 == 0){ System.out.println("We now have tweets: " + tweets); }*/ } private void CheckUser(Status status) { // TODO Auto-generated method stub Long id = status.getUser().getId(); String username = status.getUser().getScreenName(); String realname = status.getUser().getName(); String text = status.getText(); Date date = status.getCreatedAt(); if (discoveredUsers.containsKey(id)) { //System.out.println("Already found this user: " + id); long num = discoveredUsers.get(id); discoveredUsers.put(id, num + 1); } else { discoveredUsers.put(id, (long) 1); storeUserID(status); } } 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); } public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; try { FileWriter outFile = new FileWriter("discoveredUser" + args[0] + ".txt", true); idOut = new PrintWriter(outFile); //out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); TwitterStream twitterStream = getOAuthTwitterStream(); twitterStream.addListener(listener); FilterQuery query = new FilterQuery(); String[] track = { args[0] }; //String[] track = {"#BaltimoreRiots"}; query.track(track); twitterStream.filter(query); }
From source file:crawling.PrintFilterStreamGeo.java
License:Apache License
public static void main(String[] args) throws TwitterException { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { /* my shutdown code here */ // Record the user status idOut.println("Final status -------------------:"); for (Long id : discoveredUsers.keySet()) { idOut.println(id + "," + discoveredUsers.get(id)); }/*from w w w. j ava2 s .c o m*/ idOut.close(); } }); if (args.length < 5) { System.out.println("Usage: java twitter4j.examples.PrintFilterStreamGeo long1 lati1 long2 lati2 CITY"); System.exit(-1); } if (args.length == 6) { // Preload the collected user IDs preloadID(args[5]); } File file = new File(fileName); InputStream is = null; try { if (file.exists()) { is = new FileInputStream(file); prop.load(is); } else { System.out.println(fileName + " doesn't exist!"); System.exit(-1); } } catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); } StatusListener listener = new StatusListener() { public void onStatus(Status status) { //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); CheckUser(status); } private void CheckUser(Status status) { // TODO Auto-generated method stub Long id = status.getUser().getId(); /*String username = status.getUser().getScreenName(); String realname = status.getUser().getName(); String text = status.getText(); Date date = status.getCreatedAt();*/ if (discoveredUsers.containsKey(id)) { //System.out.println("Already found this user: " + id); long num = discoveredUsers.get(id); discoveredUsers.put(id, num + 1); } else { discoveredUsers.put(id, (long) 1); storeUserID(status); } } 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); } public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; try { FileWriter outFile = new FileWriter("discoveredUser" + args[4] + ".txt", true); idOut = new PrintWriter(outFile); //out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); TwitterStream twitterStream = getOAuthTwitterStream(); twitterStream.addListener(listener); /*ArrayList<Long> follow = new ArrayList<Long>(); ArrayList<String> track = new ArrayList<String>(); long[] followArray = new long[follow.size()]; for (int i = 0; i < follow.size(); i++) { followArray[i] = follow.get(i); } String[] trackArray = track.toArray(new String[track.size()]);*/ // Geographic location double[][] location = new double[2][2]; //location[0] = new double[4]; //location[1] = new double[4]; location[0][0] = Double.parseDouble(args[0]); location[0][1] = Double.parseDouble(args[1]); location[1][0] = Double.parseDouble(args[2]); location[1][1] = Double.parseDouble(args[3]); // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. //twitterStream.filter(new FilterQuery(0, followArray, trackArray, location)); FilterQuery query = new FilterQuery(); query.locations(location); twitterStream.filter(query); }
From source file:datasite.DataSite.java
public static void main(String[] args) { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true);/*from w w w.ja v a 2s.c o m*/ cb.setOAuthConsumerKey("ConsumerKey"); cb.setOAuthConsumerSecret("ConsumerSecret"); cb.setOAuthAccessToken("AccessToken"); cb.setOAuthAccessTokenSecret("AccessTokenSecret"); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener; listener = new StatusListener() { @Override public void onException(Exception arg0) { // TODO Auto-generated method stub } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { // TODO Auto-generated method stub } @Override public void onScrubGeo(long arg0, long arg1) { // TODO Auto-generated method stub } @Override public void onStatus(Status status) { User user = status.getUser(); // gets Username String username = status.getUser().getScreenName(); try (PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter("tweet_input\\tweets.txt", true)))) { //out.println(username); String content = status.getText(); out.println(content + "\n"); //System.out.append( content +"\n"); } catch (IOException ex) { Logger.getLogger(DataSite.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void onTrackLimitationNotice(int arg0) { // TODO Auto-generated method stub } @Override public void onStallWarning(StallWarning sw) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }; FilterQuery fq = new FilterQuery(); String keywords[] = { "africa" }; fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); //twitterStream.(); }