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