Changeset - 728517cf2312
[Not reviewed]
0 6 0
Sarang Bharadwaj - 13 years ago 2012-06-06 22:07:01
sarang.bh@gmail.com
Polling user's hometimline to get tweet updates; Need to fix #friends
6 files changed with 48 insertions and 14 deletions:
0 comments (0 inline, 0 general)
backends/twitter/Requests/TimelineRequest.cpp
Show inline comments
 
#include "TimelineRequest.h"
 
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) {	
 
		LOG4CXX_INFO(logger, "Sending timeline request for user " << user)
 

	
 
		while(replyMsg.length() == 0) {
 
@@ -17,21 +15,26 @@ void TimelineRequest::run()
 
		}
 

	
 
		LOG4CXX_INFO(logger, user << " - " << replyMsg.length() << " " << replyMsg << "\n" );
 
		
 
		std::vector<Status> tweets = getTimeline(replyMsg);
 
		timeline = "\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);
 
			LOG4CXX_INFO(logger, user << ": " << error);
 
		} else {	
 
			LOG4CXX_INFO(logger, user << "'s timeline\n" << replyMsg);
backends/twitter/Requests/TimelineRequest.h
Show inline comments
 
@@ -2,12 +2,13 @@
 
#define TIMELINE_H
 

	
 
#include "../ThreadPool.h"
 
#include "../libtwitcurl/twitcurl.h"
 
#include "../TwitterResponseParser.h"
 
#include "transport/networkplugin.h"
 
#include "../TwitterPlugin.h"
 
#include "transport/logging.h"
 
#include <string>
 
#include <iostream>
 

	
 
using namespace Transport;
 

	
 
@@ -15,20 +16,23 @@ class TimelineRequest : public Thread
 
{
 
	twitCurl *twitObj;
 
	std::string user;
 
	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() {
 
		//std::cerr << "*****Timeline request: DESTROYING twitObj****" << std::endl;
 
		delete twitObj;
 
	}
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
@@ -105,26 +105,30 @@ void TwitterPlugin::handleLogoutRequest(const std::string &user, const std::stri
 

	
 
void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml) 
 
{
 
	
 
	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);
 

	
 
		if(cmd == "#pin") tp->runAsThread(new PINExchangeProcess(np, sessions[user], user, data));
 
		else if(cmd == "#help") tp->runAsThread(new HelpMessageRequest(np, user));
 
		else if(cmd[0] == '@') {
 
			std::string username = cmd.substr(1); 
 
			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.");
 
	}
 
}
 

	
 
void TwitterPlugin::handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) 
 
@@ -142,13 +146,13 @@ void TwitterPlugin::handleBuddyRemovedRequest(const std::string &user, const std
 
void TwitterPlugin::pollForTweets()
 
{
 
	boost::mutex::scoped_lock lock(userlock);
 
	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();
 
}
 

	
 

	
 
@@ -234,7 +238,20 @@ void TwitterPlugin::pinExchangeComplete(const std::string user, const std::strin
 
	boost::mutex::scoped_lock lock(userlock);	
 
		
 
	sessions[user]->getOAuth().setOAuthTokenKey( OAuthAccessTokenKey );
 
	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];
 
}
backends/twitter/TwitterPlugin.h
Show inline comments
 
@@ -81,12 +81,16 @@ class TwitterPlugin : public NetworkPlugin {
 
		void initUserSession(const std::string user, const std::string password);
 
		
 
		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};
 
		
 
		Config *config;
 

	
 
		std::string consumerKey;
 
@@ -96,10 +100,11 @@ class TwitterPlugin : public NetworkPlugin {
 

	
 
		boost::mutex dblock, userlock;
 

	
 
		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;
 
};
 

	
 
#endif
backends/twitter/libtwitcurl/twitcurl.cpp
Show inline comments
 
@@ -461,17 +461,22 @@ bool twitCurl::timelinePublicGet()
 
* @input: none
 
*
 
* @output: true if GET is success, otherwise false. This does not check http
 
*          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 );
 
}
 
 
/*++
 
* @method: twitCurl::featuredUsersGet
 
*
 
* @description: method to get featured users
backends/twitter/libtwitcurl/twitcurl.h
Show inline comments
 
@@ -146,13 +146,13 @@ public:
 
    /* Twitter status APIs */
 
    bool statusUpdate( std::string& newStatus /* in */ );
 
    bool statusShowById( std::string& statusId /* in */ );
 
    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 */ );
 
    bool featuredUsersGet();
 
    bool mentionsGet( std::string sinceId = "" /* in */ );
 
0 comments (0 inline, 0 general)