So, the main goal here is to connect to the Bitfinex WebSocket by building a WebSocket Client. I would like to start receiving a stream of information(price,trades,etc). The problem is that at this stage I cannot even subscribe to a specific currency pair. In other words, I am sending a request for information to the WebSocket server but I am not receiving any responses and I cannot figure why this is. My code is below.
This is the main method
public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {
String URL = "wss://api-pub.bitfinex.com/ws/2/";
WebSocketClient client = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(client);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
StompSessionHandler sessionHandler = new MyStompSessionHandler();
stompClient.connect(URL,sessionHandler);
new Scanner(System.in).nextLine(); // Don't close immediately.
}
}
This is the MyStompSessionHandler
public class MyStompSessionHandler extends StompSessionHandlerAdapter{
#Override
public void afterConnected(
StompSession session, StompHeaders connectedHeaders) {
System.out.println("New session established : " + session.getSessionId());
System.out.println("wss://api-pub.bitfinex.com/ws/2");
session.send("wss://api-pub.bitfinex.com/ws/2/", getSampleMessage());
System.out.println("Message sent to websocket server");
}
#Override
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
System.out.println("Got an exception:" + exception);
}
#Override
public Type getPayloadType(StompHeaders headers) {
return OutboundMessage.class;
}
private Object getSampleMessage() {
InboundMessage inboundMessage = new InboundMessage();
inboundMessage.setEvent("subscribe");
inboundMessage.setChannel("ticker");
inboundMessage.setSymbol("tBTCUSD");
return inboundMessage;
}
#Override
public void handleFrame(StompHeaders headers, Object payload) {
InboundMessage msg = (InboundMessage) payload;
System.out.println(msg.toString());
}
}
This is the InboundMessage class
public class InboundMessage {
private String event;
private String channel;
private String symbol;
public InboundMessage() {
}
public InboundMessage(String event, String channel, String symbol) {
this.event = event;
this.channel = channel;
this.symbol = symbol;
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
#Override
public String toString() {
return "InboundMessage{" +
"event='" + event + '\'' +
", channel='" + channel + '\'' +
", symbol='" + symbol + '\'' +
'}';
}
I looked at the Bitfinex website and I don't see any evidence that STOMP is supported. They just have a REST and a WebSocket API. Therefore, using STOMP from your client isn't going to work.
I am using the paging library with Firebase, and set ValueEventListener on the repository to invalidate the data whenever a change happens. The problem is as soon as I trigger invalidate(), the Observer trigger onChanged and Adapter.submitList(items) gets items with 0 size before the new date is loaded from Firebase via loadInitial method on the ItemKeyedDataSource.
The result is that the DIFF_CALLBACK is never called on PagedListAdapter because there is no comparison between the old list and the new items list (0 size) and when the data received it refresh the list like if I used notifyDataSetChanged().
The only solution so far is to delay Adapter.submitList(items) by 5 seconds until all the data is received from Firebase, but it's not a practical solution.
public class UsersRepository {
private final static String TAG = UsersRepository.class.getSimpleName();
// [START declare_database_ref]
private DatabaseReference mDatabaseRef;
private DatabaseReference mUsersRef;
private Boolean isFirstLoaded = true;
public ValueEventListener usersChangesListener;
public UsersRepository() {
mDatabaseRef = FirebaseDatabase.getInstance().getReference();
mUsersRef = mDatabaseRef.child("users");
isFirstLoaded = true;
Log.d(TAG, "UsersRepository init. isFirstLoaded= " + isFirstLoaded);
}
public void getUsers(Long initialKey, final int size, #NonNull final ItemKeyedDataSource.LoadInitialCallback < User > callback) {
if (initialKey == null) {
Log.d(TAG, "getUsers initialKey= " + initialKey);
mUsersRef.orderByChild("created").limitToFirst(size).addListenerForSingleValueEvent(new ValueEventListener() {#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
// [START_EXCLUDE]
if (dataSnapshot.exists()) {
// loop throw users value
List < User > usersList = new ArrayList < >();
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
usersList.add(userSnapshot.getValue(User.class));
Log.d(TAG, "getUsers dataSnapshot. getSnapshotKey= " + userSnapshot.getKey());
}
if (usersList.size() == 0) {
return;
}
Log.d(TAG, "getUsers usersList.size= " + usersList.size() + " lastkey= " + usersList.get(usersList.size() - 1).getCreatedLong());
if (callback instanceof ItemKeyedDataSource.LoadInitialCallback) {
//initial load
/*((ItemKeyedDataSource.LoadInitialCallback)callback)
.onResult(usersList, 0, 14);*/
callback.onResult(usersList);
}
} else {
Log.w(TAG, "getUsers no users exist");
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
} else {
Log.d(TAG, "getUsers initialKey= " + initialKey);
mUsersRef.orderByChild("created").startAt(initialKey).limitToFirst(size).addListenerForSingleValueEvent(new ValueEventListener() {#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
// [START_EXCLUDE]
if (dataSnapshot.exists()) {
// loop throw users value
List < User > usersList = new ArrayList < >();
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
usersList.add(userSnapshot.getValue(User.class));
Log.d(TAG, "getUsers dataSnapshot. getSnapshotKey= " + userSnapshot.getKey());
}
if (usersList.size() == 0) {
return;
}
Log.d(TAG, "getUsers usersList.size= " + usersList.size() + " lastkey= " + usersList.get(usersList.size() - 1).getCreatedLong());
if (callback instanceof ItemKeyedDataSource.LoadInitialCallback) {
//initial load
/*((ItemKeyedDataSource.LoadInitialCallback)callback)
.onResult(usersList, 0, 14);*/
callback.onResult(usersList);
}
} else {
Log.w(TAG, "getUsers no users exist");
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
}
}
public void getUsersAfter(final Long key, final int size, #NonNull final ItemKeyedDataSource.LoadCallback < User > callback) {
/*if(key == entireUsersList.get(entireUsersList.size()-1).getCreatedLong()){
Log.d(TAG, "getUsersAfter init. afterKey= " + key+ "entireUsersList= "+entireUsersList.get(entireUsersList.size()-1).getCreatedLong());
return;
}*/
Log.d(TAG, "getUsersAfter. AfterKey= " + key);
mUsersRef.orderByChild("created").startAt(key).limitToFirst(size).addListenerForSingleValueEvent(new ValueEventListener() {#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
// [START_EXCLUDE]
if (dataSnapshot.exists()) {
// loop throw users value
List < User > usersList = new ArrayList < >();
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
usersList.add(userSnapshot.getValue(User.class));
Log.d(TAG, "getUsersAfter dataSnapshot. getSnapshotKey= " + userSnapshot.getKey());
}
if (usersList.size() == 0) {
return;
}
Log.d(TAG, "getUsersAfter usersList.size= " + usersList.size() + "lastkey= " + usersList.get(usersList.size() - 1).getCreatedLong());
if (callback instanceof ItemKeyedDataSource.LoadCallback) {
//initial After
callback.onResult(usersList);
/*((ItemKeyedDataSource.LoadCallback)callback)
.onResult(usersList);*/
}
} else {
Log.w(TAG, "getUsersAfter no users exist");
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
//mUsersRef.addValueEventListener(usersListener);
}
public void getUsersBefore(final Long key, final int size, #NonNull final ItemKeyedDataSource.LoadCallback < User > callback) {
Log.d(TAG, "getUsersBefore. BeforeKey= " + key);
/*if(key == entireUsersList.get(0).getCreatedLong()){
return;
}*/
mUsersRef.orderByChild("created").endAt(key).limitToLast(size).addListenerForSingleValueEvent(new ValueEventListener() {#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
// [START_EXCLUDE]
if (dataSnapshot.exists()) {
// loop throw users value
List < User > usersList = new ArrayList < >();
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
usersList.add(userSnapshot.getValue(User.class));
Log.d(TAG, "getUsersBefore dataSnapshot. getSnapshotKeys= " + userSnapshot.getKey());
}
if (usersList.size() == 0) {
return;
}
Log.d(TAG, "getUsersBefore usersList.size= " + usersList.size() + "lastkey= " + usersList.get(usersList.size() - 1).getCreatedLong());
if (callback instanceof ItemKeyedDataSource.LoadCallback) {
//initial before
callback.onResult(usersList);
/*((ItemKeyedDataSource.LoadCallback)callback)
.onResult(usersList);*/
}
//initial load
/* ((ItemKeyedDataSource.LoadCallback)callback)
.onResult(usersList, 0, usersList.size());*/
} else {
Log.w(TAG, "getUsersBefore no users exist");
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
//mUsersRef.addValueEventListener(usersListener);
}
public void usersChanged(final DataSource.InvalidatedCallback InvalidatedCallback) {
final Query query = mUsersRef.orderByChild("created");
usersChangesListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!isFirstLoaded) {
isFirstLoaded = true;
Log.d(TAG, "entireUsersList Invalidated:");
// Remove post value event listener
if (usersChangesListener != null) {
query.removeEventListener(usersChangesListener);
Log.d(TAG, "usersChanged Invalidated removeEventListener");
}
((ItemKeyedDataSource.InvalidatedCallback)InvalidatedCallback).onInvalidated();
}
isFirstLoaded = false;
/*if(entireUsersList.size() > 0){
entireUsersList.clear();
((ItemKeyedDataSource.InvalidatedCallback)onInvalidatedCallback).onInvalidated();
Log.d(TAG, "entireUsersList Invalidated:");
return;
}
if (dataSnapshot.exists()) {
// loop throw users value
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()){
entireUsersList.add(userSnapshot.getValue(User.class));
}
Log.d(TAG, "entireUsersList size= "+entireUsersList.size()+"dataSnapshot count= "+dataSnapshot.getChildrenCount());
} else {
Log.w(TAG, "usersChanged no users exist");
}*/
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
};
query.addValueEventListener(usersChangesListener);
//mUsersRef.addValueEventListener(eventListener);
}
}
public class UsersDataSource extends ItemKeyedDataSource < Long,
User >
public class UsersDataSource extends ItemKeyedDataSource < Long,
User > {
private final static String TAG = UsersDataSource.class.getSimpleName();
private UsersRepository usersRepository;
public UsersDataSource() {
usersRepository = new UsersRepository();
}
#Override
public void addInvalidatedCallback(#NonNull InvalidatedCallback onInvalidatedCallback) {
//super.addInvalidatedCallback(onInvalidatedCallback);
Log.d(TAG, "Callback Invalidated ");
usersRepository.usersChanged(onInvalidatedCallback);
}
#Override
public void loadInitial(#NonNull LoadInitialParams < Long > params, #NonNull LoadInitialCallback < User > callback) {
/*List<User> items = usersRepository.getUsers(params.requestedInitialKey, params.requestedLoadSize);
callback.onResult(items);*/
Log.d(TAG, "loadInitial params key" + params.requestedInitialKey + " " + params.requestedLoadSize);
usersRepository.getUsers(params.requestedInitialKey, params.requestedLoadSize, callback);
//usersRepository.getUsers( 0L, params.requestedLoadSize, callback);
}
#Override
public void loadAfter(#NonNull LoadParams < Long > params, #NonNull LoadCallback < User > callback) {
/*List<User> items = usersRepository.getUsers(params.key, params.requestedLoadSize);
callback.onResult(items);*/
Log.d(TAG, "loadAfter params key " + (params.key + 1));
usersRepository.getUsersAfter(params.key + 1, params.requestedLoadSize, callback);
}
#Override
public void loadBefore(#NonNull LoadParams < Long > params, #NonNull LoadCallback < User > callback) {
/*List<User> items = fetchItemsBefore(params.key, params.requestedLoadSize);
callback.onResult(items);*/
Log.d(TAG, "loadBefore params " + (params.key - 1));
usersRepository.getUsersBefore(params.key - 1, params.requestedLoadSize, callback);
}
#NonNull#Override
public Long getKey(#NonNull User user) {
return user.getCreatedLong();
}
}
public MainFragment()
public MainFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
*/
public static MainFragment newInstance(String param1, String param2) {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
//fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
/*if(activity != null){
activity.setTitle(R.string.main_frag_title);
}*/
View fragView = inflater.inflate(R.layout.fragment_main, container, false);
// prepare the Adapter
mUserArrayList = new ArrayList < >();
mProfileAdapter = new UsersAdapter();
// Initiate the viewModel
viewModel = ViewModelProviders.of(this).get(UsersViewModel.class);
// Initiate the RecyclerView
mProfileRecycler = (RecyclerView) fragView.findViewById(R.id.users_recycler);
mProfileRecycler.setHasFixedSize(true);
mProfileRecycler.setLayoutManager(new LinearLayoutManager(mActivityContext));
//viewModel.usersList.observe(this, mProfileAdapter::submitList);
//viewModel.getPagedListObservable().observe(this, new Observer<PagedList<User>>() {
viewModel.usersList.observe(this, new Observer < PagedList < User >> () {#Override
public void onChanged(#Nullable PagedList < User > items) {
System.out.println("onChanged");
if (items != null) {
new java.util.Timer().schedule(
new java.util.TimerTask() {#Override
public void run() {
// your code here
Log.d(TAG, "submitList size" + items.size());
mProfileAdapter.submitList(items);
}
},
5000);
}
}
});
mProfileRecycler.setAdapter(mProfileAdapter);
return fragView;
}
Update:
Well, I found a better solution than delaying submitList for 5 second. I decided to use a while loop until the data is received from firebase database, once items.size() is greater than 0 I stops the loop and submit the list to the adapter.
public class MainFragment extends Fragment {
private final static String TAG = MainFragment.class.getSimpleName();
private RecyclerView mUsersRecycler;
private ArrayList<User> mUserArrayList;
private UsersAdapter mUsersAdapter;
private Context mActivityContext;
private Activity activity;
private UsersViewModel viewModel;
public MainFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
*/
public static MainFragment newInstance(String param1, String param2) {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
//fragment.setArguments(args);
return fragment;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Initiate viewModel for this fragment instance
viewModel = ViewModelProviders.of(this).get(UsersViewModel.class);
//viewModel.getPagedListObservable().observe(this, new Observer<PagedList<User>>() {
viewModel.usersList.observe(this, new Observer<PagedList<User>>() {
#Override
public void onChanged(#Nullable final PagedList<User> items) {
System.out.println("mama onChanged");
if (items != null ){
// Create new Thread to loop until items.size() is greater than 0
new Thread(new Runnable() {
int sleepCounter = 0;
#Override
public void run() {
try {
while(items.size()==0) {
//Keep looping as long as items size is 0
Thread.sleep(20);
Log.d(TAG, "sleep 1000. size= "+items.size()+" sleepCounter="+sleepCounter++);
if(sleepCounter == 1000){
break;
}
//handler.post(this);
}
//Now items size is greater than 0, let's submit the List
Log.d(TAG, "after sleep finished. size= "+items.size());
if(items.size() == 0 && sleepCounter == 1000){
// If we submit List after loop is finish with 0 results
// we may erase another results submitted via newer thread
Log.d(TAG, "Loop finished with 0 items. Don't submitList");
}else{
Log.d(TAG, "submitList");
mUsersAdapter.submitList(items);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
});
}
}
I have write this simple code for getting tweets from twitter
public static void main(String[] args) throws TwitterException {
// TODO code application logic here
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("**********")
.setOAuthConsumerSecret("**************")
.setOAuthAccessToken("***************")
.setOAuthAccessTokenSecret("**************");
TwitterFactory tf= new TwitterFactory(cb.build());
twitter4j.Twitter tw= tf.getInstance();
List<Status> statuses = tw.getHomeTimeline();
for(Status status1 : statuses){
System.out.println(status1.getUser().getName()+ " : "+ status1.getText());
}
But I want to get about 4000 tweets in urdu language. I don't know how to do. please help me
With that code you will only get Tweets from the Timeline of the register user, from Twitter4j:
ResponseList getHomeTimeline() Returns the 20 most recent
statuses, including retweets, posted by the authenticating user and
that user's friends. This is the equivalent of /timeline/home on the
Web."
If you want to get at least 4000 tweets in urdu you could do several things, for example you could get the sample stream from Twitter:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("**********")
.setOAuthConsumerSecret("**************")
.setOAuthAccessToken("***************")
.setOAuthAccessTokenSecret("**************");
TwitterStream twitterStream = new TwitterStreamFactory(cb.build())
.getInstance();
StatusListener listener = new StatusListener() {
#Override
public void onStatus(Status status) {
System.out.println("#"+status.getUser().getScreenName()+": "+status.getText());
// HERE YOU STORE YOUR TWEETS
}
#Override
public void onException(Exception ex) {
ex.printStackTrace();
}
#Override
public void onDeletionNotice(StatusDeletionNotice arg0) {
// TODO Auto-generated method stub
}
#Override
public void onScrubGeo(long arg0, long arg1) {
}
#Override
public void onStallWarning(StallWarning arg0) {
// TODO Auto-generated method stub
System.out.println(arg0);
}
#Override
public void onTrackLimitationNotice(int arg0) {
// TODO Auto-generated method stub
System.out.println(arg0);
}
};
twitterStream.addListener(listener);
twitterStream.sample("ur");
Until you get the 4000 tweets that you want.
You can use twitter search instead of just getting specific user's tweets. What I have done for getting tweets in Turkish language is just using twitter's advanced search feature. In my situation adding lang:tr after specified search phrase returned tweets in Turkish language to me. You can use lang:ur instead of lang:tr for getting tweets in Urdu Language.
Here is my code:
public class TwitterDataGetter implements Runnable {
private Thread twitterDataGetterThread;
final static Logger logger = Logger.getLogger(TwitterDataGetter.class);
public TwitterDataGetter() {
try {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("your consumer key");
cb.setOAuthConsumerSecret("your consumer secret");
cb.setOAuthAccessToken("your access token");
cb.setOAuthAccessTokenSecret("your access token secret");
cb.setIncludeEntitiesEnabled(true);
this.twitter = new TwitterFactory(cb.build()).getInstance();
logger.info("Twitter API Configuration Successful");
} catch (Exception e) {
logger.error("Twitter API Configuration Error", e);
}
}
public void getTweet(String keyword){
List<Status> statuses = null;
Query query;
QueryResult result;
try {
query = new Query(keyword + " lang:tr");
query.setCount(100);
do {
final long startTime = System.nanoTime();
result = twitter.search(query);
statuses = result.getTweets();
for (Status status : statuses) {
System.out.println(status.getText());
}
final long duration = System.nanoTime() - startTime;
if((5500 - duration/1000000) > 0){
Thread.sleep((5500 - duration/1000000));
}
} while ((query = result.nextQuery()) != null);
} catch (TwitterException e) {
logger.error("TwitterException", e);
} catch (InterruptedException e) {
logger.error("InterruptedException", e);
throw new RuntimeException(e);
}
}
#Override
public void run() {
while(true){
this.getTweet("");
}
}
public void start(){
if(twitterDataGetterThread == null){
twitterDataGetterThread = new Thread(this, "Twitter Thread");
}
twitterDataGetterThread.start();
logger.info("Twitter Thread started");
}
}
Calling getTweet(String keyword) method with empty string returns latest tweets in your language without filtering them with any keyword.
Hope it helps.
PS: you can also check search results for your language from this link https://twitter.com/search?f=tweets&vertical=default&q=lang%3Aur
I want to stream tweets from twitter in my java application. I am currently able to do that using Twitter4J.
Here is my code sample -
public static void main(String args[])
{
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("PEBF3A1wUnNLfT83jpjGBEVNn");
cb.setOAuthConsumerSecret("Cqcuw6xyQ2tVtkGdy76s9fQuDigyDuJwxrgMETNhfuORloNFju");
cb.setOAuthAccessToken("2492966954-Fut0P36Enh0V1UAAVODUHSTGvYKy4lscWIEpaej");
cb.setOAuthAccessTokenSecret("x8onfYsnZvgImnyLVd1ncwvMhwNtrieU16gTkywUZOzpP");
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new StatusListener(){
public void onStatus(Status status)
{
System.out.println();
System.out.println("*****************************************************************");
User user = status.getUser();
// gets Username
String username = status.getUser().getScreenName();
System.out.println(username);
String profileLocation = user.getLocation();
System.out.println(profileLocation);
long tweetId = status.getId();
System.out.println(tweetId);
String content = status.getText();
System.out.println(content +"\n");
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
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
}
};
List<String> queries = new ArrayList<String>();
queries.add("#carb0nx");
twitterStream.addListener(listener);
//twitterStream.firehose(20);
String[] trackQueries = (String[]) queries.toArray(new String[queries.size()]);
FilterQuery filterQuery = new FilterQuery();
twitterStream.filter(filterQuery.track(trackQueries));
}
The above program fetches only the tweets which are getting added for the hashtag after running the program. I want to get all the older tweets as well before getting the newly added tweets.
Thanks in advance.enter code here
The code you have posted uses streaming api. So you will get only new tweets.
Check out this example for old tweets:
https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/search/SearchTweets.java
Also check out https://dev.twitter.com/docs/rate-limiting/1.1 for REST API Rate Limiting
I use the twitter4j query interface to filter tweets http://twitter4j.org/javadoc/twitter4j/Query.html. But the twitter spout in https://github.com/nathanmarz/storm-starter/blob/master/src/jvm/storm/starter/spout/TwitterSampleSpout.java:43 uses queue.offer(status). I don't have a reference to Status, how do I integrate these API(s) to process live tweets.
This is what we have been using successfully to filter tweets:
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
queue = new LinkedBlockingQueue<Status>(1000);
_collector = collector;
StatusListener listener = new StatusListener() {
public void onStatus(Status status) {
queue.offer(status);
}
public void onDeletionNotice(StatusDeletionNotice sdn) {
}
public void onTrackLimitationNotice(int i) {
}
public void onScrubGeo(long l, long l1) {
}
public void onException(Exception e) {
}
};
TwitterStreamFactory fact = new TwitterStreamFactory(new ConfigurationBuilder().setUser(_username).setPassword(_pwd).build());
_twitterStream = fact.getInstance();
_twitterStream.addListener(listener);
_twitterStream.filter(new FilterQuery().track(TERMS_TO_TRACK).setIncludeEntities(true));
}