Polling user's hometimline to get tweet updates; Need to fix #friends

This commit is contained in:
Sarang Bharadwaj 2012-06-07 01:37:01 +05:30
parent f8a183661c
commit 728517cf23
6 changed files with 51 additions and 17 deletions

View file

@ -2,11 +2,9 @@
DEFINE_LOGGER(logger, "TimelineRequest")
void TimelineRequest::run()
{
bool success;
if(userRequested != "") success = twitObj->timelineUserGet(false, false, 20, userRequested, false);
else success = twitObj->timelineHomeGet();
else success = twitObj->timelineHomeGet(since_id);
replyMsg = "";
if(success) {
@ -20,15 +18,20 @@ void TimelineRequest::run()
std::vector<Status> tweets = getTimeline(replyMsg);
timeline = "\n";
for(int i=0 ; i<tweets.size() ; i++) {
timeline += tweets[i].getTweet() + "\n";
if(tweets.size() && (since_id == "" ||
(since_id != "" && tweets[0].getID() != np->getMostRecentTweetID(user)) )) {
for(int i=0 ; i<tweets.size() ; i++) {
timeline += tweets[i].getTweet() + "\n";
}
np->updateUsersLastTweetID(user, tweets[0].getID());
}
}
}
void TimelineRequest::finalize()
{
if(replyMsg.length()) {
if(success && timeline != "\n") {
std::string error = getErrorMessage(replyMsg);
if(error.length()) {
np->handleMessage(user, "twitter-account", error);

View file

@ -5,6 +5,7 @@
#include "../libtwitcurl/twitcurl.h"
#include "../TwitterResponseParser.h"
#include "transport/networkplugin.h"
#include "../TwitterPlugin.h"
#include "transport/logging.h"
#include <string>
#include <iostream>
@ -18,14 +19,17 @@ class TimelineRequest : public Thread
std::string userRequested;
std::string replyMsg;
std::string timeline;
NetworkPlugin *np;
std::string since_id;
TwitterPlugin *np;
bool success;
public:
TimelineRequest(NetworkPlugin *_np, twitCurl *obj, const std::string &_user, const std::string &_user2) {
TimelineRequest(TwitterPlugin *_np, twitCurl *obj, const std::string &_user, const std::string &_user2, const std::string &_since_id) {
twitObj = obj->clone();
np = _np;
user = _user;
userRequested = _user2;
since_id = _since_id;
}
~TimelineRequest() {

View file

@ -108,9 +108,13 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
if(legacyName == "twitter-account") {
char ch;
std::string cmd = "", data = "";
std::istringstream in(message.c_str());
in >> cmd >> data;
int i;
for(i=0 ; i<message.size() && message[i] != ' '; i++) cmd += message[i];
while(i<message.size() && message[i] == ' ') i++;
data = message.substr(i);
//handleMessage(user, "twitter-account", cmd + " " + data);
@ -121,7 +125,7 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
tp->runAsThread(new DirectMessageRequest(np, sessions[user], user, username, data));
}
else if(cmd == "#status") tp->runAsThread(new StatusUpdateRequest(np, sessions[user], user, data));
else if(cmd == "#timeline") tp->runAsThread(new TimelineRequest(np, sessions[user], user, data));
else if(cmd == "#timeline") tp->runAsThread(new TimelineRequest(np, sessions[user], user, data, ""));
else if(cmd == "#friends") tp->runAsThread(new FetchFriends(np, sessions[user], user));
else handleMessage(user, "twitter-account", "Unknown command! Type #help for a list of available commands.");
}
@ -145,7 +149,7 @@ void TwitterPlugin::pollForTweets()
std::set<std::string>::iterator it = onlineUsers.begin();
while(it != onlineUsers.end()) {
std::string user = *it;
tp->runAsThread(new TimelineRequest(np, sessions[user], user, ""));
tp->runAsThread(new TimelineRequest(np, sessions[user], user, "", mostRecentTweetID[user]));
it++;
}
m_timer->start();
@ -237,4 +241,17 @@ void TwitterPlugin::pinExchangeComplete(const std::string user, const std::strin
sessions[user]->getOAuth().setOAuthTokenSecret( OAuthAccessTokenSecret );
connectionState[user] = CONNECTED;
onlineUsers.insert(user);
mostRecentTweetID[user] = "";
}
void TwitterPlugin::updateUsersLastTweetID(const std::string user, const std::string ID)
{
boost::mutex::scoped_lock lock(userlock);
mostRecentTweetID[user] = ID;
}
std::string TwitterPlugin::getMostRecentTweetID(const std::string user)
{
boost::mutex::scoped_lock lock(userlock);
return mostRecentTweetID[user];
}

View file

@ -83,6 +83,10 @@ class TwitterPlugin : public NetworkPlugin {
void OAuthFlowComplete(const std::string user, twitCurl *obj);
void pinExchangeComplete(const std::string user, const std::string OAuthAccessTokenKey, const std::string OAuthAccessTokenSecret);
void updateUsersLastTweetID(const std::string user, const std::string ID);
std::string getMostRecentTweetID(const std::string user);
private:
enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
@ -99,6 +103,7 @@ class TwitterPlugin : public NetworkPlugin {
ThreadPool *tp;
std::map<std::string, twitCurl*> sessions;
std::map<std::string, status> connectionState;
std::map<std::string, std::string> mostRecentTweetID;
std::set<std::string> onlineUsers;
};

View file

@ -464,11 +464,16 @@ bool twitCurl::timelinePublicGet()
* response by twitter. Use getLastWebResponse() for that.
*
*--*/
bool twitCurl::timelineHomeGet()
bool twitCurl::timelineHomeGet(std::string sinceId)
{
std::string buildUrl = twitterDefaults::TWITCURL_HOME_TIMELINE_URL +
twitCurlDefaults::TWITCURL_EXTENSIONFORMATS[m_eApiFormatType];
if( sinceId.length() )
{
buildUrl += twitCurlDefaults::TWITCURL_URL_SEP_QUES + twitCurlDefaults::TWITCURL_SINCEID + sinceId;
}
/* Perform GET */
return performGet( twitterDefaults::TWITCURL_HOME_TIMELINE_URL +
twitCurlDefaults::TWITCURL_EXTENSIONFORMATS[m_eApiFormatType] );
return performGet( buildUrl );
}
/*++

View file

@ -149,7 +149,7 @@ public:
bool statusDestroyById( std::string& statusId /* in */ );
/* Twitter timeline APIs */
bool timelineHomeGet();
bool timelineHomeGet(std::string sinceId = "" /* in */);
bool timelinePublicGet();
bool timelineFriendsGet();
bool timelineUserGet( bool trimUser /* in */, bool includeRetweets /* in */, unsigned int tweetCount /* in */, std::string userInfo = "" /* in */, bool isUserId = false /* in */ );