Changeset - ae2c0acb5058
[Not reviewed]
0 2 0
Sarang Bharadwaj - 13 years ago 2012-05-30 20:14:41
sarang.bh@gmail.com
Directed Message
2 files changed with 33 insertions and 4 deletions:
0 comments (0 inline, 0 general)
backends/twitter/TwitterResponseParser.cpp
Show inline comments
 
#include "TwitterResponseParser.h"
 
#include "transport/logging.h"
 

	
 
DEFINE_LOGGER(logger, "TwitterResponseParser")
 

	
 
User getUser(const Swift::ParserElement::ref &element, const std::string xmlns) 
 
{
 
	User user;
 
	if(element->getName() != "user") {
 
		std::cerr << "Not a user element!" << std::endl;
 
		LOG4CXX_ERROR(logger, "Not a user element!")
 
		return user;
 
	}
 

	
 
	user.setUserID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
 
	user.setScreenName( std::string( element->getChild(TwitterReponseTypes::screen_name, xmlns)->getText() ) );
 
	user.setUserName( std::string( element->getChild(TwitterReponseTypes::name, xmlns)->getText() ) );
 
	user.setNumberOfTweets( atoi(element->getChild(TwitterReponseTypes::statuses_count, xmlns)->getText().c_str()) );
 
	return user;
 
}
 

	
 
Status getStatus(const Swift::ParserElement::ref &element, const std::string xmlns) 
 
{
 
	Status status;
 
	if(element->getName() != "status") {
 
		std::cerr << "Not a status element!" << std::endl;
 
		LOG4CXX_ERROR(logger, "Not a status element!")
 
		return status;
 
	}
 

	
 
	status.setCreationTime( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) );
 
	status.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
 
	status.setTweet( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) );
 
	status.setTruncated( std::string( element->getChild(TwitterReponseTypes::truncated, xmlns)->getText() )=="true" );
 
	status.setReplyToStatusID( std::string( element->getChild(TwitterReponseTypes::in_reply_to_status_id, xmlns)->getText() ) );
 
	status.setReplyToUserID( std::string( element->getChild(TwitterReponseTypes::in_reply_to_user_id, xmlns)->getText() ) );
 
	status.setReplyToScreenName( std::string( element->getChild(TwitterReponseTypes::in_reply_to_screen_name, xmlns)->getText() ) );
 
	status.setUserData( getUser(element->getChild(TwitterReponseTypes::user, xmlns), xmlns) );
 
	status.setRetweetCount( atoi( element->getChild(TwitterReponseTypes::retweet_count, xmlns)->getText().c_str() ) );
 
	status.setFavorited( std::string( element->getChild(TwitterReponseTypes::favorited, xmlns)->getText() )=="true" );
 
	status.setRetweeted( std::string( element->getChild(TwitterReponseTypes::retweeted, xmlns)->getText() )=="true" );
 
	return status;
 
}
 

	
 
std::vector<Status> getTimeline(std::string &xml)
 
{
 
	std::vector<Status> statuses;
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 
	
 
	if(rootElement->getName() != "statuses") {
 
		std::cerr << "XML doesnt correspond to timline\n";
 
		LOG4CXX_ERROR(logger, "XML doesnt correspond to timline")
 
		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;
 

	
 
	for(int i = 0; i <  children.size() ; i++) {
 
		const Swift::ParserElement::ref status = children[i];
 
		statuses.push_back(getStatus(status, xmlns));
 
	}
 
	return statuses;
backends/twitter/main.cpp
Show inline comments
 
@@ -194,24 +194,48 @@ class TwitterPlugin : public NetworkPlugin {
 
						LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
 
						handleLogoutRequest(user, "");
 
						return;
 
					}
 

	
 
					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");
 
				}
 

	
 
				if(cmd == "#help") {
 
					std::string helpMsg = "\nHELP\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);
 
				}
 

	
 
				if(cmd[0] == '@') {
 
					std::string username = cmd.substr(1);
 
					if(sessions[user]->directMessageSend(username, data, false) == false) {
 
						LOG4CXX_ERROR(logger, "Error while sending directed message to user " << username );
 
						return;
 
					}
 

	
 
					LOG4CXX_INFO(logger, "Sending " << data << " to " << username)
 

	
 
					std::string replyMsg;
 
					sessions[user]->getLastWebResponse( replyMsg );
 
					LOG4CXX_INFO(logger, replyMsg);
 
				}
 

	
 
				if(cmd == "status") {
 
					if(connectionState[user] != CONNECTED) {
 
						LOG4CXX_ERROR(logger, "Trying to update status for " << user << " when not connected!");
 
						return;
 
					}
 

	
 
					std::string replyMsg; 
 
					if( sessions[user]->statusUpdate( data ) ) {
 
						replyMsg = "";
 
						while(replyMsg.length() == 0) {
 
							sessions[user]->getLastWebResponse( replyMsg );
 
						}
 
@@ -229,27 +253,29 @@ class TwitterPlugin : public NetworkPlugin {
 
				if(cmd == "#timeline") {
 
					if(connectionState[user] != CONNECTED) {
 
						LOG4CXX_ERROR(logger, "Trying to update status for " << user << " when not connected!");
 
						return;
 
					}
 
					
 
					std::string replyMsg; 
 
					if( sessions[user]->timelineHomeGet()/*(false, false, 20, sessions[user]->getTwitterUsername(), true)*/ ) {
 
						sessions[user]->getLastWebResponse( replyMsg );
 
						LOG4CXX_INFO(logger, "twitCurl::timeline web response: " << replyMsg );
 
						
 
						std::vector<Status> tweets = getTimeline(replyMsg);
 
						std::string timeline = "\n";
 
						for(int i=0 ; i<tweets.size() ; i++) {
 
							handleMessage(user, "twitter-account", tweets[i].getTweet() + "\n");
 
							timeline += tweets[i].getTweet() + "\n";
 
						}
 
						handleMessage(user, "twitter-account", timeline);
 
					} else {
 
						sessions[user]->getLastCurlError( replyMsg );
 
						LOG4CXX_INFO(logger, "twitCurl::timeline error: " << replyMsg );
 
					}
 

	
 
				}
 
			}
 
		}
 

	
 
		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)