#friends - Fetch and display the list of friends

This commit is contained in:
Sarang Bharadwaj 2012-05-31 23:04:31 +05:30
parent 26e5289cef
commit 9111a39937
5 changed files with 84 additions and 21 deletions

View file

@ -52,7 +52,6 @@ std::vector<Status> getTimeline(std::string &xml)
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;
for(int i = 0; i < children.size() ; i++) {
const Swift::ParserElement::ref status = children[i];
@ -61,6 +60,26 @@ std::vector<Status> getTimeline(std::string &xml)
return statuses;
}
std::vector<User> getUsers(std::string &xml)
{
std::vector<User> users;
Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
if(rootElement->getName() != TwitterReponseTypes::users) {
LOG4CXX_ERROR(logger, "XML doesn't correspond to user list")
return users;
}
const std::string xmlns = rootElement->getNamespace();
const std::vector<Swift::ParserElement::ref> children = rootElement->getChildren(TwitterReponseTypes::user, xmlns);
for(int i = 0 ; i < children.size() ; i++) {
const Swift::ParserElement::ref user = children[i];
users.push_back(getUser(user, xmlns));
}
return users;
}
std::vector<std::string> getIDs(std::string &xml)
{
std::vector<std::string> IDs;

View file

@ -23,6 +23,7 @@ namespace TwitterReponseTypes
const std::string favorited = "favorited";
const std::string retweeted = "retweeted";
const std::string user = "user";
const std::string users = "users";
const std::string status = "status";
};
@ -96,6 +97,7 @@ class Status
std::vector<Status> getTimeline(std::string &xml);
std::vector<std::string> getIDs(std::string &xml);
std::vector<User> getUsers(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

View file

@ -584,6 +584,39 @@ bool twitCurl::userGet( std::string& userInfo, bool isUserId )
return retVal;
}
/*++
* @method: twitCurl::userLookup
*
* @description: method to get a number of user's profiles
*
* @input: userInfo - vector of screen names or user ids
* isUserId - true if userInfo contains an id
*
* @output: true if POST is success, otherwise false. This does not check http
* response by twitter. Use getLastWebResponse() for that.
*
*--*/
bool twitCurl::userLookup( std::vector<std::string> &userInfo, bool isUserId )
{
bool retVal = false;
if( userInfo.size() )
{
std::string userIds = isUserId?twitCurlDefaults::TWITCURL_USERID : twitCurlDefaults::TWITCURL_SCREENNAME;
std::string sep = "";
for(int i=0 ; i<std::min(100U, userInfo.size()) ; i++, sep = ",")
userIds += sep + userInfo[i];
/* Set URL */
std::string buildUrl = twitterDefaults::TWITCURL_LOOKUPUSERS_URL + twitCurlDefaults::TWITCURL_EXTENSIONFORMATS[m_eApiFormatType];
std::cerr << buildUrl << " " << userIds << std::endl;
/* Perform POST */
retVal = performPost( buildUrl, userIds );
}
return retVal;
}
/*++
* @method: twitCurl::friendsGet
*

View file

@ -2,6 +2,8 @@
#define _TWITCURL_H_
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstring>
#include "oauthlib.h"
@ -69,6 +71,7 @@ namespace twitterDefaults
const std::string TWITCURL_USERTIMELINE_URL = "http://api.twitter.com/1/statuses/user_timeline";
/* Users URLs */
const std::string TWITCURL_LOOKUPUSERS_URL = "http://api.twitter.com/1/users/lookup";
const std::string TWITCURL_SHOWUSERS_URL = "http://api.twitter.com/1/users/show";
const std::string TWITCURL_SHOWFRIENDS_URL = "http://api.twitter.com/1/statuses/friends";
const std::string TWITCURL_SHOWFOLLOWERS_URL = "http://api.twitter.com/1/statuses/followers";
@ -154,6 +157,7 @@ public:
bool mentionsGet( std::string sinceId = "" /* in */ );
/* Twitter user APIs */
bool userLookup( std::vector<std::string> &userInfo /* in */, bool isUserId = false /* in */);
bool userGet( std::string& userInfo /* in */, bool isUserId = false /* in */ );
bool friendsGet( std::string userInfo = "" /* in */, bool isUserId = false /* in */ );
bool followersGet( std::string userInfo = "" /* in */, bool isUserId = false /* in */ );

