Changeset - b546e57b5033
[Not reviewed]
0 2 0
Vitaly Takmazov - 12 years ago 2013-08-07 18:45:39
vitalyster@gmail.com
Twitter backend: expand urls
2 files changed with 49 insertions and 5 deletions:
0 comments (0 inline, 0 general)
backends/twitter/TwitterResponseParser.cpp
Show inline comments
 
@@ -17,19 +17,24 @@ static std::string tolowercase(std::string inp)
 
template <class T> std::string stringOf(T object) {
 
	std::ostringstream os;
 
	os << object;
 
	return (os.str());
 
}
 

	
 
static std::string unescape(std::string data) {
 
static std::string unescape(std::string data, std::vector<UrlEntity> urls) {
 
	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;",   ">");
 

	
 
	for (std::vector<UrlEntity>::size_type i = 0; i < urls.size(); i++) {
 
		replace_all(data, urls[i].getUrl(), urls[i].getExpandedUrl());
 
	}
 

	
 
	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();
 
@@ -50,13 +55,13 @@ EmbeddedStatus getEmbeddedStatus(const rapidjson::Value &element)
 
	if(!element.IsObject()) {
 
		LOG4CXX_ERROR(logger, "Not a status element!")
 
		return status;
 
	}
 
	status.setCreationTime( toIsoTime ( std::string( element[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
 
	status.setID( stringOf( element[TwitterReponseTypes::id.c_str()].GetInt64() ) );
 
	status.setTweet( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ) ) );
 
	status.setTweet( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ), getUrlEntities(element) ) );
 
	status.setTruncated( element[TwitterReponseTypes::truncated.c_str()].GetBool());
 
	status.setReplyToStatusID( element[TwitterReponseTypes::in_reply_to_status_id.c_str()].IsNull() ?
 
"" : stringOf(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
 
	status.setReplyToUserID( element[TwitterReponseTypes::in_reply_to_user_id.c_str()].IsNull() ?
 
"" : stringOf(element[TwitterReponseTypes::in_reply_to_user_id.c_str()].GetInt64())  );
 
	status.setReplyToScreenName( element[TwitterReponseTypes::in_reply_to_screen_name.c_str()].IsNull() ?
 
@@ -88,13 +93,13 @@ User getUser(const rapidjson::Value &element)
 
Status getStatus(const rapidjson::Value &element) 
 
{
 
	Status status;
 

	
 
	status.setCreationTime( toIsoTime ( std::string( element[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
 
	status.setID( stringOf( element[TwitterReponseTypes::id.c_str()].GetInt64()  ));
 
	status.setTweet( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ) ) );
 
	status.setTweet( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ), getUrlEntities(element) ) );
 
	status.setTruncated( element[TwitterReponseTypes::truncated.c_str()].GetBool());
 
	status.setReplyToStatusID( element[TwitterReponseTypes::in_reply_to_status_id.c_str()].IsNull() ?
 
"" : stringOf(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
 
	status.setReplyToUserID( element[TwitterReponseTypes::in_reply_to_user_id.c_str()].IsNull() ?
 
"" : stringOf(element[TwitterReponseTypes::in_reply_to_user_id.c_str()].GetInt64())  );
 
	status.setReplyToScreenName( element[TwitterReponseTypes::in_reply_to_screen_name.c_str()].IsNull() ?
 
@@ -102,27 +107,29 @@ Status getStatus(const rapidjson::Value &element)
 
	status.setUserData( getUser(element[TwitterReponseTypes::user.c_str()]) );
 
	status.setRetweetCount( element[TwitterReponseTypes::retweet_count.c_str()].GetInt64() );
 
	status.setFavorited( element[TwitterReponseTypes::favorited.c_str()].GetBool() );
 
	status.setRetweeted( element[TwitterReponseTypes::retweeted.c_str()].GetBool());
 
	const rapidjson::Value &rt = element[TwitterReponseTypes::retweeted_status.c_str()];
 
	if (rt.IsObject()) {
 
		status.setTweet(unescape( std::string( rt[TwitterReponseTypes::text.c_str()].GetString()) + " (RT by @" + status.getUserData().getScreenName() + ")") );
 
		status.setTweet(unescape( std::string( rt[TwitterReponseTypes::text.c_str()].GetString()) + " (RT by @" + status.getUserData().getScreenName() + ")"
 
			, getUrlEntities(element)) );
 
		status.setRetweetID( stringOf(rt[TwitterReponseTypes::id.c_str()].GetInt64()) );
 
		status.setCreationTime( toIsoTime ( std::string (rt[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
 
		status.setUserData( getUser ( rt[TwitterReponseTypes::user.c_str()]) );
 
	}
 
	
 
	return status;
 
}
 

	
 
DirectMessage getDirectMessage(const rapidjson::Value &element) 
 
{
 
	DirectMessage DM;
 
	
 
	DM.setCreationTime( toIsoTime ( std::string( element[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
 
	DM.setID( stringOf( element[TwitterReponseTypes::id.c_str()].GetInt64() ) );
 
	DM.setMessage( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ) ) );
 
	DM.setMessage( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ), getUrlEntities(element) ) );
 
	DM.setSenderID( stringOf( element[TwitterReponseTypes::sender_id.c_str()].GetInt64())  );
 
	DM.setRecipientID( stringOf( element[TwitterReponseTypes::recipient_id.c_str()].GetInt64() ) );
 
	DM.setSenderScreenName( std::string( element[TwitterReponseTypes::sender_screen_name.c_str()].GetString() ) );
 
	DM.setRecipientScreenName( std::string( element[TwitterReponseTypes::recipient_screen_name.c_str()].GetString() ) );
 
	DM.setSenderData( getUser(element[TwitterReponseTypes::sender.c_str()] ));
 
	DM.setRecipientData( getUser(element[TwitterReponseTypes::recipient.c_str()]) );
 
@@ -266,6 +273,26 @@ Error getErrorMessage(std::string &json)
 
			resp.setMessage(error);
 
		}
 
	}
 

	
 
	return resp;
 
}
 

	
 
std::vector<UrlEntity> getUrlEntities(const rapidjson::Value &element) 
 
{
 

	
 
	std::vector<UrlEntity> url_entities;
 

	
 
	const rapidjson::Value &entities = element["entities"];
 
	
 
	if (entities.IsObject()) {
 
		const rapidjson::Value &urls = entities["urls"];
 
		if (urls.IsArray()) {		
 
			for (rapidjson::SizeType i = 0; i < urls.Size(); i++) {
 
				UrlEntity entity(urls[i]["url"].GetString(), urls[i]["expanded_url"].GetString());	
 
				url_entities.push_back(entity);
 
			}
 
		}
 
	}
 
	
 
	return url_entities;
 
}
backends/twitter/TwitterResponseParser.h
Show inline comments
 
@@ -2,12 +2,13 @@
 
#define TWITTERRESPOSNSEPARSER_H
 

	
 
#include "rapidjson/document.h"
 
#include <iostream>
 
#include <vector>
 
#include <string>
 
#include <utility>
 

	
 
namespace TwitterReponseTypes
 
{
 
	const std::string id = "id";
 
	const std::string id_list = "id_list";
 
	const std::string ids = "ids";
 
@@ -208,17 +209,33 @@ class Error
 
	bool isCurlError() { return code.empty(); }
 

	
 
	void setCode(std::string &_code) {code = _code;}
 
	void setMessage(std::string &_message) {message = _message;}
 
};
 

	
 
class UrlEntity 
 
{
 
	std::string url;
 
	std::string expanded_url;	
 
public:
 
	UrlEntity(std::string _url, std::string _expanded) 
 
	{ 
 
		url = _url;
 
		expanded_url = _expanded;
 
	}
 
	std::string getUrl() {return url;}
 
	std::string getExpandedUrl() {return expanded_url;}
 
	
 
};
 

	
 
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);
 
User getUser(std::string &xml);
 
Error getErrorMessage(std::string &xml);
 

	
 
std::vector<UrlEntity> getUrlEntities(const rapidjson::Value &element);
 
Status getStatus(const rapidjson::Value &element);
 
DirectMessage getDirectMessage(const rapidjson::Value &element);
 
User getUser(const rapidjson::Value &element);
 
#endif
0 comments (0 inline, 0 general)