Example usage for twitter4j FilterQuery FilterQuery

List of usage examples for twitter4j FilterQuery FilterQuery

Introduction

In this page you can find the example usage for twitter4j FilterQuery FilterQuery.

Prototype

public FilterQuery() 

Source Link

Document

Creates a new FilterQuery

Usage

From source file:datasite.SortedUnique.java

public static void main(String[] args) {
    ConfigurationBuilder confb = new ConfigurationBuilder();
    confb.setDebugEnabled(true);//from  w w w.  j a  va  2  s . c  o  m
    confb.setOAuthConsumerKey("Consumer Key");
    confb.setOAuthConsumerSecret("Consumer Secret");
    confb.setOAuthAccessToken("Access Token");
    confb.setOAuthAccessTokenSecret("Access Token Secret");

    TwitterStream twitterStream = new TwitterStreamFactory(confb.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) {

            PrintStream out = null;
            try {
                User user = status.getUser();
                // gets Username

                String username = status.getUser().getScreenName();
                String content = status.getText();
                Set<String> userWrds = new HashSet<String>(); // HashSet implements Set
                Scanner sc = new Scanner(content + user);
                while (sc.hasNext()) {
                    String word = sc.next();
                    userWrds.add(word);

                }
                out = new PrintStream(new FileOutputStream("tweet_output\\unique.txt", true));
                Iterator<String> iter = userWrds.iterator();
                for (int i = 1; i <= 1 && iter.hasNext(); i++)
                    //System.out.println(iter.next());
                    out.println(userWrds.size());
            } catch (FileNotFoundException ex) {
                Logger.getLogger(SortedUnique.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                out.close();
            }

        }

        @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.();
}

From source file:de.fhm.bigdata.projekt.dataimport.TwitterSource.java

License:Apache License

/**
 * Start processing events. This uses the Twitter Streaming API to sample
 * Twitter, and process tweets.// w w  w .jav  a2  s .com
 */
@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
            // shouldn't log possibly sensitive customer data
            logger.debug("tweet arrived");

            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();
}

From source file:demo.Investigation.java

License:Apache License

public static void main(String[] args) throws Exception {
    Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();

    TwitterStream twStream = new TwitterStreamFactory(configuration).getInstance();
    twStream.addListener(new MyStatusListener());
    //String[] track = {"#daihyo"};
    //#JPNvs#COL//from   w  w  w  .  ja v a  2  s  . c  o m
    //String[] track = {"#tvasahi,#daihyo,#jfa,#tvasahi_soccer,#JPN,#COL"};//soccer
    //String[] track = {"#nhk,#tvtokyo,#etv,#ntv,#tvasahi,#tbs,#fujitv,#tokyomx"};//TV
    String[] track = { "#nhk" };
    FilterQuery query = new FilterQuery();
    query.track(track);
    twStream.filter(query);
}

From source file:edu.csupomona.nlp.tool.crawler.Twitter.java

/**
 * Query with given keywords. Crawling will start immediately.
 * @param filename      File name for the query result
 * @param keywords      Array of keywords
 *///from   www . ja  v a2 s  .co  m
public void query(String filename, String[] keywords) {
    // prepare for the new query
    queryDone_ = false;
    // construct file name
    filename_ = filename;
    // init tweet list
    tweet_ = new ArrayList<>();
    // init id set
    idSet_ = loadSet();
    // calculate ETA time
    Calendar cal = Calendar.getInstance();
    etaTime.setTimeInMillis(cal.getTimeInMillis() + hourLimit_ * 3600 * 1000);

    // debug info
    System.out.println("Querying for => " + filename_);

    // construct FilterQuery
    FilterQuery fQuery = new FilterQuery();
    fQuery.track(keywords); // track specified keywords
    String[] languages = { lang_ };
    fQuery.language(languages); // track specified language

    // start streaming with FilterQuery
    ts_.filter(fQuery);
}

From source file:edu.uci.ics.asterix.external.util.TwitterUtil.java

License:Apache License

public static FilterQuery getFilterQuery(Map<String, String> configuration) throws AsterixException {
    String locationValue = configuration.get(ConfigurationConstants.KEY_LOCATION);
    double[][] locations = null;
    if (locationValue != null) {
        if (locationValue.contains(",")) {
            String[] coordinatesString = locationValue.trim().split(",");
            locations = new double[2][2];
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    try {
                        locations[i][j] = Double.parseDouble(coordinatesString[2 * i + j]);
                    } catch (NumberFormatException ne) {
                        throw new AsterixException(
                                "Incorrect coordinate value " + coordinatesString[2 * i + j]);
                    }//  ww w. j a  v a 2  s .c  o m
                }
            }
        } else {
            locations = GeoConstants.boundingBoxes.get(locationValue);
        }
        if (locations != null) {
            FilterQuery filterQuery = new FilterQuery();
            filterQuery.locations(locations);
            return filterQuery;
        }
    }
    return null;

}

