Files
@ 737dd6a0625e
Branch filter:
Location: libtransport.git/backends/twitter/TwitterResponseParser.cpp - annotation
737dd6a0625e
10.4 KiB
text/x-c++hdr
Fixed merge conflict
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 929b49d6412d ae2c0acb5058 9469109d37e4 bdb8aed78f63 50119c8e4ef2 50119c8e4ef2 ae2c0acb5058 ae2c0acb5058 929b49d6412d bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 9469109d37e4 9469109d37e4 9469109d37e4 9469109d37e4 9469109d37e4 9469109d37e4 9469109d37e4 9469109d37e4 9469109d37e4 9469109d37e4 9388d599156c 50119c8e4ef2 50119c8e4ef2 50119c8e4ef2 50119c8e4ef2 50119c8e4ef2 50119c8e4ef2 50119c8e4ef2 50119c8e4ef2 50119c8e4ef2 50119c8e4ef2 50119c8e4ef2 9388d599156c 9388d599156c ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 9388d599156c 9388d599156c ffd0baea6fd9 9469109d37e4 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 ffd0baea6fd9 929b49d6412d 929b49d6412d 929b49d6412d 11937227ff60 11937227ff60 11937227ff60 ae2c0acb5058 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d bdb8aed78f63 929b49d6412d bdb8aed78f63 929b49d6412d ffd0baea6fd9 ffd0baea6fd9 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d ae2c0acb5058 929b49d6412d 929b49d6412d 929b49d6412d 9388d599156c 929b49d6412d 9469109d37e4 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 9388d599156c 11937227ff60 9469109d37e4 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d b297329ba32f b297329ba32f b297329ba32f b297329ba32f b297329ba32f 929b49d6412d 26e5289cefeb 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 929b49d6412d 26e5289cefeb 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 b297329ba32f b297329ba32f b297329ba32f b297329ba32f b297329ba32f 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 11937227ff60 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 b297329ba32f b297329ba32f b297329ba32f b297329ba32f b297329ba32f 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 9111a3993741 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 bdb8aed78f63 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb b297329ba32f b297329ba32f b297329ba32f b297329ba32f b297329ba32f 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb 26e5289cefeb e9891aa2000a c236c464ffac e9891aa2000a c236c464ffac c236c464ffac c236c464ffac c236c464ffac e9891aa2000a e9891aa2000a b297329ba32f b297329ba32f c236c464ffac b297329ba32f b297329ba32f e9891aa2000a e9891aa2000a c236c464ffac e9891aa2000a c236c464ffac c236c464ffac c236c464ffac c236c464ffac c236c464ffac c236c464ffac c236c464ffac c236c464ffac c236c464ffac e9891aa2000a | #include "TwitterResponseParser.h"
#include "transport/logging.h"
#include "boost/algorithm/string.hpp"
#include <cctype>
#include "boost/date_time/local_time/local_time.hpp"
#include "boost/date_time/time_facet.hpp"
DEFINE_LOGGER(logger, "TwitterResponseParser")
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, "&", "&");
replace_all(data, """, "\"");
replace_all(data, "'", "\'");
replace_all(data, "<", "<");
replace_all(data, ">", ">");
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" );
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" );
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;
}
std::vector<DirectMessage> getDirectMessages(std::string &xml)
{
std::vector<DirectMessage> DMs;
Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
if(rootElement == NULL) {
LOG4CXX_ERROR(logger, "Error while parsing XML")
return DMs;
}
if(rootElement->getName() != TwitterReponseTypes::directmessages) {
LOG4CXX_ERROR(logger, "XML doesn't correspond to direct-messages")
return DMs;
}
const std::string xmlns = rootElement->getNamespace();
const std::vector<Swift::ParserElement::ref> children = rootElement->getChildren(TwitterReponseTypes::direct_message, xmlns);
for(int i = 0; i < children.size() ; i++) {
const Swift::ParserElement::ref dm = children[i];
DMs.push_back(getDirectMessage(dm, xmlns));
}
return DMs;
}
std::vector<User> getUsers(std::string &xml)
{
std::vector<User> users;
Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
if(rootElement == NULL) {
LOG4CXX_ERROR(logger, "Error while parsing XML")
return users;
}
if(rootElement->getName() != TwitterReponseTypes::users) {
LOG4CXX_ERROR(logger, "XML doesn't correspond to user list")
return users;
}
const std::string xmlns = rootElement->getNamespace();
const std::vector<Swift::ParserElement::ref> children = rootElement->getChildren(TwitterReponseTypes::user, xmlns);
for(int i = 0 ; i < children.size() ; i++) {
const Swift::ParserElement::ref user = children[i];
users.push_back(getUser(user, xmlns));
}
return users;
}
User getUser(std::string &xml)
{
User user;
Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
if(rootElement == NULL) {
LOG4CXX_ERROR(logger, "Error while parsing XML")
return user;
}
if(rootElement->getName() != TwitterReponseTypes::user) {
LOG4CXX_ERROR(logger, "XML doesn't correspond to user object")
return user;
}
const std::string xmlns = rootElement->getNamespace();
return user = getUser(rootElement, xmlns);
}
std::vector<std::string> getIDs(std::string &xml)
{
std::vector<std::string> IDs;
Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
if(rootElement == NULL) {
LOG4CXX_ERROR(logger, "Error while parsing XML")
return IDs;
}
if(rootElement->getName() != TwitterReponseTypes::id_list) {
LOG4CXX_ERROR(logger, "XML doesn't correspond to id_list");
return IDs;
}
const std::string xmlns = rootElement->getNamespace();
const std::vector<Swift::ParserElement::ref> ids = rootElement->getChild(TwitterReponseTypes::ids, xmlns)->getChildren(TwitterReponseTypes::id, xmlns);
for(int i=0 ; i<ids.size() ; i++) {
IDs.push_back(std::string( ids[i]->getText() ));
}
return IDs;
}
Error getErrorMessage(std::string &xml)
{
std::string error = "";
std::string code = "";
Error resp;
Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
if(rootElement == NULL) {
LOG4CXX_ERROR(logger, "Error while parsing XML");
return resp;
}
const std::string xmlns = rootElement->getNamespace();
const Swift::ParserElement::ref errorElement = rootElement->getChild(TwitterReponseTypes::error, xmlns);
Swift::AttributeMap attributes = errorElement->getAttributes();
if(errorElement != NULL) {
error = errorElement->getText();
code = (errorElement->getAttributes()).getAttribute("code");
}
resp.setCode(code);
resp.setMessage(error);
return resp;
}
|