Changeset - 11937227ff60
[Not reviewed]
0 8 0
Sarang Bharadwaj - 13 years ago 2012-06-11 16:09:20
sarang.bh@gmail.com
Added Direct Message XML parser
8 files changed with 134 insertions and 16 deletions:
0 comments (0 inline, 0 general)
backends/twitter/Requests/DirectMessageRequest.cpp
Show inline comments
 
#include "DirectMessageRequest.h"
 
#include "../TwitterResponseParser.h"
 

	
 
DEFINE_LOGGER(logger, "DirectMessageRequest")
 

	
 
void DirectMessageRequest::run() 
 
{
 
	replyMsg = "";
 
	success = twitObj->directMessageSend(username, data, false);
 
	if(success) twitObj->getLastWebResponse( replyMsg );
 
	if(username != "") success = twitObj->directMessageSend(username, data, false);
 
	else success = twitObj->directMessageGet(data); /* data will contain sinceId */
 

	
 
	if(success) {
 
		twitObj->getLastWebResponse( replyMsg );
 
		if(username == "" ) messages = getDirectMessages( replyMsg );
 
	}
 
}
 

	
 
void DirectMessageRequest::finalize()
 
@@ -15,11 +19,11 @@ void DirectMessageRequest::finalize()
 
	if(!success) {
 
		LOG4CXX_ERROR(logger, user << ": Error while sending directed message to " << username );
 
		twitObj->getLastCurlError( replyMsg );
 
		callBack(user, replyMsg);
 
		callBack(user, messages, replyMsg);
 
	} else {
 
		std::string error = getErrorMessage(replyMsg);
 
		if(error.length()) LOG4CXX_ERROR(logger,  user << " - " << error)
 
		else LOG4CXX_INFO(logger, user << " - " << replyMsg)
 
		callBack(user, error);	
 
		callBack(user, messages, error);	
 
	}
 
}
backends/twitter/Requests/DirectMessageRequest.h
Show inline comments
 
@@ -2,6 +2,7 @@
 
#define DIRECT_MESSAGE
 

	
 
#include "../ThreadPool.h"
 
#include "../TwitterResponseParser.h"
 
#include "../libtwitcurl/twitcurl.h"
 
#include "transport/logging.h"
 
#include <string>
 
@@ -17,12 +18,13 @@ class DirectMessageRequest : public Thread
 
	std::string user;
 
	std::string username;
 
	std::string replyMsg;
 
	boost::function< void (std::string&, std::string&) > callBack;
 
	boost::function< void (std::string&, std::vector<DirectMessage>&, std::string&) > callBack;
 
	std::vector<DirectMessage> messages;
 
	bool success;
 

	
 
	public:
 
	DirectMessageRequest(twitCurl *obj, const std::string &_user, const std::string & _username, const std::string &_data,
 
			     		boost::function< void (std::string&, std::string&) >  cb) {
 
			     		boost::function< void (std::string&, std::vector<DirectMessage>&, std::string&) >  cb) {
 
		twitObj = obj->clone();
 
		data = _data;
 
		user = _user;
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
@@ -131,7 +131,7 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
 
		else if(cmd[0] == '@') {
 
			std::string username = cmd.substr(1); 
 
			tp->runAsThread(new DirectMessageRequest(sessions[user], user, username, data,
 
												     boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2)));
 
												     boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
 
		}
 
		else if(cmd == "#status") tp->runAsThread(new StatusUpdateRequest(np, sessions[user], user, data));
 
		else if(cmd == "#timeline") tp->runAsThread(new TimelineRequest(sessions[user], user, data, "",
 
@@ -143,7 +143,7 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
 

	
 
	else {
 
		tp->runAsThread(new DirectMessageRequest(sessions[user], user, legacyName, message,
 
												 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2)));
 
												 boost::bind(&TwitterPlugin::directMessageResponse, this, _1, _2, _3)));
 
	}
 
}
 

	
 
@@ -354,9 +354,25 @@ void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested,
 
	} else handleMessage(user, "twitter-account", errMsg);	
 
}
 

	
 
void TwitterPlugin::directMessageResponse(std::string &user, std::string &errMsg)
 
void TwitterPlugin::directMessageResponse(std::string &user, std::vector<DirectMessage> &messages, std::string &errMsg)
 