From source file:example.justids.java

License:Apache License

/**
* Usage: java twitter4j.examples.search.SearchTweets [query]
*
* @param args/*  w  w  w. jav a2  s .c om*/
*/
public static void main(String[] args) {

    StatusListener listener = new StatusListener() {
        public Double count = 0d;
        Date started = new Date();
        Date previous = new Date();

        @Override
        public void onStatus(Status status) {

            try {

                File file = new File("Filtered_over1percent_lab_pc_obama_all.txt");
                File file2 = new File("Filtered_over1percent_lab_pc_obama.txt");

                // if file doesnt exists, then create it

                FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
                BufferedWriter bw = new BufferedWriter(fw);
                FileWriter fw2 = new FileWriter(file2.getAbsoluteFile(), true);
                BufferedWriter bw2 = new BufferedWriter(fw2);

                if (this.count % 1000 == 0) {
                    Date finished10k = new Date();

                    System.out.println("\n\n\n\n   AVERAGE  RATE OF TWEETS is "
                            + (this.count * 1000 / (finished10k.getTime() - this.started.getTime())));
                    System.out.println(1000000d / (finished10k.getTime() - this.previous.getTime()));
                    System.out.println(this.count);
                    System.out.println(finished10k.getTime() + " " + this.started.getTime() + " "
                            + (finished10k.getTime() - this.started.getTime()));
                    System.out.println(finished10k.getTime() + " " + this.previous.getTime() + " "
                            + (finished10k.getTime() - this.previous.getTime()));
                    System.out.println(status.getSource());
                    System.out.println("\n\n\n\n");
                    this.previous = finished10k;

                }

                this.count++;
                //  System.out.println(status.getUser().getName() + " : " + status.getText()+" "+ this.count);
                bw.write(status.getId() + "\n");
                bw.close();
                if (status.getText().contains("obama")) {
                    bw2.write(status.getId() + "\n");
                    bw2.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //   System.out.println(statusDeletionNotice.getUserId()+" has deleted this tweet");
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println(numberOfLimitedStatuses + " are missing from here");

        }

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

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

        }

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

        }
    };

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    RawStreamListener rawst = new RawStreamListener() {
        public Double count = 0d;
        public Double lengthsum = 0d;
        Date started = new Date();
        Date previous = new Date();

        @Override

        public void onMessage(String message) {
            if (!message.startsWith("{\"delete")) {

                count++;
                lengthsum += message.length();
                if (count % 1000 == 0) {
                    System.out.println(lengthsum / count);
                    //                       lengthsum=0d;
                    //                       count=0d;
                }
            }
            // TODO Auto-generated method stub

        }

        @Override
        public void onException(Exception arg0) {
            // TODO Auto-generated method stub

        }
    };
    //      twitterStream.addListener(rawst);
    String[] searchfor = { "language", "people", "problem", "microsoft", "epidemic", "obama", "zoo" };
    FilterQuery query = new FilterQuery();
    query.track(searchfor);
    twitterStream.addListener(listener);
    twitterStream.filter(query);

    // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    //        twitterStream.sample();
}

From source file:flight_ranker.Flight_colllector.java

