Changeset - 86626d92ff59
[Not reviewed]
0 2 0
Vitaly Takmazov - 12 years ago 2013-07-29 16:17:12
vitalyster@gmail.com
Twitter backend: fix OSX/CLang compilation
2 files changed with 19 insertions and 16 deletions:
0 comments (0 inline, 0 general)
backends/twitter/CMakeLists.txt
Show inline comments
 
include_directories (${libtransport_SOURCE_DIR}/backends/twitter/libtwitcurl) 
 
FILE(GLOB SRC *.cpp libtwitcurl/*.cpp Requests/*.cpp)
 
add_executable(spectrum2_twitter_backend ${SRC})
 

	
 
if (NOT WIN32)
 
if (CMAKE_COMPILER_IS_GNUCXX)
 
	add_definitions("-std=c++0x")
 
endif()
 
target_link_libraries(spectrum2_twitter_backend curl transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
else ()
 
include_directories("${CMAKE_SOURCE_DIR}/msvc-deps/curl/include")
 
target_link_libraries(spectrum2_twitter_backend libcurl transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
 
endif()
 

	
backends/twitter/TwitterResponseParser.cpp
Show inline comments
 
@@ -11,12 +11,18 @@ 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;
 
}
 

	
 
template <class T> std::string stringOf(T object) {
 
	std::ostringstream os;
 
	os << object;
 
	return (os.str());
 
}
 

	
 
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;",   "<");
 
@@ -43,19 +49,19 @@ EmbeddedStatus getEmbeddedStatus(const rapidjson::Value &element)
 
	EmbeddedStatus status;
 
	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( std::to_string( element[TwitterReponseTypes::id.c_str()].GetInt64() ) ); 
 
	status.setID( stringOf( element[TwitterReponseTypes::id.c_str()].GetInt64() ) );
 
	status.setTweet( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ) ) );
 
	status.setTruncated( element[TwitterReponseTypes::truncated.c_str()].GetBool());
 
	status.setReplyToStatusID( element[TwitterReponseTypes::in_reply_to_status_id.c_str()].IsNull() ?
 
"" : std::to_string(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
 
"" : stringOf(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
 
	status.setReplyToUserID( element[TwitterReponseTypes::in_reply_to_user_id.c_str()].IsNull() ?
 
"" : std::to_string(element[TwitterReponseTypes::in_reply_to_user_id.c_str()].GetInt64())  );
 
"" : stringOf(element[TwitterReponseTypes::in_reply_to_user_id.c_str()].GetInt64())  );
 
	status.setReplyToScreenName( element[TwitterReponseTypes::in_reply_to_screen_name.c_str()].IsNull() ?
 
"" : std::string(element[TwitterReponseTypes::in_reply_to_screen_name.c_str()].GetString()) );
 
	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());	
 
	return status;
 
@@ -66,13 +72,13 @@ User getUser(const rapidjson::Value &element)
 
	User user;
 
	if(!element.IsObject()) {
 
		LOG4CXX_ERROR(logger, "Not a user element!")
 
		return user;
 
	}
 

	
 
	user.setUserID( std::to_string( element[TwitterReponseTypes::id.c_str()].GetInt64() ) );
 
	user.setUserID( stringOf( element[TwitterReponseTypes::id.c_str()].GetInt64() ) );
 
	user.setScreenName( tolowercase( std::string( element[TwitterReponseTypes::screen_name.c_str()].GetString() ) ) );
 
	user.setUserName( std::string( element[TwitterReponseTypes::name.c_str()].GetString() ) );
 
	user.setProfileImgURL( std::string( element[TwitterReponseTypes::profile_image_url.c_str()].GetString() ) );
 
	user.setNumberOfTweets( element[TwitterReponseTypes::statuses_count.c_str()].GetInt64() );
 
	if(element[TwitterReponseTypes::status.c_str()].IsObject()) 
 
		user.setLastStatus(getEmbeddedStatus(element[TwitterReponseTypes::status.c_str()]));
 
@@ -81,44 +87,44 @@ 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( std::to_string( element[TwitterReponseTypes::id.c_str()].GetInt64()  )); 
 
	status.setID( stringOf( element[TwitterReponseTypes::id.c_str()].GetInt64()  ));
 
	status.setTweet( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ) ) );
 
	status.setTruncated( element[TwitterReponseTypes::truncated.c_str()].GetBool());
 
	status.setReplyToStatusID( element[TwitterReponseTypes::in_reply_to_status_id.c_str()].IsNull() ?
 
"" : std::to_string(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
 
"" : stringOf(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
 
	status.setReplyToUserID( element[TwitterReponseTypes::in_reply_to_user_id.c_str()].IsNull() ?
 
"" : std::to_string(element[TwitterReponseTypes::in_reply_to_user_id.c_str()].GetInt64())  );
 
"" : stringOf(element[TwitterReponseTypes::in_reply_to_user_id.c_str()].GetInt64())  );
 
	status.setReplyToScreenName( element[TwitterReponseTypes::in_reply_to_screen_name.c_str()].IsNull() ?
 
"" : std::string(element[TwitterReponseTypes::in_reply_to_screen_name.c_str()].GetString()) );
 
	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.setRetweetID( std::to_string(rt[TwitterReponseTypes::id.c_str()].GetInt64()) );
 
		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( std::to_string( element[TwitterReponseTypes::id.c_str()].GetInt64() ) );
 
	DM.setID( stringOf( element[TwitterReponseTypes::id.c_str()].GetInt64() ) );
 
	DM.setMessage( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ) ) );
 
	DM.setSenderID( std::to_string( element[TwitterReponseTypes::sender_id.c_str()].GetInt64())  );
 
	DM.setRecipientID( std::to_string( element[TwitterReponseTypes::recipient_id.c_str()].GetInt64() ) );
 
	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()]) );
 
	return DM;
 
}
 
@@ -231,13 +237,13 @@ std::vector<std::string> getIDs(std::string &json)
 
		return IDs;
 
	}
 

	
 
	const rapidjson::Value & ids = rootElement[TwitterReponseTypes::ids.c_str()];
 
	
 
	for(int i=0 ; i<ids.Size() ; i++) {
 
		IDs.push_back(std::to_string( ids[i].GetInt64()) );
 
		IDs.push_back(stringOf( ids[i].GetInt64()) );
 
	}
 
	return IDs;
 
}
 

	
 
Error getErrorMessage(std::string &json)
 
{
 
@@ -252,13 +258,13 @@ Error getErrorMessage(std::string &json)
 
		return resp;
 
	}
 
	if (rootElement.IsObject()) {
 
		if (!rootElement["errors"].IsNull()) {			
 
			const rapidjson::Value &errorElement = rootElement["errors"][0u]; // first error
 
			error = std::string(errorElement["message"].GetString());
 
			code = std::to_string(errorElement["code"].GetInt64());
 
			code = stringOf(errorElement["code"].GetInt64());
 
			resp.setCode(code);
 
			resp.setMessage(error);
 
		}
 
	}
 

	
 
	return resp;
0 comments (0 inline, 0 general)