{
 
	if(errMsg.length()) {
 
		handleMessage(user, "twitter-account", std::string("Error while sending direct message! - ") + errMsg);	
 
		return;
 
	}
 
	
 
	if(!messages.size()) return;
 
	
 
	if(twitterMode == SINGLECONTACT) {
 
		std::string msglist = "\n***************MSG LIST****************\n";
 
		for(int i=0 ; i < messages.size() ; i++) {
 
			msglist += " - " + messages[i].getSenderData().getScreenName() + ": " + messages[i].getMessage() + "\n";
 
		}	
 
		msglist += "***************************************\n";
 
		handleMessage(user, "twitter-account", msglist);	
 
	} else if(twitterMode == MULTIPLECONTACT) {
 
		for(int i=0 ; i < messages.size() ; i++) {
 
			handleMessage(user, messages[i].getSenderData().getScreenName(), messages[i].getMessage());		
 
		}	
 
	}
 
}
backends/twitter/TwitterPlugin.h
Show inline comments
 
@@ -98,7 +98,7 @@ class TwitterPlugin : public NetworkPlugin {
 
		void populateRoster(std::string &user, std::vector<User> &friends, std::string &errMsg);
 
		void displayFriendlist(std::string &user, std::vector<User> &friends, std::string &errMsg);
 
		void displayTweets(std::string &user, std::string &userRequested, std::vector<Status> &tweets , std::string &errMsg);
 
		void directMessageResponse(std::string &user, std::string &errMsg);
 
		void directMessageResponse(std::string &user, std::vector<DirectMessage> &messages, std::string &errMsg);
 
		/***********************************************************************************/
 

	
 
	private:
backends/twitter/TwitterResponseParser.cpp
Show inline comments
 
@@ -6,7 +6,9 @@ DEFINE_LOGGER(logger, "TwitterResponseParser")
 
User getUser(const Swift::ParserElement::ref &element, const std::string xmlns) 
 
{
 
	User user;
 
	if(element->getName() != "user") {
 
	if(element->getName() != TwitterReponseTypes::user 
 
	   && element->getName() != TwitterReponseTypes::sender
 
	   && element->getName() != TwitterReponseTypes::recipient) {
 
		LOG4CXX_ERROR(logger, "Not a user element!")
 
		return user;
 
	}
 
@@ -40,6 +42,26 @@ Status getStatus(const Swift::ParserElement::ref &element, const std::string xml
 
	return status;
 
}
 

	
 
DirectMessage getDirectMessage(const Swift::ParserElement::ref &element, const std::string xmlns) 
 
{
 
	DirectMessage DM;
 
	if(element->getName() != TwitterReponseTypes::direct_message) {
 
		LOG4CXX_ERROR(logger, "Not a direct_message element!")
 
		return DM;
 
	}
 

	
 
	DM.setCreationTime( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) );
 
	DM.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
 
	DM.setMessage( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) );
 
	DM.setSenderID( std::string( element->getChild(TwitterReponseTypes::sender_id, xmlns)->getText() ) );
 
	DM.setRecipientID( std::string( element->getChild(TwitterReponseTypes::recipient_id, xmlns)->getText() ) );
 
	DM.setSenderScreenName( std::string( element->getChild(TwitterReponseTypes::sender_screen_name, xmlns)->getText() ) );
 
	DM.setRecipientScreenName( std::string( element->getChild(TwitterReponseTypes::recipient_screen_name, xmlns)->getText() ) );
 
	DM.setSenderData( getUser(element->getChild(TwitterReponseTypes::sender, xmlns), xmlns) );
 
	DM.setRecipientData( getUser(element->getChild(TwitterReponseTypes::recipient, xmlns), xmlns) );
 
	return DM;
 
}
 

	
 
std::vector<Status> getTimeline(std::string &xml)
 
{
 
	std::vector<Status> statuses;
 
@@ -60,6 +82,26 @@ std::vector<Status> getTimeline(std::string &xml)
 
	return statuses;
 
}
 

	
 
std::vector<DirectMessage> getDirectMessages(std::string &xml)
 
{
 
	std::vector<DirectMessage> DMs;
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 
	
 
	if(rootElement->getName() != TwitterReponseTypes::directmessages) {
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to direct-messages")
 
		return DMs;
 
	}
 

	
 
	const std::string xmlns = rootElement->getNamespace();
 
	const std::vector<Swift::ParserElement::ref> children = rootElement->getChildren(TwitterReponseTypes::direct_message, xmlns);
 

	
 
	for(int i = 0; i <  children.size() ; i++) {
 
		const Swift::ParserElement::ref dm = children[i];
 
		DMs.push_back(getDirectMessage(dm, xmlns));
 
	}
 
	return DMs;
 
}
 

	
 
std::vector<User> getUsers(std::string &xml)
 
{
 
	std::vector<User> users;
backends/twitter/TwitterResponseParser.h
Show inline comments
 
@@ -26,6 +26,14 @@ namespace TwitterReponseTypes
 
	const std::string users = "users";
 
	const std::string status = "status";
 
	const std::string error = "error";
 
	const std::string direct_message = "direct_message";
 
	const std::string directmessages = "direct-messages";
 
	const std::string sender_id = "sender_id";
 
	const std::string recipient_id = "recipient_id";
 
	const std::string sender_screen_name = "sender_screen_name";
 
	const std::string recipient_screen_name = "recipient_screen_name";
 
	const std::string sender = "sender";
 
   	const std::string recipient = "recipient";
 
};
 

	
 
//Class holding user data
 
@@ -96,10 +104,50 @@ class Status
 
	void setRetweeted(bool val) {retweeted = val;}
 
};
 

	
 
