Changeset - 9fbd37a5f3b7
[Not reviewed]
0 1 0
Vitaly Takmazov - 12 years ago 2013-06-14 15:52:11
vitalyster@gmail.com
Twitter backend: fixed json parsing
1 file changed with 4 insertions and 4 deletions:
0 comments (0 inline, 0 general)
backends/twitter/TwitterResponseParser.cpp
Show inline comments
 
@@ -29,89 +29,89 @@ static std::string toIsoTime(std::string in) {
 
	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 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.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::string(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetString()) );
 
"" : std::to_string(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
 
	status.setReplyToUserID( element[TwitterReponseTypes::in_reply_to_user_id.c_str()].IsNull() ?
 
"" : std::string(element[TwitterReponseTypes::in_reply_to_user_id.c_str()].GetString())  );
 
"" : std::to_string(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;
 
}
 

	
 
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.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()]));
 
	return user;
 
}
 

	
 
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.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::string(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetString()) );
 
"" : std::to_string(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
 
	status.setReplyToUserID( element[TwitterReponseTypes::in_reply_to_user_id.c_str()].IsNull() ?
 
"" : std::string(element[TwitterReponseTypes::in_reply_to_user_id.c_str()].GetString())  );
 
"" : std::to_string(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.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.setMessage( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ) ) );
 
	DM.setSenderID( std::to_string( element[TwitterReponseTypes::sender_id.c_str()].GetInt64())  );
0 comments (0 inline, 0 general)