Added XML Parser. User can now fetch his timeline
This commit is contained in:
parent
f1c11320c7
commit
929b49d641
4 changed files with 171 additions and 4 deletions
|
@ -1,4 +1,6 @@
|
|||
include_directories (${libtransport_SOURCE_DIR}/backends/twitter/libtwitcurl)
|
||||
FILE(GLOB SRC *.cpp libtwitcurl/*.cpp)
|
||||
add_executable(spectrum_twitter_backend ${SRC})
|
||||
#add_executable(parser TwitterResponseParser.cpp test.cpp)
|
||||
target_link_libraries(spectrum_twitter_backend curl transport pthread sqlite3 ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
|
||||
#target_link_libraries(parser curl transport pthread sqlite3 ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
|
||||
|
|
59
backends/twitter/TwitterResponseParser.cpp
Normal file
59
backends/twitter/TwitterResponseParser.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include "TwitterResponseParser.h"
|
||||
|
||||
User getUser(const Swift::ParserElement::ref &element, const std::string xmlns)
|
||||
{
|
||||
User user;
|
||||
if(element->getName() != "user") {
|
||||
std::cerr << "Not a user element!" << std::endl;
|
||||
return user;
|
||||
}
|
||||
|
||||
user.setUserID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
|
||||
user.setScreenName( std::string( element->getChild(TwitterReponseTypes::screen_name, xmlns)->getText() ) );
|
||||
user.setUserName( std::string( element->getChild(TwitterReponseTypes::name, xmlns)->getText() ) );
|
||||
user.setNumberOfTweets( atoi(element->getChild(TwitterReponseTypes::statuses_count, xmlns)->getText().c_str()) );
|
||||
return user;
|
||||
}
|
||||
|
||||
Status getStatus(const Swift::ParserElement::ref &element, const std::string xmlns)
|
||||
{
|
||||
Status status;
|
||||
if(element->getName() != "status") {
|
||||
std::cerr << "Not a status element!" << std::endl;
|
||||
return status;
|
||||
}
|
||||
|
||||
status.setCreationTime( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) );
|
||||
status.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
|
||||
status.setTweet( 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;
|
||||
}
|
||||
|
||||
std::vector<Status> getTimeline(std::string &xml)
|
||||
{
|
||||
std::vector<Status> statuses;
|
||||
Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
|
||||
|
||||
if(rootElement->getName() != "statuses") {
|
||||
std::cerr << "XML doesnt correspond to timline\n";
|
||||
return statuses;
|
||||
}
|
||||
|
||||
const std::string xmlns = rootElement->getNamespace();
|
||||
const std::vector<Swift::ParserElement::ref> children = rootElement->getChildren(TwitterReponseTypes::status, xmlns);
|
||||
// const std::vector<Swift::ParserElement::ref>::iterator it;
|
||||
|
||||
for(int i = 0; i < children.size() ; i++) {
|
||||
const Swift::ParserElement::ref status = children[i];
|
||||
statuses.push_back(getStatus(status, xmlns));
|
||||
}
|
||||
return statuses;
|
||||
}
|
98
backends/twitter/TwitterResponseParser.h
Normal file
98
backends/twitter/TwitterResponseParser.h
Normal file
|
@ -0,0 +1,98 @@
|
|||
#ifndef TWITTERRESPOSNSEPARSER_H
|
||||
#define TWITTERRESPOSNSEPARSER_H
|
||||
|
||||
#include "Swiften/Parser/StringTreeParser.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
namespace TwitterReponseTypes
|
||||
{
|
||||
const std::string id = "id";
|
||||
const std::string name = "name";
|
||||
const std::string screen_name = "screen_name";
|
||||
const std::string statuses_count = "statuses_count";
|
||||
const std::string created_at = "created_at";
|
||||
const std::string text = "text";
|
||||
const std::string truncated = "truncated";
|
||||
const std::string in_reply_to_status_id = "in_reply_to_user_id";
|
||||
const std::string in_reply_to_user_id = "in_reply_to_user_id";
|
||||
const std::string in_reply_to_screen_name = "in_reply_to_screen_name";
|
||||
const std::string retweet_count = "retweet_count";
|
||||
const std::string favorited = "favorited";
|
||||
const std::string retweeted = "retweeted";
|
||||
const std::string user = "user";
|
||||
const std::string status = "status";
|
||||
};
|
||||
|
||||
//Class holding user data
|
||||
class User
|
||||
{
|
||||
std::string ID;
|
||||
std::string name;
|
||||
std::string screen_name;
|
||||
unsigned int statuses_count;
|
||||
|
||||
public:
|
||||
User():ID(""),name(""),screen_name(""),statuses_count(0){}
|
||||
|
||||
std::string getUserID() {return ID;}
|
||||
std::string getUserName() {return name;}
|
||||
std::string getScreenName() {return screen_name;}
|
||||
unsigned int getNumberOfTweets() {return statuses_count;}
|
||||
|
||||
|
||||
void setUserID(std::string _id) {ID = _id;}
|
||||
void setUserName(std::string _name) {name = _name;}
|
||||
void setScreenName(std::string _screen) {screen_name = _screen;}
|
||||
void setNumberOfTweets(unsigned int sc) {statuses_count = sc;}
|
||||
};
|
||||
|
||||
//Class representing a status (tweet)
|
||||
class Status
|
||||
{
|
||||
std::string created_at;
|
||||
std::string ID;
|
||||
std::string text;
|
||||
bool truncated;
|
||||
std::string in_reply_to_status_id;
|
||||
std::string in_reply_to_user_id;
|
||||
std::string in_reply_to_screen_name;
|
||||
User user;
|
||||
unsigned int retweet_count;
|
||||
bool favorited;
|
||||
bool retweeted;
|
||||
|
||||
public:
|
||||
Status():created_at(""),ID(""),text(""),truncated(false),in_reply_to_status_id(""),
|
||||
in_reply_to_user_id(""),in_reply_to_screen_name(""),user(User()),retweet_count(0),
|
||||
favorited(false),retweeted(0){}
|
||||
|
||||
std::string getCreationTime() {return created_at;}
|
||||
std::string getID() {return ID;}
|
||||
std::string getTweet() {return text;}
|
||||
bool isTruncated() {return truncated;}
|
||||
std::string getReplyToStatusID() {return in_reply_to_status_id;}
|
||||
std::string getReplyToUserID() {return in_reply_to_user_id;}
|
||||
std::string getReplyToScreenName() {return in_reply_to_screen_name;}
|
||||
User getUserData() {return user;}
|
||||
unsigned int getRetweetCount() {return retweet_count;}
|
||||
bool isFavorited() {return favorited;}
|
||||
bool isRetweeted() {return retweeted;}
|
||||
|
||||
void setCreationTime(std::string _created) {created_at = _created;}
|
||||
void setID(std::string _id) {ID = _id;}
|
||||
void setTweet(std::string _text) {text = _text;}
|
||||
void setTruncated(bool val) {truncated = val;}
|
||||
void setReplyToStatusID(std::string _id) {in_reply_to_status_id = _id;}
|
||||
void setReplyToUserID(std::string _id) {in_reply_to_user_id = _id;}
|
||||
void setReplyToScreenName(std::string _name) {in_reply_to_screen_name = _name;}
|
||||
void setUserData(User u) {user = u;}
|
||||
void setRetweetCount(unsigned int rc) {retweet_count = rc;}
|
||||
void setFavorited(bool val) {favorited = val;}
|
||||
void setRetweeted(bool val) {retweeted = val;}
|
||||
};
|
||||
|
||||
std::vector<Status> getTimeline(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
|
|
@ -12,7 +12,7 @@
|
|||
#include "sys/signal.h"
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include "twitcurl.h"
|
||||
#include "Swiften/Parser/StringTreeParser.h"
|
||||
#include "TwitterResponseParser.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
@ -211,7 +211,11 @@ class TwitterPlugin : public NetworkPlugin {
|
|||
|
||||
std::string replyMsg;
|
||||
if( sessions[user]->statusUpdate( data ) ) {
|
||||
sessions[user]->getLastWebResponse( replyMsg );
|
||||
replyMsg = "";
|
||||
while(replyMsg.length() == 0) {
|
||||
sessions[user]->getLastWebResponse( replyMsg );
|
||||
}
|
||||
|
||||
LOG4CXX_INFO(logger, "twitCurl:statusUpdate web response: " << replyMsg );
|
||||
}
|
||||
else {
|
||||
|
@ -229,10 +233,14 @@ class TwitterPlugin : public NetworkPlugin {
|
|||
}
|
||||
|
||||
std::string replyMsg;
|
||||
if( sessions[user]->timelinePublicGet()/*(false, false, 20, sessions[user]->getTwitterUsername(), true)*/ ) {
|
||||
if( sessions[user]->timelineHomeGet()/*(false, false, 20, sessions[user]->getTwitterUsername(), true)*/ ) {
|
||||
sessions[user]->getLastWebResponse( replyMsg );
|
||||
LOG4CXX_INFO(logger, "twitCurl::timeline web response: " << replyMsg );
|
||||
handleMessage(user, "twitter-account", replyMsg);
|
||||
|
||||
std::vector<Status> tweets = getTimeline(replyMsg);
|
||||
for(int i=0 ; i<tweets.size() ; i++) {
|
||||
handleMessage(user, "twitter-account", tweets[i].getTweet() + "\n");
|
||||
}
|
||||
} else {
|
||||
sessions[user]->getLastCurlError( replyMsg );
|
||||
LOG4CXX_INFO(logger, "twitCurl::timeline error: " << replyMsg );
|
||||
|
|
Loading…
Add table
Reference in a new issue