Permalink
Browse files

some stuff is working

about to start on the saving of tweets to the database
  • Loading branch information...
1 parent 0dd0394 commit 59aee765a0c11073baa12125f76818c1181089d3 @matthewfl committed Jul 23, 2009
Showing with 119 additions and 15 deletions.
  1. +3 −0 .gitignore
  2. +1 −1 Makefile
  3. +14 −0 README
  4. +48 −6 TwitterChatBot.cpp
  5. +14 −3 TwitterChatBot.h
  6. +11 −0 config.h
  7. +1 −0 know.sql
  8. +7 −0 tweet.sql
  9. +20 −5 twitterChat.cpp
View
3 .gitignore
@@ -1 +1,4 @@
*.o
+*.database
+# binary
+/twitterChat
View
2 Makefile
@@ -2,7 +2,7 @@ CXXFLAGS = -O2 -Wall -fmessage-length=0
OBJS = twitterChat.o TwitterChatBot.o
-LIBS = -lhoard -lpthread -lcurl
+LIBS = -lhoard -lpthread -lcurl -lsqlite3
# download libs from:
# hoard.org # not needed but helps
# boost.org
View
14 README
@@ -0,0 +1,14 @@
+TwitterChat is designed to be a chatter bot that works by looking at the tweets from users.
+More specifically the replys to try and find patterns and become a chatter bot using replys.
+The system uses the twitter stream api to get as many replys/tweets that is can.
+
+To build:
+ you need to get sqlite (http://sqlite.org) and libcurl (http://curl.haxx.se/)
+ also the system can use hoard for better memory (http://hoard.org/) if not using you need to remove from the
+libs in the makefile
+
+ you might want to edit the setting in the make file
+ run make
+ then do:
+ sqlite3 tweets.database < tweet.sql
+ sqlite3 know.database < know.sql
View
54 TwitterChatBot.cpp
@@ -1,22 +1,27 @@
#include "TwitterChatBot.h"
#include <pthread.h>
#include <curl/curl.h>
+#include <matthewfl/json.hpp>
#include <string>
+#include <stdlib.h>
#include <iostream>
using namespace std;
void TwitterChatBot::startFeed () {
- feedRunning = true;
- pthread_create(&Pfeed, NULL, StreamThreadStart, (void*)this);
+ /// if feed is running do nothing
+ if(!feedRunning) {
+ feedRunning = true;
+ pthread_create(&Pfeed, NULL, StreamThreadStart, (void*)this);
+ }
}
void TwitterChatBot::stopFeed() {
if(feedRunning) {
pthread_cancel(Pfeed);
curl_easy_pause(curl, CURLPAUSE_ALL);
curl_easy_cleanup(curl);
-
+ curl = NULL;
feedRunning = false;
}
}
@@ -59,14 +64,51 @@ void * TwitterChatBot::StreamThreadStart(void * self) {
size_t TwitterChatBot::StreamCallBack (char * str, size_t size, size_t nmemb, TwitterChatBot * self) {
string s;
s.append(str, size * nmemb);
- self->proccessStream(s);
+ self->tempHolder += s;
+ if(self->tempHolder.find("\n") != string::npos) {
+ self->proccessStream(self->tempHolder);
+ self->tempHolder="";
+ }
return size * nmemb;
}
void TwitterChatBot::proccessStream (string & data) {
- cout << data << endl << endl;
+ //cout << data << endl << endl;
matthewfl::json j;
j.prase(data);
-
+ if(!j["delete"].empty()) {
+ // TODO: make a delete spool for deleting tweets out of the data base
+ return;
+ }
+ if(!j["text"].empty()) {
+ cout << j["text"].cast<matthewfl::json::String>() << endl;
+ if(!j["in_reply_to_user_id"].empty()) {
+ cout << "reply to: ";
+ cout << j["in_reply_to_user_id"].cast<matthewfl::json::Number>() << endl << endl;
+ }
+ }
+}
+
+void * TwitterChatBot::AddTweetData (void * s) {
+ TwitterChatBot * self = reinterpret_cast<TwitterChatBot*>(s);
+ while(1) {
+ while(!self->tweetQueue.empty()) {
+
+ }
+ sleep(1);
+ }
+ return NULL;
+}
+
+unsigned int TwitterChatBot::queueSize() {
+ return tweetQueue.size();
+}
+
+void TwitterChatBot::start() {
+ pthread_create(&PtweetData, NULL, AddTweetData, (void*)this);
+}
+
+void TwitterChatBot::stop() {
+ pthread_cancel(PtweetData);
}
View
17 TwitterChatBot.h
@@ -4,24 +4,35 @@
#include <pthread.h>
#include <curl/curl.h>
#include <string>
+#include <queue>
+#include <sqlite3.h>
class TwitterChatBot
{
private:
+ sqlite3 * Tweet;
+ sqlite3 * Know;
std::string loginInfo;
- pthread_t Pfeed;
+ std::queue<std::string> tweetQueue;
+ pthread_t Pfeed, PtweetData;
CURL * curl;
bool feedRunning;
+ std::string tempHolder;
static void * StreamThreadStart (void*);
static size_t StreamCallBack (char*, size_t, size_t, TwitterChatBot*);
void proccessStream (std::string &);
+ static void * AddTweetData (void*);
+ void start();
+ void stop();
public:
std::string feedUrl;
- TwitterChatBot (std::string user, std::string pass): loginInfo(user+":"+pass), feedRunning(false) {};
- TwitterChatBot (std::string userpwd): loginInfo(userpwd), feedRunning(false) {}
+ TwitterChatBot (std::string user, std::string pass, sqlite3 * tweet, sqlite3 * know): Tweet(tweet), Know(know), loginInfo(user+":"+pass), feedRunning(false) { start(); };
+ TwitterChatBot (std::string userpwd, sqlite3 * tweet, sqlite3 * know): Tweet(tweet), Know(know), loginInfo(userpwd), feedRunning(false) { start(); }
+ ~TwitterChatBot () { stop(); }
void startFeed();
void stopFeed();
+ unsigned int queueSize();
};
View
11 config.h
@@ -1,11 +1,22 @@
#ifndef _twitter_config_h
#define _twitter_config_h
+// if using this for user in password comment out Twitter_login_args
#define Twitter_UserName "matthewfl"
#define Twitter_Password "no way"
+#define Twitter_login_args
+
+// what feed to use
#define Twitter_Feed_url "http://stream.twitter.com/spritzer.json"
+// names of database
+// databases should be inited before running the programs
+#define Twitter_database_tweet "tweets.database"
+#define Twitter_database_knowledge "know.database"
+
+// the max size of all the queues
+#define Twitter_Queue_maxsize 500
#endif // _tiwtter_config_h
View
1 know.sql
@@ -0,0 +1 @@
+
View
7 tweet.sql
@@ -0,0 +1,7 @@
+create table tweet (
+ text TEXT,
+ byID INTEGER,
+ byName TEXT,
+ tweetID INTEGER,
+ toID INTEGER
+);
View
25 twitterChat.cpp
@@ -1,6 +1,7 @@
#include <iostream>
#include <string>
#include <stdlib.h>
+#include <assert.h>
#include "config.h"
#include "TwitterChatBot.h"
@@ -9,15 +10,29 @@
using namespace std;
int main (int argc, char *argv[]) {
- cout << "started\n";
- TwitterChatBot twit(Twitter_UserName, Twitter_Password);
+ sqlite3 *dbTweet, *dbKnow;
+ sqlite3_open(Twitter_database_tweet, &dbTweet);
+ sqlite3_open(Twitter_database_knowledge, &dbKnow);
+#ifdef Twitter_login_args
+ assert(argc > 1);
+ TwitterChatBot twit(argv[1], dbTweet, dbKnow);
+#else
+ TwitterChatBot twit(Twitter_UserName, Twitter_Password, dbTweet, dbKnow);
+#endif
twit.feedUrl = Twitter_Feed_url;
cout << "starting feed\n";
twit.startFeed();
- cout << "back\n";
+
- sleep(10);
+ while(1) {
+ if(twit.queueSize() > Twitter_Queue_maxsize) {
+ twit.stopFeed();
+ }else{
+ twit.startFeed();
+ }
+ sleep(5);
+ }
twit.stopFeed();
- sleep(10);
+ //sleep(10);
return 0;
}

0 comments on commit 59aee76

Please sign in to comment.