Changeset - 9111a3993741
[Not reviewed]
0 5 0
Sarang Bharadwaj - 13 years ago 2012-05-31 19:34:31
sarang.bh@gmail.com
#friends - Fetch and display the list of friends
5 files changed with 84 insertions and 21 deletions:
0 comments (0 inline, 0 general)
backends/twitter/TwitterResponseParser.cpp
Show inline comments
 
@@ -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;
backends/twitter/TwitterResponseParser.h
Show inline comments
 
@@ -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
backends/twitter/libtwitcurl/twitcurl.cpp
Show inline comments
 
@@ -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
 
*
backends/twitter/libtwitcurl/twitcurl.h
Show inline comments
 
@@ -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 */ );
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 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]);
 
				}
 
				/*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" );
 

	
 
				std::vector<User> users = getUsers( replyMsg );
 
				
 
				/*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);*/
 
				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 );
 
			}
 
			
 
		}
0 comments (0 inline, 0 general)