public static void main(String[] args) throws FileNotFoundException, IOException {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);//  w  ww.java2s . c  om
    cb.setOAuthConsumerKey("Oa6WAzH0j3sgVrP0CNGvxnWA2");
    cb.setOAuthConsumerSecret("sLdoFybvJvVFz7Lxbbv9KWQDFeKcVeZAkWDC4QMHnx5lV2OmGE");
    cb.setOAuthAccessToken("2691889945-5NOBWKUgT9FiAoyOQSCFg8CLlPRlDMbWcUrJBdK");
    cb.setOAuthAccessTokenSecret("J6tA8Sxrtz2JNSFdQwAonbGxNfLNuD9I54Zfvomku3p5t");

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

    StatusListener 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();
            long followers = user.getFollowersCount();

            long retweets = status.getRetweetCount();

            long favs = status.getFavoriteCount();

            System.out.println("USERNAME--> " + username);
            System.out.println("FOLLOWERS--> " + followers);
            String profileLocation = user.getLocation();

            //                System.out.println("RETWEETS--> "+retweets);

            //                System.out.println("FAVOURITES--> "+favs);

            System.out.println("LOCATION--> " + profileLocation);
            long tweetId = status.getId();
            System.out.println("TWEET ID--> " + tweetId);
            String content = status.getText();
            System.out.println("TWEET--> " + content + "\n");

            BufferedWriter b1 = null, b2, b3, b4, b5, b6, b7 = null;

            try {
                //output_file = new BufferedReader(new FileReader("G:\\Sentiwords.txt"));

                FileWriter f1 = new FileWriter("G:\\flights_data.txt", true);
                b1 = new BufferedWriter(f1);
                b1.write("#USERNAME- " + username);
                b1.newLine();

                b1.write("#Followers- " + followers);

                b1.newLine();

                b1.write("#Location- " + profileLocation);

                b1.newLine();
                b1.write("#ID- " + tweetId);

                b1.newLine();
                b1.write("#Tweet- " + content);
                b1.newLine();
                b1.newLine();

                tweet_editor modified_tweet = new tweet_editor(content);
                //tweet_tagger tagged_tweet = new tweet_tagger(modified_tweet.edited_tweet);
                //System.out.println(tagged_tweet.tagged);

                sentiment_calculator senti_value = new sentiment_calculator(modified_tweet.edited_tweet);

                if (content.contains("Indigo")) {
                    System.out.println("indigo");
                    FileWriter f2 = new FileWriter("G:\\Indigo.txt", true);
                    b2 = new BufferedWriter(f2);
                    b2.write(Double.toString(senti_value.senti_rate));
                    b2.newLine();
                }

                if (content.contains("Jet")) {
                    System.out.println("jet");
                    FileWriter f3 = new FileWriter("G:\\jet.txt", true);
                    b3 = new BufferedWriter(f3);
                    b3.write(Double.toString(senti_value.senti_rate));
                    b3.newLine();
                }

                if (content.contains("Indian")) {
                    System.out.println("indian");
                    FileWriter f4 = new FileWriter("G:\\Indian.txt", true);
                    b4 = new BufferedWriter(f4);
                    b4.write(Double.toString(senti_value.senti_rate));
                    b4.newLine();
                }

                if (content.contains("Spicejet")) {
                    System.out.println("spicejet");
                    FileWriter f5 = new FileWriter("G:\\spicejet.txt", true);
                    b5 = new BufferedWriter(f5);
                    b5.write(Double.toString(senti_value.senti_rate));
                    b5.newLine();
                }

                if (content.contains("AirAsia")) {
                    System.out.println("airasia");
                    FileWriter f6 = new FileWriter("G:\\airasia.txt", true);
                    b6 = new BufferedWriter(f6);
                    b6.write(Double.toString(senti_value.senti_rate));
                    b6.newLine();
                }

                try {
                    //output_file = new BufferedReader(new FileReader("G:\\Sentiwords.txt"));

                    FileWriter f7 = new FileWriter("G:\\flight_senti.txt", true);
                    b7 = new BufferedWriter(f7);
                    b7.write(String.valueOf(senti_value.senti_rate));
                    b7.newLine();

                }

                catch (IOException e) {
                    e.printStackTrace();
                }

                finally {
                    try {
                        b7.close();
                    } catch (IOException ex) {
                        Logger.getLogger(Flight_colllector.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

            }

            catch (IOException e) {
                e.printStackTrace();
            }

            finally {
                try {
                    b1.close();
                } catch (IOException ex) {
                    Logger.getLogger(Flight_colllector.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[] = {
            "Indian Airlines, Indigo Airlines, Indigo Airline , Indian Airline , Spicejet , jetAirways , Jet Airways, Jet Airlines , airasia" }; //we will pass stock related keyword here

    fq.track(keywords);

    twitterStream.addListener(listener);
    twitterStream.filter(fq);

}

From source file:fr.ybonnel.TwitterListener.java

License:Apache License

public TwitterListener(String filter, Consumer<String> onShutdown) {
    this.filter = filter;
    this.onShutdown = onShutdown;
    twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(this);
    twitterStream.filter(new FilterQuery().track(new String[] { filter }));
    this.executor.scheduleAtFixedRate(() -> {
        TweetPhoto tweet = tweetToSend.poll();
        if (tweet != null) {
            new HashSet<>(handlers).stream().forEach(handler -> sendTweetToHandler(handler, tweet));
        }/*from   w w w . j  av a 2  s  . c o m*/
    }, 1000, 1000, TimeUnit.MILLISECONDS);
}

From source file:fr.ybonnel.TwitterListenner.java

License:Apache License

public void startConsumeTwitter() {
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(this);
    twitterStream.filter(new FilterQuery().track(new String[] { filter }));
}

From source file:it.unipr.aotlab.TwitterMiner.twitter.client.TwitterMain.java

License:Open Source License

/**
 * The args[] are used to filter twitter stream (example:
 * "#twitter #facebook #social #redis"). If no filter is specified, the
 * default "#twitter" filter is used//  w w  w. j  a  va 2s.co  m
 * 
 * @param args
 * @throws FileNotFoundException
 * @throws TwitterException
 */
public static void main(String[] args) throws FileNotFoundException {

    redisDB = new RedisBackend();
    listener = new TwitterStreamListener(redisDB);

    /** Number of old tweets to catch before starting to listen live stream */
    int count = 0;

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);
    FilterQuery filterQuery = new FilterQuery();
    filterQuery.count(count);
    if (args.length == 0) {
        args = new String[1];
        args[0] = "#twitter";
    }
    filterQuery.track(args);
    twitterStream.filter(filterQuery);
}