View file

@ -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 for Access Token!")
LOG4CXX_ERROR(logger, user << ": Error while exchanging PIN for Access Token!")
handleLogoutRequest(user, "");
return;
}
@ -186,7 +186,7 @@ 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, user << ": Sent PIN " << data << " and obtained Access Token");
}
void printHelpMessage(const std::string &user) {
@ -203,11 +203,11 @@ class TwitterPlugin : public NetworkPlugin {
void handleDirectMessage(const std::string &user, std::string &username, std::string &data) {
if(sessions[user]->directMessageSend(username, data, false) == false) {
LOG4CXX_ERROR(logger, "Error while sending directed message to user " << username );
LOG4CXX_ERROR(logger, user << ": Error while sending directed message to user " << username );
return;
}
LOG4CXX_INFO(logger, "Sending " << data << " to " << username)
LOG4CXX_INFO(logger, user << ": Sending " << data << " to " << username)
std::string replyMsg;
sessions[user]->getLastWebResponse( replyMsg );
@ -226,10 +226,10 @@ class TwitterPlugin : public NetworkPlugin {
while(replyMsg.length() == 0) {
sessions[user]->getLastWebResponse( replyMsg );
}
LOG4CXX_INFO(logger, "twitCurl:statusUpdate web response: " << replyMsg );
LOG4CXX_INFO(logger, user << ": twitCurl:statusUpdate web response: " << replyMsg );
} else {
sessions[user]->getLastCurlError( replyMsg );
LOG4CXX_INFO(logger, "twitCurl::statusUpdate error: " << replyMsg );
LOG4CXX_INFO(logger, user << ": twitCurl::statusUpdate error: " << replyMsg );
}
LOG4CXX_INFO(logger, "Updated status for " << user << ": " << data);
}
@ -247,7 +247,7 @@ class TwitterPlugin : public NetworkPlugin {
sessions[user]->getLastWebResponse( replyMsg );
}
LOG4CXX_INFO(logger, "twitCurl::timeline web response: " << replyMsg.length() << " " << replyMsg << "\n" );
LOG4CXX_INFO(logger, user << ": twitCurl::timeline web response: " << replyMsg.length() << " " << replyMsg << "\n" );
std::vector<Status> tweets = getTimeline(replyMsg);
std::string timeline = "\n";
@ -259,7 +259,7 @@ class TwitterPlugin : public NetworkPlugin {
} else {
sessions[user]->getLastCurlError( replyMsg );
LOG4CXX_INFO(logger, "twitCurl::timeline error: " << replyMsg );
LOG4CXX_INFO(logger, user << ": twitCurl::timeline error: " << replyMsg );
}
}
@ -276,24 +276,29 @@ class TwitterPlugin : public NetworkPlugin {
sessions[user]->getLastWebResponse( replyMsg );
}
LOG4CXX_INFO(logger, "twitCurl::friendsIdsGet web response: " << replyMsg.length() << " " << replyMsg << "\n" );
LOG4CXX_INFO(logger, user << ": 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";
}
/*for(int i=0 ; i<IDs.size() ; i++) {
//LOG4CXX_INFO(logger, "ID #" << i+1 << ": " << IDs[i]);
}*/
sessions[user]->userLookup(IDs, true);
sessions[user]->getLastWebResponse( replyMsg );
LOG4CXX_INFO(logger, user << ": twitCurl::UserLookUp web response: " << replyMsg.length() << " " << replyMsg << "\n" );
handleMessage(user, "twitter-account", timeline);*/
std::vector<User> users = getUsers( replyMsg );
std::string userlist = "\n***************USER LIST****************\n";
for(int i=0 ; i < users.size() ; i++) {
userlist += "*)" + users[i].getUserName() + " (" + users[i].getScreenName() + ")\n";
}
userlist += "***************************************\n";
handleMessage(user, "twitter-account", userlist);
} else {
sessions[user]->getLastCurlError( replyMsg );
LOG4CXX_INFO(logger, "twitCurl::friendsIdsGet error: " << replyMsg );
LOG4CXX_INFO(logger, user << ": twitCurl::friendsIdsGet error: " << replyMsg );
}
}