//Class representing a Direct Message
 
class DirectMessage
 
{
 
	std::string created_at;
 
	std::string ID;
 
	std::string text;
 
	std::string sender_id;
 
	std::string recipient_id;
 
	std::string sender_screen_name;
 
	std::string recipient_screen_name;
 
	User sender, recipient;
 

	
 
	public:
 
	DirectMessage():created_at(""),ID(""),text(""),sender_id(""),recipient_id(""),
 
			 sender_screen_name(""),recipient_screen_name(""),sender(User()),recipient(User()){}
 
	
 
	std::string getCreationTime() {return created_at;}
 
	std::string getID() {return ID;}
 
	std::string getMessage() {return text;}
 
	std::string getSenderID() {return sender_id;}
 
	std::string getRecipientID() {return recipient_id;}
 
	std::string getSenderScreenName() {return sender_screen_name;}
 
	std::string getRecipientScreenName() {return recipient_screen_name;}
 
	User getSenderData() {return sender;}
 
	User getRecipientData() {return recipient;}
 
	
 
	void setCreationTime(std::string _created) {created_at = _created;}
 
	void setID(std::string _id) {ID = _id;}
 
	void setMessage(std::string _text) {text = _text;}
 
	void setSenderID(std::string _id) {sender_id = _id;}
 
	void setRecipientID(std::string _id) {recipient_id = _id;}
 
	void setSenderScreenName(std::string _name) {sender_screen_name = _name;}
 
	void setRecipientScreenName(std::string _name) {recipient_screen_name = _name;}
 
	void setSenderData(User u) {sender = u;}
 
	void setRecipientData(User u) {recipient = u;}
 
};
 

	
 
std::vector<Status> getTimeline(std::string &xml);
 
std::vector<DirectMessage> getDirectMessages(std::string &xml);
 
std::vector<std::string> getIDs(std::string &xml);
 
std::vector<User> getUsers(std::string &xml);
 
std::string getErrorMessage(std::string &xml);
 

	
 
Status getStatus(const Swift::ParserElement::ref &element, const std::string xmlns);
 
DirectMessage getDirectMessage(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
 
@@ -711,11 +711,17 @@ bool twitCurl::followersGet( std::string userInfo, bool isUserId )
 
*          response by twitter. Use getLastWebResponse() for that.
 
*
 
*--*/
 
bool twitCurl::directMessageGet()
 
bool twitCurl::directMessageGet( std::string sinceId )
 
{
 
	std::string buildUrl = twitterDefaults::TWITCURL_DIRECTMESSAGES_URL + twitCurlDefaults::TWITCURL_EXTENSIONFORMATS[m_eApiFormatType];
 
	
 
	if(sinceId.length())
 
	{
 
      	buildUrl += twitCurlDefaults::TWITCURL_URL_SEP_QUES + twitCurlDefaults::TWITCURL_SINCEID + sinceId;
 
	}
 
 
    /* Perform GET */
 
    return performGet( twitterDefaults::TWITCURL_DIRECTMESSAGES_URL +
 
                       twitCurlDefaults::TWITCURL_EXTENSIONFORMATS[m_eApiFormatType] );
 
    return performGet( buildUrl );
 
}
 
 
/*++
backends/twitter/libtwitcurl/twitcurl.h
Show inline comments
 
@@ -163,7 +163,7 @@ public:
 
    bool followersGet( std::string userInfo = "" /* in */, bool isUserId = false /* in */ );
 
 
    /* Twitter direct message APIs */
 
    bool directMessageGet();
 
    bool directMessageGet( std::string sinceId /* in */ );
 
    bool directMessageSend( std::string& userInfo /* in */, std::string& dMsg /* in */, bool isUserId = false /* in */ );
 
    bool directMessageGetSent();
 
    bool directMessageDestroyById( std::string& dMsgId /* in */ );
0 comments (0 inline, 0 general)