Example usage for twitter4j TwitterStreamFactory TwitterStreamFactory

List of usage examples for twitter4j TwitterStreamFactory TwitterStreamFactory

Introduction

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

Prototype

public TwitterStreamFactory() 

Source Link

Document

Creates a TwitterStreamFactory with the root configuration.

Usage

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));
        }/*w w  w  .  ja  v a2 s.co  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:gh.polyu.user.TrackUsers.java

License:Apache License

public void track(final int no, final int p) {
    final TwitterDBHandle handle = new TwitterDBHandle();
    handle.intialTwitterDBhandle();/*from w w  w .  j  a va  2 s .c om*/

    while (alive) {
        alive = false;
        StatusListener listener = new StatusListener() {
            ArrayList<_TweetLink> listlink = new ArrayList<_TweetLink>();
            int cnt = 0;
            String oldmonth = "20138";
            String table = "UserTweet20138";
            String oldday = "";
            String currentday = "";
            String currentmonth = "";
            long lastinsert = 0l;
            long nowinsert = 0l;
            int newday = 0;
            String newtime = "";

            @Override

            public void onStatus(Status status) {
                if (status.getId() == 123 && status.getText().equals("YOU are WORNG!.")) {
                    System.out.println("Connection Need to be rebuilt!!");
                    alive = true;
                } else if (status.getLang().equals("en")) {
                    _TweetLink tweet = new _TweetLink();
                    String Test = status.getText();
                    tweet.setText(Test);
                    Date time = status.getCreatedAt();
                    tweet.setTime(time);
                    tweet.setUserName(status.getUser().getName());
                    HashtagEntity[] hashtagentity = status.getHashtagEntities();
                    StringBuffer hashen = new StringBuffer();
                    for (int i = 0; i < hashtagentity.length; i++) {
                        hashen.append(hashtagentity[i].getText());
                        hashen.append(";");

                    }
                    tweet.setHashtag(hashen.toString());

                    URLEntity[] URLEn = status.getURLEntities();
                    StringBuffer URL = new StringBuffer();
                    for (int i = 0; i < URLEn.length; i++) {
                        URL.append(URLEn[i].getURL());
                        URL.append(";");

                    }
                    tweet.setURL(URL.toString());

                    //user mention
                    UserMentionEntity[] userEn = status.getUserMentionEntities();
                    StringBuffer mentuser = new StringBuffer();
                    for (int i = 0; i < userEn.length; i++) {
                        mentuser.append(userEn[i].getId());
                        mentuser.append(";");
                    }
                    tweet.setUerMention(mentuser.toString());
                    //if(mentuser.length()!=0);
                    //System.out.println("mentuser: "+ mentuser);
                    //tweetID
                    tweet.setTweetID(status.getId());
                    //if(ID!=null)
                    // original twitterID
                    tweet.setOriginID(status.getInReplyToStatusId());
                    //original user ID
                    tweet.setOriginUser(status.getInReplyToUserId());

                    // user ID

                    User users = status.getUser();
                    tweet.setTweetUser(users.getId());

                    //places
                    Place Pl = status.getPlace();
                    String place = "";
                    if (Pl != null) {
                        place = Pl.getFullName();
                        //System.out.println("place "+place);
                    }
                    tweet.setPlace(place);
                    // Retweetcoun
                    long num = 0;
                    if (status.getRetweetedStatus() != null) {
                        num = status.getRetweetedStatus().getRetweetCount();
                        //System.out.println("retweetcount"+num);
                        tweet.setRetweetCount(num);
                        tweet.setRetweet(1);
                    } else {
                        tweet.setRetweetCount(0);
                        tweet.setRetweet(0);
                    }
                    //   if(Retweet!=null)

                    //System.out.println("Retweetcount: "+ Retweet);

                    //isfavourate
                    boolean favourate = status.isFavorited();
                    /*if(favourate)
                    {
                    fav = 1;
                    tweet.setFavourate(fav);
                    fav =0;
                    System.out.println("isf "+ fav);
                    }*/

                    // is retweet

                    //String other = status.toString();
                    // tweet.setOther(other);
                    listlink.add(tweet);
                    Calendar cal = Calendar.getInstance();
                    int year = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH) + 1;
                    int day = cal.get(Calendar.DAY_OF_MONTH);
                    currentmonth = String.valueOf(year) + String.valueOf(month);
                    currentday = String.valueOf(day);
                    if (currentmonth.equals(oldmonth)) {
                        if (currentday.equals(oldday))
                            ;
                        else {
                            newday = 1;
                            SimpleDateFormat formatter = new SimpleDateFormat("MMddHH:mm:ss ");
                            Date curDate = new Date(System.currentTimeMillis());//     
                            newtime = formatter.format(curDate);
                        }
                    } else {
                        try {
                            handle.database_connection();
                            table = "UserTweet" + String.valueOf(year) + String.valueOf(month);
                            System.out.println("create new table " + table);
                            String CREATE_TABLE = "create table " + table
                                    + "(TweetID varchar(100), UserName varchar(200), TwitterUser varchar(145), OriginID varchar(100), OriginUser varchar(100), place varchar(100), RetweetCount varchar(100), isRetweet int(5), Text varchar(500), Time datetime,"
                                    + "Hashtag varchar(200), URL varchar(200), UerMention varchar(200))";
                            Statement st = handle.conn.createStatement();
                            st.execute(CREATE_TABLE);
                            String Create_Index = "alter table " + table + " add index time (Time)";
                            st.execute(Create_Index);
                            String Create_Index2 = "alter table " + table + " add index userID (TwitterUser)";
                            st.execute(Create_Index2);
                            String key = "ALTER TABLE " + table + " ADD PRIMARY KEY (TweetID)";
                            st.execute(key);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        handle.close_databasehandle();
                        try {
                            TwitterDBHandle handle2 = new TwitterDBHandle();
                            handle2.intialTwitterDBhandle2();
                            handle2.database_connection();
                            table = "UserTweet" + String.valueOf(year) + String.valueOf(month);
                            System.out.println("create new table " + table);
                            String CREATE_TABLE = "create table " + table
                                    + "(TweetID varchar(100), UserName varchar(200), TwitterUser varchar(145), OriginID varchar(100), OriginUser varchar(100), place varchar(100), RetweetCount varchar(100), isRetweet int(5), Text varchar(500), Time datetime,"
                                    + "Hashtag varchar(200), URL varchar(200), UerMention varchar(200))";
                            Statement st = handle2.conn.createStatement();
                            st.execute(CREATE_TABLE);
                            String Create_Index = "alter table " + table + " add index time (Time)";
                            st.execute(Create_Index);
                            String Create_Index2 = "alter table " + table + " add index userID (TwitterUser)";
                            st.execute(Create_Index2);
                            String key = "ALTER TABLE " + table + " ADD PRIMARY KEY (TweetID)";
                            st.execute(key);
                            handle2.close_databasehandle();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                    //System.out.println("OTHER: "+ other);

                    if ((cnt++) % 1000 == 0) {

                        if (newday == 1) {
                            newday = 0;
                            oldday = currentday;
                            GmailSend gs = new GmailSend("cscchenyoyo@gmail.com", "910316ccy");
                            gs.send("THREAD" + p + " :" + "program no" + no + "message" + newtime,
                                    "I am still alive");
                            newtime = "";
                        }

                        try {
                            handle.database_connection();
                            handle.userTweet(table, listlink);
                            nowinsert = System.currentTimeMillis();
                            System.err.println(
                                    "No: " + no + "program " + "totally " + cnt + " tweets downloaded!\n"
                                            + new Date(nowinsert) + "  " + new Date(lastinsert));
                            lastinsert = nowinsert;
                            nowinsert = 0l;
                            handle.close_databasehandle();
                        } catch (SQLException e) {
                            handle.close_databasehandle();
                            e.printStackTrace();
                            // TODO Auto-generated catch block
                            TwitterDBHandle handle2 = new TwitterDBHandle();
                            handle2.intialTwitterDBhandle2();
                            handle2.database_connection();

                            try {
                                handle2.userTweet(table, listlink);
                                nowinsert = System.currentTimeMillis();
                                System.err.println("New Database No: " + no + "program " + "totally " + cnt
                                        + " tweets downloaded!\n" + new Date(nowinsert) + new Date(lastinsert));
                                lastinsert = nowinsert;
                                nowinsert = 0l;
                                handle2.close_databasehandle();
                            } catch (SQLException e1) {
                                // TODO Auto-generated catch block
                                GmailSend gs = new GmailSend("cscchenyoyo@gmail.com", "910316ccy");
                                try {
                                    gs.SendSSLMessage("cscchenyoyo@gmail.com", "program error",
                                            "both databases are down");
                                } catch (MessagingException ee) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                            GmailSend gs = new GmailSend("cscchenyoyo@gmail.com", "910316ccy");
                            try {
                                gs.SendSSLMessage("cscchenyoyo@gmail.com", "program error",
                                        "change database to another one");
                            } catch (MessagingException ee) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        listlink.clear();
                    }
                }
            }

            @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 twitterStream = new TwitterStreamFactory().getInstance();
        twitterOAuth twtOauth = new twitterOAuth();
        twtOauth.AuthoritywithS(twitterStream, key);
        twitterStream.addListener(listener);
        twitterStream.filter(new FilterQuery(0, follow));

        /*  try {
           Thread.sleep(3000);
                   
        } catch (InterruptedException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }*/
    }
}

From source file:gov.nasa.jpl.memex.elwha.impl.ElwhaResource.java

License:Apache License

/**
 * Main entry of this application./* ww w .j a  va2s .c o  m*/
 *
 * @param userIds follow(comma separated user ids) track(comma separated filter terms)
 * @throws TwitterException when Twitter service or network is unavailable
 * @return
 */
@Override
public Elwha get(String userIds) {
    //return new Elwha().setMessage("Hello, Rest.li!");
    // }

    //if (userIds.length < 1) {
    //  System.out.println("Usage: java twitter4j.examples.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]");
    //  System.exit(-1);
    //}

    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 twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);
    ArrayList<Long> follow = new ArrayList<Long>();
    ArrayList<String> track = new ArrayList<String>();
    if (isNumericalArgument(userIds)) {
        for (String id : userIds.split(",")) {
            follow.add(Long.parseLong(id));
        }
    } else {
        track.addAll(Arrays.asList(userIds.split(",")));
    }
    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()]);

    // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
    twitterStream.filter(new FilterQuery(0, followArray, trackArray));

    return new Elwha().setUserIds("Mock stub!");
}

From source file:io.druid.examples.twitter.TwitterSpritzerFirehoseFactory.java

License:Apache License

@Override
public Firehose connect(InputRowParser parser) throws IOException {
    final ConnectionLifeCycleListener connectionLifeCycleListener = new ConnectionLifeCycleListener() {
        @Override/*from  w w  w  .  j  a v a2 s .com*/
        public void onConnect() {
            log.info("Connected_to_Twitter");
        }

        @Override
        public void onDisconnect() {
            log.info("Disconnect_from_Twitter");
        }

        /**
         * called before thread gets cleaned up
         */
        @Override
        public void onCleanUp() {
            log.info("Cleanup_twitter_stream");
        }
    }; // ConnectionLifeCycleListener

    final TwitterStream twitterStream;
    final StatusListener statusListener;
    final int QUEUE_SIZE = 2000;
    /** This queue is used to move twitter events from the twitter4j thread to the druid ingest thread.   */
    final BlockingQueue<Status> queue = new ArrayBlockingQueue<Status>(QUEUE_SIZE);
    final long startMsec = System.currentTimeMillis();

    //
    //   set up Twitter Spritzer
    //
    twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addConnectionLifeCycleListener(connectionLifeCycleListener);
    statusListener = new StatusListener() { // This is what really gets called to deliver stuff from twitter4j
        @Override
        public void onStatus(Status status) {
            // time to stop?
            if (Thread.currentThread().isInterrupted()) {
                throw new RuntimeException("Interrupted, time to stop");
            }
            try {
                boolean success = queue.offer(status, 15L, TimeUnit.SECONDS);
                if (!success) {
                    log.warn("queue too slow!");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException("InterruptedException", e);
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            //log.info("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            // This notice will be sent each time a limited stream becomes unlimited.
            // If this number is high and or rapidly increasing, it is an indication that your predicate is too broad, and you should consider a predicate with higher selectivity.
            log.warn("Got track limitation notice:" + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            //log.info("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
        }

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

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

    twitterStream.addListener(statusListener);
    twitterStream.sample(); // creates a generic StatusStream
    log.info("returned from sample()");

    return new Firehose() {

        private final Runnable doNothingRunnable = new Runnable() {
            public void run() {
            }
        };

        private long rowCount = 0L;
        private boolean waitIfmax = (getMaxEventCount() < 0L);
        private final Map<String, Object> theMap = new TreeMap<>();
        // DIY json parsing // private final ObjectMapper omapper = new ObjectMapper();

        private boolean maxTimeReached() {
            if (getMaxRunMinutes() <= 0) {
                return false;
            } else {
                return (System.currentTimeMillis() - startMsec) / 60000L >= getMaxRunMinutes();
            }
        }

        private boolean maxCountReached() {
            return getMaxEventCount() >= 0 && rowCount >= getMaxEventCount();
        }

        @Override
        public boolean hasMore() {
            if (maxCountReached() || maxTimeReached()) {
                return waitIfmax;
            } else {
                return true;
            }
        }

        @Override
        public InputRow nextRow() {
            // Interrupted to stop?
            if (Thread.currentThread().isInterrupted()) {
                throw new RuntimeException("Interrupted, time to stop");
            }

            // all done?
            if (maxCountReached() || maxTimeReached()) {
                if (waitIfmax) {
                    // sleep a long time instead of terminating
                    try {
                        log.info("reached limit, sleeping a long time...");
                        sleep(2000000000L);
                    } catch (InterruptedException e) {
                        throw new RuntimeException("InterruptedException", e);
                    }
                } else {
                    // allow this event through, and the next hasMore() call will be false
                }
            }
            if (++rowCount % 1000 == 0) {
                log.info("nextRow() has returned %,d InputRows", rowCount);
            }

            Status status;
            try {
                status = queue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException("InterruptedException", e);
            }

            theMap.clear();

            HashtagEntity[] hts = status.getHashtagEntities();
            String text = status.getText();
            theMap.put("text", (null == text) ? "" : text);
            theMap.put("htags", (hts.length > 0)
                    ? Lists.transform(Arrays.asList(hts), new Function<HashtagEntity, String>() {
                        @Nullable
                        @Override
                        public String apply(HashtagEntity input) {
                            return input.getText();
                        }
                    })
                    : ImmutableList.<String>of());

            long[] lcontrobutors = status.getContributors();
            List<String> contributors = new ArrayList<>();
            for (long contrib : lcontrobutors) {
                contributors.add(String.format("%d", contrib));
            }
            theMap.put("contributors", contributors);

            GeoLocation geoLocation = status.getGeoLocation();
            if (null != geoLocation) {
                double lat = status.getGeoLocation().getLatitude();
                double lon = status.getGeoLocation().getLongitude();
                theMap.put("lat", lat);
                theMap.put("lon", lon);
            } else {
                theMap.put("lat", null);
                theMap.put("lon", null);
            }

            if (status.getSource() != null) {
                Matcher m = sourcePattern.matcher(status.getSource());
                theMap.put("source", m.find() ? m.group(1) : status.getSource());
            }

            theMap.put("retweet", status.isRetweet());

            if (status.isRetweet()) {
                Status original = status.getRetweetedStatus();
                theMap.put("retweet_count", original.getRetweetCount());

                User originator = original.getUser();
                theMap.put("originator_screen_name", originator != null ? originator.getScreenName() : "");
                theMap.put("originator_follower_count",
                        originator != null ? originator.getFollowersCount() : "");
                theMap.put("originator_friends_count", originator != null ? originator.getFriendsCount() : "");
                theMap.put("originator_verified", originator != null ? originator.isVerified() : "");
            }

            User user = status.getUser();
            final boolean hasUser = (null != user);
            theMap.put("follower_count", hasUser ? user.getFollowersCount() : 0);
            theMap.put("friends_count", hasUser ? user.getFriendsCount() : 0);
            theMap.put("lang", hasUser ? user.getLang() : "");
            theMap.put("utc_offset", hasUser ? user.getUtcOffset() : -1); // resolution in seconds, -1 if not available?
            theMap.put("statuses_count", hasUser ? user.getStatusesCount() : 0);
            theMap.put("user_id", hasUser ? String.format("%d", user.getId()) : "");
            theMap.put("screen_name", hasUser ? user.getScreenName() : "");
            theMap.put("location", hasUser ? user.getLocation() : "");
            theMap.put("verified", hasUser ? user.isVerified() : "");

            theMap.put("ts", status.getCreatedAt().getTime());

            List<String> dimensions = Lists.newArrayList(theMap.keySet());

            return new MapBasedInputRow(status.getCreatedAt().getTime(), dimensions, theMap);
        }

        @Override
        public Runnable commit() {
            // ephemera in, ephemera out.
            return doNothingRunnable; // reuse the same object each time
        }

        @Override
        public void close() throws IOException {
            log.info("CLOSE twitterstream");
            twitterStream.shutdown(); // invokes twitterStream.cleanUp()
        }
    };
}

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//from w  w  w  .j a va 2  s  . 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);
}

From source file:Jimbo.Cheerlights.TweetListener.java

License:Open Source License

/**
 * @param args the command line arguments
 * @throws twitter4j.TwitterException/*ww w.  j  a v  a 2 s. c o  m*/
 * @throws java.io.IOException
 * @throws org.apache.commons.cli.ParseException In case of command line error
 */
public static void main(String[] args) throws TwitterException, IOException, ParseException {
    // Set up simpler logging to stdout
    Jimbo.Logging.Logging.useStdout();

    LOG.log(Level.INFO, "Starting twitter listener");

    Options options = new Options();

    options.addOption("b", Listener.MQTT_BROKER_KEY, true, "URL of the broker")
            .addOption("c", Listener.MQTT_CLIENT_KEY, true, "The MQTT client name to use")
            .addOption("t", Listener.MQTT_TOPIC_KEY, true, "The MQTT topic to use");

    CommandLineParser parser = new DefaultParser();
    CommandLine command = parser.parse(options, args);

    MQTTClient mqtt = null;
    String mqtt_topic = Listener.DEFAULT_MQTT_TOPIC;

    if (command.hasOption(Listener.MQTT_BROKER_KEY)) {
        if (!command.hasOption(Listener.MQTT_CLIENT_KEY))
            throw new ParseException("MQTT without client name");

        if (command.hasOption(Listener.MQTT_TOPIC_KEY))
            mqtt_topic = command.getOptionValue(Listener.MQTT_TOPIC_KEY);

        try {
            mqtt = new MQTTClient(command.getOptionValue(Listener.MQTT_BROKER_KEY),
                    command.getOptionValue(Listener.MQTT_CLIENT_KEY));
            mqtt.run();
        }

        catch (MqttException e) {
            LOG.log(Level.WARNING, "Failed to create MQTT client: {0}", e.toString());
        }
    } else {
        if (command.hasOption(Listener.MQTT_TOPIC_KEY))
            LOG.warning("MQTT topic supplied but no broker");

        if (command.hasOption(Listener.MQTT_CLIENT_KEY))
            LOG.warning("MQTT client name but no broker");
    }

    Twitter twitter = new TwitterFactory().getInstance();
    StatusListener listener = new listener("224.1.1.1", (short) 5123, mqtt, mqtt_topic);
    FilterQuery fq = new FilterQuery();

    String keywords[] = { "#cheerlights" };

    fq.track(keywords);

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);
    twitterStream.filter(fq);

    LOG.log(Level.INFO, "Up and running....");
}

From source file:kafka.producer.PrintGeoStream.java

License:Apache License

/**
 * Main entry of this application./*w  ww . j  a va  2s.c  om*/
 *
 * @param args <kafka host> <kafka topic>
 * @throws twitter4j.TwitterException
 */
public static void main(String[] args) throws TwitterException {

    //        if (args.length != 2) {
    //            System.out.println("Usage: java twitter4j.examples.PrintFilterStream  <kafka host>  <kafka topic>");
    //            System.exit(-1);
    //        }

    //        final String kafkaZKHost = args[0];
    //        final String kafkaTopic = args[1];

    final String kafkaZKHost = "localhost";
    final String kafkaTopic = "flightDelay";

    BasicConfigurator.configure();

    StatusListener listener = new StatusListener() {

        Gson gson = new Gson();

        Properties kafkaProps = new Properties();
        //kafkaProps.put("serializer.class", "kafka.serializer.StringEncoder");
        //kafkaProps.put("zk.connect", "localhost:2181");
        Producer<Integer, String> kafkaProducer = null;
        int tweetCount = 0;
        int interimCount = 0;

        @Override
        public void onStatus(Status status) {
            if (kafkaProducer == null) {
                kafkaProps.put("serializer.class", "kafka.serializer.StringEncoder");
                //kafkaProps.put("zk.connect", "localhost:2181");
                kafkaProps.put("zk.connect", kafkaZKHost.concat(":2181"));
                kafkaProducer = new Producer<Integer, String>(new ProducerConfig(kafkaProps));

            }
            tweetCount++;
            interimCount++;
            if (interimCount == 100) {
                LOG.info(" total tweets received: " + tweetCount);
                interimCount = 0;
            }

            //System.out.println("@" + status.getUser().getScreenName() + " - "); // + status.getText());
            //if ( status.getPlace() != null ) {
            //    System.out.println("coordinates = " + status.getPlace().getGeometryCoordinates());
            //}

            TweetInfo tweetInfo = new TweetInfo();
            tweetInfo.populate(status);
            String message = gson.toJson(tweetInfo, TweetInfo.class);

            kafkaProducer.send(new ProducerData<Integer, String>(kafkaTopic, message));
            //kafkaProducer.send(new ProducerData<Integer, String>("live_tweets", message));
        }

        @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();
        }
    };
    /*
     * - use Redis to pass filter messages in - or ZK?
     * - Read in filter JSON with parms for:
     *     - lat/lon boxj
     *     - screen names to follow
     *     - hashtags to track
     * - Convert screen names to userIds
     *     - Twitter twitter = new TwitterFactory().getInstance();
     *       ResponseList<User> users = twitter.lookupUsers(args[0].split(","));
     * - Kick off filter with new FilterQuery
     *     - periodically check, and if filter updated, restart the TwitterStream:
     *         .shutdown()
     *         new
     *         .addListener
     * 
     * 
     */

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    //        twitterStream.setOAuthAccessToken(new AccessToken("14098069-IgutahzmbaCubdoMU0MxsfsJT2iNVpRnoohCbV67k", "d1kFKqRSnrlA4bP1XWWLiHNsHfYDZQPWDHRVpGP4hM"));
    //        twitterStream.setOAuthConsumer("G0SKTmMXjfMsunBh3Furg", "iVTYNLqVaVdUZz4xOn7H8ywylTaTuW3kH1dT21NUy4");
    twitterStream.addListener(listener);
    /*
     * For CONUS only: 
     *  "lowerLat": "22.0",
     *  "lowerLon": "-127.0",
     *  "upperLat": "47.5",
     *  "upperLon": "-67.0"
     */
    FilterQuery fQuery = new FilterQuery();
    String[] topics = new String[2];
    topics[0] = "delayed flight";
    topics[1] = "flight delay";

    //        double[][] us = {{-180, 24}, {-66, 50}};
    double[][] lax = { { -118.5, 33.8 }, { -118.3, 34.01 } };
    fQuery.locations(lax);
    fQuery.track(topics);
    twitterStream.filter(fQuery);

}

From source file:me.timothy.twittertoreddit.TwitterToRedditMain.java

License:Open Source License

private void begin() throws InterruptedException {
    loadConfiguration();/* w  w w  .  j  a  v  a  2  s.  c o m*/
    HttpRestClient rClient = new HttpRestClient();
    rClient.setUserAgent("TwitterToReddit Bot by /u/Tjstretchalot");

    User user = new User(rClient, username, password);
    try {
        user.connect();
    } catch (Exception e) {
        System.err.println("Failed to connect");
        return;
    }

    System.out.println("Succesfully Authenticated to Reddit");
    Thread.sleep(1000);
    System.out.println("..");
    Thread.sleep(1000);
    System.out.println("Authenticating to Twitter..");

    TwitterStream stream = new TwitterStreamFactory().getInstance();
    FilterQuery filter = new FilterQuery();
    filter.follow(new long[] { twitterId });

    TwitterToReddit mBot = new TwitterToReddit(rClient, user, twitterId, subreddit);
    mBot.beginStreaming(stream);

    stream.filter(filter);

    System.out.println("Success! The bot is now active and will post new tweets as well as print out here");
    System.out.println();

}

From source file:monitoring.tools.TwitterAPI.java

License:Apache License

private void resetStream() {
    //logger.debug("Initialising kafka producer...");
    //kafka.initProducer(confParams.getKafkaEndpoint());
    logger.debug("Initialising proxy...");
    kafka.initProxy(confParams.getKafkaEndpoint());
    logger.debug("Initialising streaming...");
    firstConnection = true;/* w w w  .  ja va 2 s  .c om*/
    tweetInfo = new ArrayList<>();
    stream = new TwitterStreamFactory().getInstance();
    timer = new Timer();
    stream.onStatus(new Consumer<Status>() {
        @Override
        public void accept(Status arg0) {
            if (confParams.getAccounts() != null && !confParams.getAccounts().isEmpty()) {
                if (confParams.getAccounts().contains(arg0.getUser().getScreenName())) {
                    tweetInfo.add(arg0);
                }
            } else
                tweetInfo.add(arg0);
        }
    });
    stream.addConnectionLifeCycleListener(new ConnectionLifeCycleListener() {
        @Override
        public void onCleanUp() {
        }

        @Override
        public void onConnect() {
            logger.debug("Connection established successfully");
            timer = new Timer();
            timer.schedule(new TimerTask() {
                public void run() {
                    if (firstConnection) {
                        firstConnection = false;
                    } else {
                        generateData((new Timestamp((new Date()).getTime()).toString()));
                    }
                }
            }, 0, Integer.parseInt(confParams.getTimeSlot()) * 1000);
        }

        @Override
        public void onDisconnect() {
            logger.debug("Connection closed");
        }
    });

    FilterQuery filterQuery = new FilterQuery();
    if (this.confParams.getKeywordExpression() != null)
        filterQuery.track(Utils.generateKeywordExp(this.confParams.getKeywordExpression()));
    stream.filter(filterQuery);
}