Changeset - f87f8cc356a1
[Not reviewed]
0 1 4
Sarang Bharadwaj - 13 years ago 2012-06-02 18:06:20
sarang.bh@gmail.com
Added threaded timeline request and fetch friends request
5 files changed with 146 insertions and 4 deletions:
0 comments (0 inline, 0 general)
backends/twitter/Requests/FetchFriends.cpp
Show inline comments
 
new file 100644
 
#include "FetchFriends.h"
 
DEFINE_LOGGER(logger, "FetchFriends")
 

	
 
void FetchFriends::run() 
 
{	
 
	replyMsg = ""; 
 
	if( twitObj.friendsIdsGet(twitObj.getTwitterUsername())) {
 
		
 
		while(replyMsg.length() == 0) {
 
			twitObj.getLastWebResponse( replyMsg );
 
		}
 

	
 
		LOG4CXX_INFO(logger, user << " - " << replyMsg.length() << " " << replyMsg << "\n" );
 

	
 
		std::vector<std::string> IDs = getIDs( replyMsg );
 
		
 
		twitObj.userLookup(IDs, true);
 
		twitObj.getLastWebResponse( replyMsg );
 

	
 
		LOG4CXX_INFO(logger, user << " - UserLookUp web response - " << replyMsg.length() << " " << replyMsg << "\n" );
 

	
 
		std::vector<User> users = getUsers( replyMsg );
 
		
 
		userlist = "\n***************USER LIST****************\n";
 
		for(int i=0 ; i < users.size() ; i++) {
 
			userlist += "*)" + users[i].getUserName() + " (" + users[i].getScreenName() + ")\n";
 
		}	
 
		userlist += "***************************************\n";	
 

	
 
	}
 
}
 

	
 
void FetchFriends::finalize()
 
{
 
	if(replyMsg != "" ) {
 
		np->handleMessage(user, "twitter-account", userlist);
 
	} else {
 
		twitObj.getLastCurlError( replyMsg );
 
		LOG4CXX_INFO(logger, user << " - friendsIdsGet error - " << replyMsg );
 
	}	
 
}
backends/twitter/Requests/FetchFriends.h
Show inline comments
 
new file 100644
 
#ifndef FRIENDS_H
 
#define FRIENDS_H
 

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

	
 
using namespace Transport;
 

	
 
class FetchFriends : public Thread
 
{
 
	twitCurl twitObj;
 
	std::string user;
 
	std::string replyMsg;
 
	std::string userlist;
 
	NetworkPlugin *np;
 

	
 
	public:
 
	FetchFriends(NetworkPlugin *_np, twitCurl *obj, const std::string &_user) {
 
		twitObj = *obj;
 
		np = _np;
 
		user = _user;
 
	}
 

	
 
	void run();
 
	void finalize();
 
};
 
#endif
backends/twitter/Requests/TimelineRequest.cpp
Show inline comments
 
new file 100644
 
#include "TimelineRequest.h"
 
DEFINE_LOGGER(logger, "TimelineRequest")
 
void TimelineRequest::run()
 
{	
 
	replyMsg = ""; 
 
	if( twitObj.timelinePublicGet() ) {	
 
		LOG4CXX_INFO(logger, "Sending timeline request for user " << user)
 

	
 
		while(replyMsg.length() == 0) {
 
			twitObj.getLastWebResponse( replyMsg );
 
		}
 

	
 
		LOG4CXX_INFO(logger, user << " - " << replyMsg.length() << " " << replyMsg << "\n" );
 
		
 
		std::vector<Status> tweets = getTimeline(replyMsg);
 
		timeline = "\n";
 
		for(int i=0 ; i<tweets.size() ; i++) {
 
			timeline += tweets[i].getTweet() + "\n";
 
		}
 
	}
 
}
 

	
 
void TimelineRequest::finalize()
 
{
 
	if(replyMsg.length()) {
 
		LOG4CXX_INFO(logger, user << "'s timeline\n" << replyMsg);
 
		np->handleMessage(user, "twitter-account", timeline); //send timeline
 
	}
 
	else {
 
		twitObj.getLastCurlError( replyMsg );
 
		LOG4CXX_ERROR(logger, user << " - " << replyMsg );
 
	}
 
}
backends/twitter/Requests/TimelineRequest.h
Show inline comments
 
new file 100644
 
#ifndef TIMELINE_H
 
#define TIMELINE_H
 

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

	
 
using namespace Transport;
 

	
 
class TimelineRequest : public Thread
 
{
 
	twitCurl twitObj;
 
	std::string user;
 
	std::string replyMsg;
 
	std::string timeline;
 
	NetworkPlugin *np;
 

	
 
	public:
 
	TimelineRequest(NetworkPlugin *_np, twitCurl *obj, const std::string &_user) {
 
		twitObj = *obj;
 
		np = _np;
 
		user = _user;
 
	}
 

	
 
	void run();
 
	void finalize();
 
};
 
#endif
backends/twitter/main.cpp
Show inline comments
 
@@ -31,6 +31,8 @@
 
#include "ThreadPool.h"
 
#include "Requests/StatusUpdateRequest.h"
 
#include "Requests/DirectMessageRequest.h"
 
#include "Requests/TimelineRequest.h"
 
#include "Requests/FetchFriends.h"
 

	
 
using namespace boost::filesystem;
 
using namespace boost::program_options;
 
@@ -405,14 +407,16 @@ class TwitterPlugin : public NetworkPlugin {
 
				else if(cmd[0] == '@') {
 
					std::string username = cmd.substr(1); 
 
					tp->runAsThread(new DirectMessageRequest(np, sessions[user], user, username, data));
 
					//handleDirectMessage(user, username, data);
 
				}
 
				else if(cmd == "#status") {
 
					tp->runAsThread(new StatusUpdateRequest(np, sessions[user], user, data));
 
					//handleStatusUpdate(user, data);
 
				}
 
				//else if(cmd == "#timeline") fetchTimeline(user);
 
				//else if(cmd == "#friends") fetchFriends(user);
 
				else if(cmd == "#timeline") {
 
					tp->runAsThread(new TimelineRequest(np, sessions[user], user));
 
				}
 
				else if(cmd == "#friends") {
 
					tp->runAsThread(new FetchFriends(np, sessions[user], user));
 
				}
 
			}
 
		}
 

	
0 comments (0 inline, 0 general)