Added threaded timeline request and fetch friends request
This commit is contained in:
parent
2b47d32a91
commit
f87f8cc356
5 changed files with 146 additions and 4 deletions
41
backends/twitter/Requests/FetchFriends.cpp
Normal file
41
backends/twitter/Requests/FetchFriends.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include "FetchFriends.h"
|
||||
DEFINE_LOGGER(logger, "FetchFriends")
|
||||
|
||||
void FetchFriends::run()
|
||||
{
|
||||
replyMsg = "";
|
||||
if( twitObj.friendsIdsGet(twitObj.getTwitterUsername())) {
|
||||
|
||||
while(replyMsg.length() == 0) {
|
||||
twitObj.getLastWebResponse( replyMsg );
|
||||
}
|
||||
|
||||
LOG4CXX_INFO(logger, user << " - " << replyMsg.length() << " " << replyMsg << "\n" );
|
||||
|
||||
std::vector<std::string> IDs = getIDs( replyMsg );
|
||||
|
||||
twitObj.userLookup(IDs, true);
|
||||
twitObj.getLastWebResponse( replyMsg );
|
||||
|
||||
LOG4CXX_INFO(logger, user << " - UserLookUp web response - " << replyMsg.length() << " " << replyMsg << "\n" );
|
||||
|
||||
std::vector<User> users = getUsers( replyMsg );
|
||||
|
||||
userlist = "\n***************USER LIST****************\n";
|
||||
for(int i=0 ; i < users.size() ; i++) {
|
||||
userlist += "*)" + users[i].getUserName() + " (" + users[i].getScreenName() + ")\n";
|
||||
}
|
||||
userlist += "***************************************\n";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void FetchFriends::finalize()
|
||||
{
|
||||
if(replyMsg != "" ) {
|
||||
np->handleMessage(user, "twitter-account", userlist);
|
||||
} else {
|
||||
twitObj.getLastCurlError( replyMsg );
|
||||
LOG4CXX_INFO(logger, user << " - friendsIdsGet error - " << replyMsg );
|
||||
}
|
||||
}
|
32
backends/twitter/Requests/FetchFriends.h
Normal file
32
backends/twitter/Requests/FetchFriends.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
#ifndef FRIENDS_H
|
||||
#define FRIENDS_H
|
||||
|
||||
#include "../ThreadPool.h"
|
||||
#include "../libtwitcurl/twitcurl.h"
|
||||
#include "../TwitterResponseParser.h"
|
||||
#include "transport/networkplugin.h"
|
||||
#include "transport/logging.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
using namespace Transport;
|
||||
|
||||
class FetchFriends : public Thread
|
||||
{
|
||||
twitCurl twitObj;
|
||||
std::string user;
|
||||
std::string replyMsg;
|
||||
std::string userlist;
|
||||
NetworkPlugin *np;
|
||||
|
||||
public:
|
||||
FetchFriends(NetworkPlugin *_np, twitCurl *obj, const std::string &_user) {
|
||||
twitObj = *obj;
|
||||
np = _np;
|
||||
user = _user;
|
||||
}
|
||||
|
||||
void run();
|
||||
void finalize();
|
||||
};
|
||||
#endif
|
33
backends/twitter/Requests/TimelineRequest.cpp
Normal file
33
backends/twitter/Requests/TimelineRequest.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include "TimelineRequest.h"
|
||||
DEFINE_LOGGER(logger, "TimelineRequest")
|
||||
void TimelineRequest::run()
|
||||
{
|
||||
replyMsg = "";
|
||||
if( twitObj.timelinePublicGet() ) {
|
||||
LOG4CXX_INFO(logger, "Sending timeline request for user " << user)
|
||||
|
||||
while(replyMsg.length() == 0) {
|
||||
twitObj.getLastWebResponse( replyMsg );
|
||||
}
|
||||
|
||||
LOG4CXX_INFO(logger, user << " - " << replyMsg.length() << " " << replyMsg << "\n" );
|
||||
|
||||
std::vector<Status> tweets = getTimeline(replyMsg);
|
||||
timeline = "\n";
|
||||
for(int i=0 ; i<tweets.size() ; i++) {
|
||||
timeline += tweets[i].getTweet() + "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TimelineRequest::finalize()
|
||||
{
|
||||
if(replyMsg.length()) {
|
||||
LOG4CXX_INFO(logger, user << "'s timeline\n" << replyMsg);
|
||||
np->handleMessage(user, "twitter-account", timeline); //send timeline
|
||||
}
|
||||
else {
|
||||
twitObj.getLastCurlError( replyMsg );
|
||||
LOG4CXX_ERROR(logger, user << " - " << replyMsg );
|
||||
}
|
||||
}
|
32
backends/twitter/Requests/TimelineRequest.h
Normal file
32
backends/twitter/Requests/TimelineRequest.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
#ifndef TIMELINE_H
|
||||
#define TIMELINE_H
|
||||
|
||||
#include "../ThreadPool.h"
|
||||
#include "../libtwitcurl/twitcurl.h"
|
||||
#include "../TwitterResponseParser.h"
|
||||
#include "transport/networkplugin.h"
|
||||
#include "transport/logging.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
using namespace Transport;
|
||||
|
||||
class TimelineRequest : public Thread
|
||||
{
|
||||
twitCurl twitObj;
|
||||
std::string user;
|
||||
std::string replyMsg;
|
||||
std::string timeline;
|
||||
NetworkPlugin *np;
|
||||
|
||||
public:
|
||||
TimelineRequest(NetworkPlugin *_np, twitCurl *obj, const std::string &_user) {
|
||||
twitObj = *obj;
|
||||
np = _np;
|
||||
user = _user;
|
||||
}
|
||||
|
||||
void run();
|
||||
void finalize();
|
||||
};
|
||||
#endif
|
|
@ -31,6 +31,8 @@
|
|||
#include "ThreadPool.h"
|
||||
#include "Requests/StatusUpdateRequest.h"
|
||||
#include "Requests/DirectMessageRequest.h"
|
||||
#include "Requests/TimelineRequest.h"
|
||||
#include "Requests/FetchFriends.h"
|
||||
|
||||
using namespace boost::filesystem;
|
||||
using namespace boost::program_options;
|
||||
|
@ -405,14 +407,16 @@ class TwitterPlugin : public NetworkPlugin {
|
|||
else if(cmd[0] == '@') {
|
||||
std::string username = cmd.substr(1);
|
||||
tp->runAsThread(new DirectMessageRequest(np, sessions[user], user, username, data));
|
||||
//handleDirectMessage(user, username, data);
|
||||
}
|
||||
else if(cmd == "#status") {
|
||||
tp->runAsThread(new StatusUpdateRequest(np, sessions[user], user, data));
|
||||
//handleStatusUpdate(user, data);
|
||||
}
|
||||
//else if(cmd == "#timeline") fetchTimeline(user);
|
||||
//else if(cmd == "#friends") fetchFriends(user);
|
||||
else if(cmd == "#timeline") {
|
||||
tp->runAsThread(new TimelineRequest(np, sessions[user], user));
|
||||
}
|
||||
else if(cmd == "#friends") {
|
||||
tp->runAsThread(new FetchFriends(np, sessions[user], user));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue