From 26e5289cefeb2d03370a17ec1577d27752bb8f44 Mon Sep 17 00:00:00 2001 From: Sarang Bharadwaj Date: Thu, 31 May 2012 20:19:20 +0530 Subject: [PATCH] Fetching the user's friends - initial code --- backends/twitter/TwitterResponseParser.cpp | 21 ++++++++++- backends/twitter/TwitterResponseParser.h | 3 ++ backends/twitter/main.cpp | 44 ++++++++++++++++++++-- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/backends/twitter/TwitterResponseParser.cpp b/backends/twitter/TwitterResponseParser.cpp index 1f8690a3..2e403da6 100644 --- a/backends/twitter/TwitterResponseParser.cpp +++ b/backends/twitter/TwitterResponseParser.cpp @@ -46,7 +46,7 @@ std::vector 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 getTimeline(std::string &xml) } return statuses; } + +std::vector getIDs(std::string &xml) +{ + std::vector 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 ids = rootElement->getChild(TwitterReponseTypes::ids, xmlns)->getChildren(TwitterReponseTypes::id, xmlns); + + for(int i=0 ; igetText() )); + } + return IDs; +} diff --git a/backends/twitter/TwitterResponseParser.h b/backends/twitter/TwitterResponseParser.h index 3eb9b507..6339d0e1 100644 --- a/backends/twitter/TwitterResponseParser.h +++ b/backends/twitter/TwitterResponseParser.h @@ -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 getTimeline(std::string &xml); +std::vector 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 diff --git a/backends/twitter/main.cpp b/backends/twitter/main.cpp index cd49c0d1..9abf8965 100644 --- a/backends/twitter/main.cpp +++ b/backends/twitter/main.cpp @@ -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: - Update your status\n" + + "#status: - Update your status\n" + "#timeline - Retrieve your timeline\n" + "@: - Send a directed message to the user \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 IDs = getIDs( replyMsg ); + for(int i=0 ; i tweets = getTimeline(replyMsg); + std::string timeline = "\n"; + for(int i=0 ; igetLastCurlError( 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); } }