Search in sources :

Example 1 with TwitterStreamFactory

use of twitter4j.TwitterStreamFactory in project Talon-for-Twitter by klinker24.

the class Utils method getStreamingTwitter.

public static TwitterStream getStreamingTwitter(Context context, AppSettings settings) {
    settings = AppSettings.getInstance(context);
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(AppSettings.TWITTER_CONSUMER_KEY).setOAuthConsumerSecret(AppSettings.TWITTER_CONSUMER_SECRET).setOAuthAccessToken(settings.authenticationToken).setOAuthAccessTokenSecret(settings.authenticationTokenSecret);
    TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
    return tf.getInstance();
}
Also used : ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) TwitterStreamFactory(twitter4j.TwitterStreamFactory)

Example 2 with TwitterStreamFactory

use of twitter4j.TwitterStreamFactory in project twitter4j by yusuke.

the class PrintRawSampleStream method main.

/**
     * Main entry of this application.
     *
     * @param args arguments doesn't take effect with this example
     * @throws TwitterException when Twitter service or network is unavailable
     */
public static void main(String[] args) throws TwitterException {
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    RawStreamListener listener = new RawStreamListener() {

        @Override
        public void onMessage(String rawJSON) {
            System.out.println(rawJSON);
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };
    twitterStream.addListener(listener);
    twitterStream.sample();
}
Also used : TwitterStreamFactory(twitter4j.TwitterStreamFactory) TwitterStream(twitter4j.TwitterStream) RawStreamListener(twitter4j.RawStreamListener) TwitterException(twitter4j.TwitterException)

Example 3 with TwitterStreamFactory

use of twitter4j.TwitterStreamFactory in project ud381 by udacity.

the class TweetSpout method open.

@Override
public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
    // create the buffer to block tweets
    queue = new LinkedBlockingQueue<String>(1000);
    SentimentAnalyzer.init();
    // save the output collector for emitting tuples
    collector = spoutOutputCollector;
    // build the config with credentials for twitter 4j
    ConfigurationBuilder config = new ConfigurationBuilder().setOAuthConsumerKey(custkey).setOAuthConsumerSecret(custsecret).setOAuthAccessToken(accesstoken).setOAuthAccessTokenSecret(accesssecret);
    // create the twitter stream factory with the config
    TwitterStreamFactory fact = new TwitterStreamFactory(config.build());
    // get an instance of twitter stream
    twitterStream = fact.getInstance();
    // See 
    FilterQuery tweetFilterQuery = new FilterQuery();
    tweetFilterQuery.locations(new double[][] { new double[] { -124.848974, 24.396308 }, new double[] { -66.885444, 49.384358 } });
    tweetFilterQuery.language(new String[] { "en" });
    // provide the handler for twitter stream
    twitterStream.addListener(new TweetListener());
    twitterStream.filter(tweetFilterQuery);
    // start the sampling of tweets
    twitterStream.sample();
}
Also used : ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) TwitterStreamFactory(twitter4j.TwitterStreamFactory) FilterQuery(twitter4j.FilterQuery)

Example 4 with TwitterStreamFactory

use of twitter4j.TwitterStreamFactory in project asterixdb by apache.

the class TwitterUtil method getTwitterStream.

public static TwitterStream getTwitterStream(Map<String, String> configuration) {
    ConfigurationBuilder cb = getAuthConfiguration(configuration);
    TwitterStreamFactory factory = new TwitterStreamFactory(cb.build());
    return factory.getInstance();
}
Also used : ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) TwitterStreamFactory(twitter4j.TwitterStreamFactory)

Example 5 with TwitterStreamFactory

use of twitter4j.TwitterStreamFactory in project Anserini by castorini.

the class TweetStreamIndexer method run.

