diff --git a/backends/twitter/CMakeLists.txt b/backends/twitter/CMakeLists.txt index 0f2b6f27..f5ad1a9b 100644 --- a/backends/twitter/CMakeLists.txt +++ b/backends/twitter/CMakeLists.txt @@ -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}) diff --git a/backends/twitter/TwitterResponseParser.cpp b/backends/twitter/TwitterResponseParser.cpp new file mode 100644 index 00000000..04b0ba88 --- /dev/null +++ b/backends/twitter/TwitterResponseParser.cpp @@ -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 getTimeline(std::string &xml) +{ + std::vector 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 children = rootElement->getChildren(TwitterReponseTypes::status, xmlns); +// const std::vector::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; +} diff --git a/backends/twitter/TwitterResponseParser.h b/backends/twitter/TwitterResponseParser.h new file mode 100644 index 00000000..3eb9b507 --- /dev/null +++ b/backends/twitter/TwitterResponseParser.h @@ -0,0 +1,98 @@ +#ifndef TWITTERRESPOSNSEPARSER_H +#define TWITTERRESPOSNSEPARSER_H + +#include "Swiften/Parser/StringTreeParser.h" +#include +#include + +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 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 diff --git a/backends/twitter/main.cpp b/backends/twitter/main.cpp index 7731c7b5..9f5c12a8 100644 --- a/backends/twitter/main.cpp +++ b/backends/twitter/main.cpp @@ -12,7 +12,7 @@ #include "sys/signal.h" #include #include "twitcurl.h" -#include "Swiften/Parser/StringTreeParser.h" +#include "TwitterResponseParser.h" #include #include @@ -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 tweets = getTimeline(replyMsg); + for(int i=0 ; igetLastCurlError( replyMsg ); LOG4CXX_INFO(logger, "twitCurl::timeline error: " << replyMsg );