Changeset - f0b99d6b02b8
[Not reviewed]
Merge
0 37 5
Vitaly Takmazov - 13 years ago 2012-12-10 14:26:15
vitalyster@gmail.com
Merge branch 'retweeted_status'
2 files changed with 7 insertions and 0 deletions:
0 comments (0 inline, 0 general)
backends/twitter/TwitterResponseParser.cpp
Show inline comments
 
@@ -12,138 +12,144 @@ static std::string tolowercase(std::string inp)
 
	std::string out = inp;
 
	for(int i=0 ; i<out.size() ; i++) out[i] = tolower(out[i]);
 
	return out;
 
}
 

	
 
static std::string unescape(std::string data) {
 
	using boost::algorithm::replace_all;
 
	replace_all(data, "&amp;",  "&");
 
	replace_all(data, "&quot;", "\"");
 
	replace_all(data, "&apos;", "\'");
 
	replace_all(data, "&lt;",   "<");
 
	replace_all(data, "&gt;",   ">");
 
	return data;
 
}
 

	
 
static std::string toIsoTime(std::string in) {
 
	std::locale locale(std::locale::classic(), new boost::local_time::local_time_input_facet("%a %b %d %H:%M:%S +0000 %Y"));
 
	boost::local_time::local_time_facet* output_facet = new boost::local_time::local_time_facet();
 
	boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
 
	std::stringstream ss(in);
 
	ss.imbue(locale);
 
	ss.imbue(std::locale(ss.getloc(), output_facet));
 
	output_facet->format("%Y%m%dT%H%M%S"); // boost::local_time::local_time_facet::iso_time_format_specifier ?
 
	ss >> ldt;
 
	ss.str("");
 
	ss << ldt;	
 
	return ss.str();
 
}
 

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

	
 
	status.setCreationTime( toIsoTime(std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) ) );
 

	
 
	status.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
 
	status.setTweet( unescape (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.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" );
 
	if (status.isRetweeted()) {
 
		status.setTweet( std::string( element->getChild(TwitterReponseTypes::retweeted_status, xmlns)->getText() ) );
 
	}
 
	return status;
 
}
 

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

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

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

	
 
	status.setCreationTime( toIsoTime ( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) ) );
 
	status.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
 
	status.setTweet( unescape ( 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" );
 
	if (status.isRetweeted()) {
 
		status.setTweet( std::string( element->getChild(TwitterReponseTypes::retweeted_status, xmlns)->getText() ) );
 
	}
 
	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( toIsoTime ( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) ) );
 
	DM.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
 
	DM.setMessage( unescape ( 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;
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 
	
 
	if(rootElement == NULL) {
 
		LOG4CXX_ERROR(logger, "Error while parsing XML")
 
		return statuses;
 
	}
 

	
 
	if(rootElement->getName() != "statuses") {
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to timeline")
 
		return statuses;
 
	}
 

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

	
 
	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/TwitterResponseParser.h
Show inline comments
 
#ifndef TWITTERRESPOSNSEPARSER_H
 
#define TWITTERRESPOSNSEPARSER_H
 

	
 
#include "Swiften/Parser/StringTreeParser.h"
 
#include <iostream>
 
#include <vector>
 

	
 
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";
 
	const std::string created_at = "created_at";
 
	const std::string text = "text";
 
	const std::string truncated = "truncated";
 
	const std::string in_reply_to_status_id = "in_reply_to_user_id";
 
	const std::string in_reply_to_user_id = "in_reply_to_user_id";
 
	const std::string in_reply_to_screen_name = "in_reply_to_screen_name";
 
	const std::string retweet_count = "retweet_count";
 
	const std::string retweeted_status = "retweeted_status";
 
	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";
 
	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";
 
	const std::string profile_image_url = "profile_image_url";
 
};
 

	
 
//Class representing an embedded status object within other objects such as the User object.
 
//Note: Not possible to user Status due to circular dependency
 
class EmbeddedStatus
 
{
 
	std::string created_at;
 
	std::string ID;
 
	std::string text;
 
	bool truncated;
 
	std::string in_reply_to_status_id;
 
	std::string in_reply_to_user_id;
 
	std::string in_reply_to_screen_name;
 
	unsigned int retweet_count;
 
	bool favorited;
 
	bool retweeted;
 
	
 
	public:
 
	EmbeddedStatus():created_at(""),ID(""),text(""),truncated(false),in_reply_to_status_id(""),
 
	         in_reply_to_user_id(""),in_reply_to_screen_name(""),retweet_count(0),
 
	         favorited(false),retweeted(0){}
 
	
 
	std::string getCreationTime() {return created_at;}
 
	std::string getID() {return ID;}
 
	std::string getTweet() {return text;}
 
	bool isTruncated() {return truncated;}
 
	std::string getReplyToStatusID() {return in_reply_to_status_id;}
 
	std::string getReplyToUserID() {return in_reply_to_user_id;}
 
	std::string getReplyToScreenName() {return in_reply_to_screen_name;}
 
	unsigned int getRetweetCount() {return retweet_count;}
 
	bool isFavorited() {return favorited;}
 
	bool isRetweeted() {return retweeted;}
 
	
0 comments (0 inline, 0 general)