Twitter backend: expand urls
This commit is contained in:
parent
9efb518107
commit
b546e57b50
2 changed files with 50 additions and 6 deletions
|
@ -20,13 +20,18 @@ template <class T> std::string stringOf(T object) {
|
||||||
return (os.str());
|
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;
|
using boost::algorithm::replace_all;
|
||||||
replace_all(data, "&", "&");
|
replace_all(data, "&", "&");
|
||||||
replace_all(data, """, "\"");
|
replace_all(data, """, "\"");
|
||||||
replace_all(data, "'", "\'");
|
replace_all(data, "'", "\'");
|
||||||
replace_all(data, "<", "<");
|
replace_all(data, "<", "<");
|
||||||
replace_all(data, ">", ">");
|
replace_all(data, ">", ">");
|
||||||
|
|
||||||
|
for (std::vector<UrlEntity>::size_type i = 0; i < urls.size(); i++) {
|
||||||
|
replace_all(data, urls[i].getUrl(), urls[i].getExpandedUrl());
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +58,7 @@ EmbeddedStatus getEmbeddedStatus(const rapidjson::Value &element)
|
||||||
}
|
}
|
||||||
status.setCreationTime( toIsoTime ( std::string( element[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
|
status.setCreationTime( toIsoTime ( std::string( element[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
|
||||||
status.setID( stringOf( 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.setTweet( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ), getUrlEntities(element) ) );
|
||||||
status.setTruncated( element[TwitterReponseTypes::truncated.c_str()].GetBool());
|
status.setTruncated( element[TwitterReponseTypes::truncated.c_str()].GetBool());
|
||||||
status.setReplyToStatusID( element[TwitterReponseTypes::in_reply_to_status_id.c_str()].IsNull() ?
|
status.setReplyToStatusID( element[TwitterReponseTypes::in_reply_to_status_id.c_str()].IsNull() ?
|
||||||
"" : stringOf(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
|
"" : stringOf(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
|
||||||
|
@ -91,7 +96,7 @@ Status getStatus(const rapidjson::Value &element)
|
||||||
|
|
||||||
status.setCreationTime( toIsoTime ( std::string( element[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
|
status.setCreationTime( toIsoTime ( std::string( element[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
|
||||||
status.setID( stringOf( 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.setTweet( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ), getUrlEntities(element) ) );
|
||||||
status.setTruncated( element[TwitterReponseTypes::truncated.c_str()].GetBool());
|
status.setTruncated( element[TwitterReponseTypes::truncated.c_str()].GetBool());
|
||||||
status.setReplyToStatusID( element[TwitterReponseTypes::in_reply_to_status_id.c_str()].IsNull() ?
|
status.setReplyToStatusID( element[TwitterReponseTypes::in_reply_to_status_id.c_str()].IsNull() ?
|
||||||
"" : stringOf(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
|
"" : stringOf(element[TwitterReponseTypes::in_reply_to_status_id.c_str()].GetInt64()) );
|
||||||
|
@ -105,11 +110,13 @@ Status getStatus(const rapidjson::Value &element)
|
||||||
status.setRetweeted( element[TwitterReponseTypes::retweeted.c_str()].GetBool());
|
status.setRetweeted( element[TwitterReponseTypes::retweeted.c_str()].GetBool());
|
||||||
const rapidjson::Value &rt = element[TwitterReponseTypes::retweeted_status.c_str()];
|
const rapidjson::Value &rt = element[TwitterReponseTypes::retweeted_status.c_str()];
|
||||||
if (rt.IsObject()) {
|
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.setRetweetID( stringOf(rt[TwitterReponseTypes::id.c_str()].GetInt64()) );
|
||||||
status.setCreationTime( toIsoTime ( std::string (rt[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
|
status.setCreationTime( toIsoTime ( std::string (rt[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
|
||||||
status.setUserData( getUser ( rt[TwitterReponseTypes::user.c_str()]) );
|
status.setUserData( getUser ( rt[TwitterReponseTypes::user.c_str()]) );
|
||||||
}
|
}
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +126,7 @@ DirectMessage getDirectMessage(const rapidjson::Value &element)
|
||||||
|
|
||||||
DM.setCreationTime( toIsoTime ( std::string( element[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
|
DM.setCreationTime( toIsoTime ( std::string( element[TwitterReponseTypes::created_at.c_str()].GetString() ) ) );
|
||||||
DM.setID( stringOf( 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.setMessage( unescape ( std::string( element[TwitterReponseTypes::text.c_str()].GetString() ), getUrlEntities(element) ) );
|
||||||
DM.setSenderID( stringOf( element[TwitterReponseTypes::sender_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.setRecipientID( stringOf( element[TwitterReponseTypes::recipient_id.c_str()].GetInt64() ) );
|
||||||
DM.setSenderScreenName( std::string( element[TwitterReponseTypes::sender_screen_name.c_str()].GetString() ) );
|
DM.setSenderScreenName( std::string( element[TwitterReponseTypes::sender_screen_name.c_str()].GetString() ) );
|
||||||
|
@ -269,3 +276,23 @@ Error getErrorMessage(std::string &json)
|
||||||
|
|
||||||
return resp;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace TwitterReponseTypes
|
namespace TwitterReponseTypes
|
||||||
{
|
{
|
||||||
|
@ -211,6 +212,21 @@ class Error
|
||||||
void setMessage(std::string &_message) {message = _message;}
|
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<Status> getTimeline(std::string &xml);
|
||||||
std::vector<DirectMessage> getDirectMessages(std::string &xml);
|
std::vector<DirectMessage> getDirectMessages(std::string &xml);
|
||||||
std::vector<std::string> getIDs(std::string &xml);
|
std::vector<std::string> getIDs(std::string &xml);
|
||||||
|
@ -218,6 +234,7 @@ std::vector<User> getUsers(std::string &xml);
|
||||||
User getUser(std::string &xml);
|
User getUser(std::string &xml);
|
||||||
Error getErrorMessage(std::string &xml);
|
Error getErrorMessage(std::string &xml);
|
||||||
|
|
||||||
|
std::vector<UrlEntity> getUrlEntities(const rapidjson::Value &element);
|
||||||
Status getStatus(const rapidjson::Value &element);
|
Status getStatus(const rapidjson::Value &element);
|
||||||
DirectMessage getDirectMessage(const rapidjson::Value &element);
|
DirectMessage getDirectMessage(const rapidjson::Value &element);
|
||||||
User getUser(const rapidjson::Value &element);
|
User getUser(const rapidjson::Value &element);
|
||||||
|
|
Loading…
Add table
Reference in a new issue