Added parser to parse error messages from twitter; Added curl option to handle gzip encoded responses

This commit is contained in:
Sarang Bharadwaj 2012-06-06 13:42:06 +05:30
parent 81a71207d5
commit e9891aa200
11 changed files with 77 additions and 12 deletions

View file

@ -1,7 +1,11 @@
#include "DirectMessageRequest.h"
#include "../TwitterResponseParser.h"
DEFINE_LOGGER(logger, "DirectMessageRequest")
void DirectMessageRequest::run()
{
replyMsg = "";
if(twitObj->directMessageSend(username, data, false) == false) {
LOG4CXX_ERROR(logger, user << ": Error while sending directed message to " << username );
return;
@ -11,6 +15,14 @@ void DirectMessageRequest::run()
void DirectMessageRequest::finalize()
{
LOG4CXX_INFO(logger, user << ": Sent " << data << " to " << username)
LOG4CXX_INFO(logger, user << ": Twitter reponse - " << replyMsg)
if(replyMsg.length()) {
std::string error = getErrorMessage(replyMsg);
if(error.length()) {
np->handleMessage(user, "twitter-account", error);
LOG4CXX_INFO(logger, user << ": " << error);
} else {
LOG4CXX_INFO(logger, user << ": Sent " << data << " to " << username)
LOG4CXX_INFO(logger, user << ": Twitter reponse - " << replyMsg)
}
}
}

View file

@ -33,7 +33,14 @@ void FetchFriends::run()
void FetchFriends::finalize()
{
if(replyMsg != "" ) {
np->handleMessage(user, "twitter-account", userlist);
std::string error = getErrorMessage(replyMsg);
if(error.length()) {
np->handleMessage(user, "twitter-account", error);
LOG4CXX_INFO(logger, user << ": " << error);
} else {
LOG4CXX_INFO(logger, user << ": " << userlist);
np->handleMessage(user, "twitter-account", userlist);
}
} else {
twitObj->getLastCurlError( replyMsg );
LOG4CXX_INFO(logger, user << " - friendsIdsGet error - " << replyMsg );

View file

@ -6,7 +6,7 @@ void HelpMessageRequest::run()
helpMsg = helpMsg
+ "\n******************************HELP************************************\n"
+ "#status <your status> ==> Update your status\n"
+ "#timeline ==> Retrieve your timeline\n"
+ "#timeline [username] ==> Retrieve <username>'s timeline; Default - own timeline\n"
+ "@<username> <message> ==> Send a directed message to the user <username>\n"
+ "#help ==> Print this help message\n"
+ "************************************************************************\n";

View file

@ -14,6 +14,20 @@ void PINExchangeProcess::finalize()
LOG4CXX_ERROR(logger, user << ": Error while exchanging PIN for Access Token!")
np->handleLogoutRequest(user, "");
} else {
std::string replyMsg;
while(replyMsg.length() == 0) {
twitObj->getLastWebResponse(replyMsg);
}
std::string error = getErrorMessage(replyMsg);
if(error.length()) {
LOG4CXX_ERROR(logger, user << ": Error while exchanging PIN for Access Token! " << error)
np->handleMessage(user, "twitter-account", error);
np->handleLogoutRequest(user, "");
return;
}
std::string OAuthAccessTokenKey, OAuthAccessTokenSecret;
twitObj->getOAuth().getOAuthTokenKey( OAuthAccessTokenKey );
twitObj->getOAuth().getOAuthTokenSecret( OAuthAccessTokenSecret );

View file

@ -1,4 +1,6 @@
#include "StatusUpdateRequest.h"
#include "../TwitterResponseParser.h"
DEFINE_LOGGER(logger, "StatusUpdateRequest")
void StatusUpdateRequest::run()
{
@ -14,10 +16,14 @@ void StatusUpdateRequest::run()
void StatusUpdateRequest::finalize()
{
if(replyMsg != "" ) {
LOG4CXX_INFO(logger, "Updated status for " << user << ": " << data);
std::string error = getErrorMessage(replyMsg);
if(error.length()) {
np->handleMessage(user, "twitter-account", error);
LOG4CXX_INFO(logger, user << ": " << error);
} else LOG4CXX_INFO(logger, "Updated status for " << user << ": " << data);
} else {
twitObj->getLastCurlError( replyMsg );
LOG4CXX_ERROR(logger, user << "Error - " << replyMsg );
LOG4CXX_ERROR(logger, user << ": CurlError - " << replyMsg );
}
return;
}

View file

@ -3,7 +3,7 @@ DEFINE_LOGGER(logger, "TimelineRequest")
void TimelineRequest::run()
{
replyMsg = "";
if( twitObj->timelineHomeGet() ) {
if( twitObj->timelineUserGet(false, false, 20, userRequested, false) ) {
LOG4CXX_INFO(logger, "Sending timeline request for user " << user)
while(replyMsg.length() == 0) {
@ -23,8 +23,14 @@ void TimelineRequest::run()
void TimelineRequest::finalize()
{
if(replyMsg.length()) {
LOG4CXX_INFO(logger, user << "'s timeline\n" << replyMsg);
np->handleMessage(user, "twitter-account", timeline); //send timeline
std::string error = getErrorMessage(replyMsg);
if(error.length()) {
np->handleMessage(user, "twitter-account", error);
LOG4CXX_INFO(logger, user << ": " << error);
} else {
LOG4CXX_INFO(logger, user << "'s timeline\n" << replyMsg);
np->handleMessage(user, "twitter-account", timeline); //send timeline
}
}
else {
twitObj->getLastCurlError( replyMsg );

View file

@ -15,15 +15,17 @@ class TimelineRequest : public Thread
{
twitCurl *twitObj;
std::string user;
std::string userRequested;
std::string replyMsg;
std::string timeline;
NetworkPlugin *np;
public:
TimelineRequest(NetworkPlugin *_np, twitCurl *obj, const std::string &_user) {
TimelineRequest(NetworkPlugin *_np, twitCurl *obj, const std::string &_user, const std::string &_user2) {
twitObj = obj->clone();
np = _np;
user = _user;
userRequested = _user2;
}
~TimelineRequest() {

View file

@ -107,7 +107,7 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
std::istringstream in(message.c_str());
in >> cmd >> data;
handleMessage(user, "twitter-account", cmd + " " + data);
//handleMessage(user, "twitter-account", cmd + " " + data);
if(cmd == "#pin") tp->runAsThread(new PINExchangeProcess(np, sessions[user], user, data));
else if(cmd == "#help") tp->runAsThread(new HelpMessageRequest(np, user));
@ -116,8 +116,9 @@ void TwitterPlugin::handleMessageSendRequest(const std::string &user, const std:
tp->runAsThread(new DirectMessageRequest(np, sessions[user], user, username, data));
}
else if(cmd == "#status") tp->runAsThread(new StatusUpdateRequest(np, sessions[user], user, data));
else if(cmd == "#timeline") tp->runAsThread(new TimelineRequest(np, sessions[user], user));
else if(cmd == "#timeline") tp->runAsThread(new TimelineRequest(np, sessions[user], user, data));
else if(cmd == "#friends") tp->runAsThread(new FetchFriends(np, sessions[user], user));
else handleMessage(user, "twitter-account", "Unknown command! Type #help for a list of available commands.");
}
}

View file

@ -98,3 +98,15 @@ std::vector<std::string> getIDs(std::string &xml)
}
return IDs;
}
std::string getErrorMessage(std::string &xml)
{
std::string error;
Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
const std::string xmlns = rootElement->getNamespace();
const Swift::ParserElement::ref errorElement = rootElement->getChild(TwitterReponseTypes::error, xmlns);
if(errorElement != NULL) error = errorElement->getText();
return error;
}

View file

@ -25,6 +25,7 @@ namespace TwitterReponseTypes
const std::string user = "user";
const std::string users = "users";
const std::string status = "status";
const std::string error = "error";
};
//Class holding user data
@ -98,6 +99,7 @@ class Status
std::vector<Status> getTimeline(std::string &xml);
std::vector<std::string> getIDs(std::string &xml);
std::vector<User> getUsers(std::string &xml);
std::string getErrorMessage(std::string &xml);
Status getStatus(const Swift::ParserElement::ref &element, const std::string xmlns);
User getUser(const Swift::ParserElement::ref &element, const std::string xmlns);
#endif

View file

@ -1472,6 +1472,9 @@ void twitCurl::prepareStandardParams()
/* Restore any custom request we may have */
curl_easy_setopt( m_curlHandle, CURLOPT_CUSTOMREQUEST, NULL );
/* All supported encodings*/
curl_easy_setopt( m_curlHandle, CURLOPT_ENCODING, "" );
/* Clear callback and error buffers */
clearCurlCallbackBuffers();