Changeset - 26e5289cefeb
[Not reviewed]
0 3 0
Sarang Bharadwaj - 13 years ago 2012-05-31 16:49:20
sarang.bh@gmail.com
Fetching the user's friends - initial code
3 files changed with 63 insertions and 5 deletions:
0 comments (0 inline, 0 general)
backends/twitter/TwitterResponseParser.cpp
Show inline comments
 
@@ -43,13 +43,13 @@ Status getStatus(const Swift::ParserElement::ref &element, const std::string xml
 
std::vector<Status> getTimeline(std::string &xml)
 
{
 
	std::vector<Status> statuses;
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 
	
 
	if(rootElement->getName() != "statuses") {
 
		LOG4CXX_ERROR(logger, "XML doesnt correspond to timline")
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to timeline")
 
		return statuses;
 
	}
 

	
 
	const std::string xmlns = rootElement->getNamespace();
 
	const std::vector<Swift::ParserElement::ref> children = rootElement->getChildren(TwitterReponseTypes::status, xmlns);
 
//	const std::vector<Swift::ParserElement::ref>::iterator it;
 
@@ -57,6 +57,25 @@ std::vector<Status> getTimeline(std::string &xml)
 
	for(int i = 0; i <  children.size() ; i++) {
 
		const Swift::ParserElement::ref status = children[i];
 
		statuses.push_back(getStatus(status, xmlns));
 
	}
 
	return statuses;
 
}
 

	
 
std::vector<std::string> getIDs(std::string &xml)
 
{
 
	std::vector<std::string> IDs;
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 

	
 
	if(rootElement->getName() != TwitterReponseTypes::id_list) {
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to id_list");
 
		return IDs;
 
	}
 

	
 
	const std::string xmlns = rootElement->getNamespace();
 
	const std::vector<Swift::ParserElement::ref> ids = rootElement->getChild(TwitterReponseTypes::ids, xmlns)->getChildren(TwitterReponseTypes::id, xmlns);
 
	
 
	for(int i=0 ; i<ids.size() ; i++) {
 
		IDs.push_back(std::string( ids[i]->getText() ));
 
	}
 
	return IDs;
 
}
backends/twitter/TwitterResponseParser.h
Show inline comments
 
@@ -5,12 +5,14 @@
 
#include <iostream>
 
#include <vector>
 

	
 
namespace TwitterReponseTypes
 
{
 
	const std::string id = "id";
 
	const std::string id_list = "id_list";
 
	const std::string ids = "ids";
 
	const std::string name = "name";
 
	const std::string screen_name = "screen_name";
 
	const std::string statuses_count = "statuses_count";
 
	const std::string created_at = "created_at";
 
	const std::string text = "text";
 
	const std::string truncated = "truncated";
 
@@ -90,9 +92,10 @@ class Status
 
	void setRetweetCount(unsigned int rc) {retweet_count = rc;}
 
	void setFavorited(bool val) {favorited = val;}
 
	void setRetweeted(bool val) {retweeted = val;}
 
};
 

	
 
std::vector<Status> getTimeline(std::string &xml);
 
std::vector<std::string> getIDs(std::string &xml);
 
Status getStatus(const Swift::ParserElement::ref &element, const std::string xmlns);
 
User getUser(const Swift::ParserElement::ref &element, const std::string xmlns);
 
#endif
backends/twitter/main.cpp
Show inline comments
 
@@ -163,13 +163,13 @@ class TwitterPlugin : public NetworkPlugin {
 
			connectionState[user] = DISCONNECTED;
 
		}
 

	
 
		void handlePINExchange(const std::string &user, std::string &data) {
 
			sessions[user]->getOAuth().setOAuthPin( data );
 
			if (sessions[user]->oAuthAccessToken() == false) {
 
				LOG4CXX_ERROR(logger, "Error while exchanging PIN with AcessToken!")
 
				LOG4CXX_ERROR(logger, "Error while exchanging PIN for Access Token!")
 
				handleLogoutRequest(user, "");
 
				return;
 
			}
 
			
 
			std::string OAuthAccessTokenKey, OAuthAccessTokenSecret;
 
			sessions[user]->getOAuth().getOAuthTokenKey( OAuthAccessTokenKey );
 
@@ -183,20 +183,20 @@ class TwitterPlugin : public NetworkPlugin {
 
			}
 

	
 
			storagebackend->updateUserSetting((long)info.id, OAUTH_KEY, OAuthAccessTokenKey);	
 
			storagebackend->updateUserSetting((long)info.id, OAUTH_SECRET, OAuthAccessTokenSecret);	
 

	
 
			connectionState[user] = CONNECTED;
 
			LOG4CXX_INFO(logger, "Sent PIN " << data << " and obtained access token");
 
			LOG4CXX_INFO(logger, "Sent PIN " << data << " and obtained Access Token");
 
		}
 

	
 
		void printHelpMessage(const std::string &user) {
 
			std::string helpMsg = "";
 
			helpMsg = helpMsg
 
				    + "\nHELP\n"
 
					+ "status:<your status> - Update your status\n"
 
					+ "#status:<your status> - Update your status\n"
 
					+ "#timeline - Retrieve your timeline\n"
 
					+ "@<username>:<message> - Send a directed message to the user <username>\n"
 
					+ "#help - print this help message\n";
 

	
 
			handleMessage(user, "twitter-account", helpMsg);
 
		}
 
@@ -233,13 +233,13 @@ class TwitterPlugin : public NetworkPlugin {
 
			}
 
			LOG4CXX_INFO(logger, "Updated status for " << user << ": " << data);
 
		}
 

	
 
		void fetchTimeline(const std::string &user) {
 
			if(connectionState[user] != CONNECTED) {
 
				LOG4CXX_ERROR(logger, "Trying to update status for " << user << " when not connected!");
 
				LOG4CXX_ERROR(logger, "Trying to fetch timeline for " << user << " when not connected!");
 
				return;
 
			}
 
			
 
			std::string replyMsg = ""; 
 
			if( sessions[user]->timelineHomeGet()) {
 
				
 
@@ -260,12 +260,47 @@ class TwitterPlugin : public NetworkPlugin {
 
			} else {
 
				sessions[user]->getLastCurlError( replyMsg );
 
				LOG4CXX_INFO(logger, "twitCurl::timeline error: " << replyMsg );
 
			}
 
		}
 

	
 
		void fetchFriends(const std::string &user) {
 
			if(connectionState[user] != CONNECTED) {
 
				LOG4CXX_ERROR(logger, "Trying to fetch friends of " << user << " when not connected!");
 
				return;
 
			}
 
			
 
			std::string replyMsg = ""; 
 
			if( sessions[user]->friendsIdsGet(sessions[user]->getTwitterUsername())) {
 
				
 
				while(replyMsg.length() == 0) {
 
					sessions[user]->getLastWebResponse( replyMsg );
 
				}
 

	
 
				LOG4CXX_INFO(logger, "twitCurl::friendsIdsGet web response: " << replyMsg.length() << " " << replyMsg << "\n" );
 

	
 
				std::vector<std::string> IDs = getIDs( replyMsg );
 
				for(int i=0 ; i<IDs.size() ; i++) {
 
					LOG4CXX_INFO(logger, "ID #" << i+1 << ": " << IDs[i]);
 
				}
 
				
 
				/*std::vector<Status> tweets = getTimeline(replyMsg);
 
				std::string timeline = "\n";
 
				for(int i=0 ; i<tweets.size() ; i++) {
 
					timeline += tweets[i].getTweet() + "\n";
 
				}
 

	
 
				handleMessage(user, "twitter-account", timeline);*/
 

	
 
			} else {
 
				sessions[user]->getLastCurlError( replyMsg );
 
				LOG4CXX_INFO(logger, "twitCurl::friendsIdsGet error: " << replyMsg );
 
			}
 
			
 
		}
 

	
 

	
 
		void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "") {
 
			LOG4CXX_INFO(logger, "Sending message from " << user << " to " << legacyName << ".");
 
			if(legacyName == "twitter-account") {
 
				std::string cmd = message.substr(0, message.find(':'));
 
				std::string data = message.substr(message.find(':') + 1);
 
@@ -274,12 +309,13 @@ class TwitterPlugin : public NetworkPlugin {
 

	
 
				if(cmd == "#pin") handlePINExchange(user, data);
 
				else if(cmd == "#help") printHelpMessage(user);
 
				else if(cmd[0] == '@') {std::string username = cmd.substr(1); handleDirectMessage(user, username, data);}
 
				else if(cmd == "#status") handleStatusUpdate(user, data);
 
				else if(cmd == "#timeline") fetchTimeline(user);
 
				else if(cmd == "#friends") fetchFriends(user);
 
			}
 
		}
 

	
 
		void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector<std::string> &groups) {
 
			LOG4CXX_INFO(logger, user << ": Added buddy " << buddyName << ".");
 
			handleBuddyChanged(user, buddyName, alias, groups, pbnetwork::STATUS_ONLINE);
0 comments (0 inline, 0 general)