@Override
public void run() {
    tweetCount = 0;
    final FieldType textOptions = new FieldType();
    // textOptions.setIndexed(true);
    textOptions.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
    textOptions.setStored(true);
    textOptions.setTokenized(true);
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    RawStreamListener rawListener = new RawStreamListener() {

        @Override
        public void onMessage(String rawString) {
            Status status = Status.fromJson(rawString);
            if (status == null) {
                try {
                    JsonObject obj = (JsonObject) JSON_PARSER.parse(rawString);
                    if (obj.has("delete")) {
                        long id = obj.getAsJsonObject("delete").getAsJsonObject("status").get("id").getAsLong();
                        Query q = LongPoint.newRangeQuery(StatusField.ID.name, id, id);
                        TweetSearcher.indexWriter.deleteDocuments(q);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return;
            }
            if (status.getText() == null) {
                return;
            }
            Document doc = new Document();
            doc.add(new LongPoint(StatusField.ID.name, status.getId()));
            doc.add(new StoredField(StatusField.ID.name, status.getId()));
            doc.add(new LongPoint(StatusField.EPOCH.name, status.getEpoch()));
            doc.add(new StoredField(StatusField.EPOCH.name, status.getEpoch()));
            doc.add(new TextField(StatusField.SCREEN_NAME.name, status.getScreenname(), Store.YES));
            doc.add(new Field(StatusField.TEXT.name, status.getText(), textOptions));
            doc.add(new IntPoint(StatusField.FRIENDS_COUNT.name, status.getFollowersCount()));
            doc.add(new StoredField(StatusField.FRIENDS_COUNT.name, status.getFollowersCount()));
            doc.add(new IntPoint(StatusField.FOLLOWERS_COUNT.name, status.getFriendsCount()));
            doc.add(new StoredField(StatusField.FOLLOWERS_COUNT.name, status.getFriendsCount()));
            doc.add(new IntPoint(StatusField.STATUSES_COUNT.name, status.getStatusesCount()));
            doc.add(new StoredField(StatusField.STATUSES_COUNT.name, status.getStatusesCount()));
            long inReplyToStatusId = status.getInReplyToStatusId();
            if (inReplyToStatusId > 0) {
                doc.add(new LongPoint(StatusField.IN_REPLY_TO_STATUS_ID.name, inReplyToStatusId));
                doc.add(new StoredField(StatusField.IN_REPLY_TO_STATUS_ID.name, inReplyToStatusId));
                doc.add(new LongPoint(StatusField.IN_REPLY_TO_USER_ID.name, status.getInReplyToUserId()));
                doc.add(new StoredField(StatusField.IN_REPLY_TO_USER_ID.name, status.getInReplyToUserId()));
            }
            String lang = status.getLang();
            if (!lang.equals("unknown")) {
                doc.add(new TextField(StatusField.LANG.name, status.getLang(), Store.YES));
            }
            long retweetStatusId = status.getRetweetedStatusId();
            if (retweetStatusId > 0) {
                doc.add(new LongPoint(StatusField.RETWEETED_STATUS_ID.name, retweetStatusId));
                doc.add(new StoredField(StatusField.RETWEETED_STATUS_ID.name, retweetStatusId));
                doc.add(new LongPoint(StatusField.RETWEETED_USER_ID.name, status.getRetweetedUserId()));
                doc.add(new StoredField(StatusField.RETWEETED_USER_ID.name, status.getRetweetedUserId()));
                doc.add(new IntPoint(StatusField.RETWEET_COUNT.name, status.getRetweetCount()));
                doc.add(new StoredField(StatusField.RETWEET_COUNT.name, status.getRetweetCount()));
                if (status.getRetweetCount() < 0 || status.getRetweetedStatusId() < 0) {
                    System.err.println("Error parsing retweet fields of " + status.getId());
                }
            }
            try {
                TweetSearcher.indexWriter.addDocument(doc);
                tweetCount++;
                if (tweetCount % 1000 == 0) {
                    LOG.info(tweetCount + " statuses indexed");
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void onException(Exception e) {
            // TODO Auto-generated method stub
            e.printStackTrace();
        }
    };
    twitterStream.addListener(rawListener);
    twitterStream.sample();
}
Also used : Status(io.anserini.document.twitter.Status) RawStreamListener(twitter4j.RawStreamListener) Query(org.apache.lucene.search.Query) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) IOException(java.io.IOException) TwitterStreamFactory(twitter4j.TwitterStreamFactory) TwitterStream(twitter4j.TwitterStream)

Aggregations

TwitterStreamFactory (twitter4j.TwitterStreamFactory)7 IOException (java.io.IOException)3 RawStreamListener (twitter4j.RawStreamListener)3 TwitterStream (twitter4j.TwitterStream)3 ConfigurationBuilder (twitter4j.conf.ConfigurationBuilder)3 JsonObject (com.google.gson.JsonObject)2 Status (io.anserini.document.twitter.Status)2 Query (org.apache.lucene.search.Query)2 Firehose (io.druid.data.input.Firehose)1 MapBasedInputRow (io.druid.data.input.MapBasedInputRow)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 Matcher (java.util.regex.Matcher)1 Nullable (javax.annotation.Nullable)1 ConnectionLifeCycleListener (twitter4j.ConnectionLifeCycleListener)1 FilterQuery (twitter4j.FilterQuery)1 GeoLocation (twitter4j.GeoLocation)1 HashtagEntity (twitter4j.HashtagEntity)1