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
 
@@ -46,7 +46,7 @@ std::vector<Status> getTimeline(std::string &xml)
 
	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;
 
	}
 

	
 
@@ -60,3 +60,22 @@ std::vector<Status> getTimeline(std::string &xml)
 
	}
 
	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
 
@@ -8,6 +8,8 @@
 
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";
 
@@ -93,6 +95,7 @@ class Status
 
};
 

	
 
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
 
@@ -166,7 +166,7 @@ class TwitterPlugin : public NetworkPlugin {
 
		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;
 
			}
 
@@ -186,14 +186,14 @@ class TwitterPlugin : public NetworkPlugin {
 
			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";
 
@@ -236,7 +236,7 @@ class TwitterPlugin : public NetworkPlugin {
 

	
 
		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;
 
			}
 
			
 
@@ -263,6 +263,41 @@ class TwitterPlugin : public NetworkPlugin {
 
			}
 
		}
 

	
 
		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 << ".");
 
@@ -277,6 +312,7 @@ class TwitterPlugin : public NetworkPlugin {
 
				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);
 
			}
 
		}
 

	
0 comments (0 inline, 0 general)