Last active
October 18, 2019 00:43
-
-
Save eddie-chinbat/13ed40d2df0326dc3bf3 to your computer and use it in GitHub Desktop.
Twitter streaming API example on c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <oauth.h> | |
#include <curl/curl.h> | |
#include <ncurses.h> | |
#include <boost/date_time/posix_time/posix_time.hpp> | |
#include <boost/property_tree/ptree.hpp> | |
#include <boost/property_tree/json_parser.hpp> | |
#include <boost/foreach.hpp> | |
typedef boost::posix_time::ptime Time; | |
typedef boost::posix_time::time_duration TimeDuration; | |
using namespace std; | |
using namespace boost::property_tree; | |
int ctr=0; //Message count | |
/*struct MemoryStruct { | |
char *memory; | |
size_t size; | |
}; | |
*/ | |
size_t fnParser_callback(char* ptr, size_t size, size_t nmemb, void *userdata) | |
{ | |
size_t totalsize = size * nmemb; | |
/* cout<<"11*************************12"; | |
for(int a=0; a<nmemb; a++){ | |
wcout<<ptr; | |
ctr++; | |
} | |
cout<<"21*************************22"; | |
*/ | |
//cout<<"Size: "<< size<<endl; | |
//struct MemoryStruct *mem = (struct MemoryStruct *)userdata; | |
//mem->memory = realloc(mem->memory, mem->size + totalsize + 1); | |
//mem->memory[mem->size] = 0; | |
ctr++; | |
wcout<<ptr<<endl; | |
//wcout<<"Total size: "<<totalsize<<endl; | |
return totalsize; | |
} | |
int main(void) | |
{ | |
CURL *curl; | |
CURLcode res; | |
// struct MemoryStruct chunk; | |
// chunk.memory = static_cast<char*>(calloc(1024,sizeof(char))); | |
// chunk.size = 0; /* no data at this point */ | |
//Setting dev ID and tokens | |
const char *URL = "https://stream.twitter.com/1.1/statuses/sample.json"; | |
const char *CONS_KEY = "your cons_key from twitter dev"; | |
const char *CONS_SEC = "your cons_sec from twitter dev"; | |
const char *ATOK_KEY = "your atok_key from twitter dev"; | |
const char *ATOK_SEC = "your atok_sec from twitter dev"; | |
//Starting time measurement | |
Time t1(boost::posix_time::microsec_clock::local_time()); | |
// ==== cURL Initialization | |
curl_global_init(CURL_GLOBAL_ALL); | |
//---edy added curl = curl_easy_init(); | |
curl = curl_easy_init(); | |
if (!curl) { | |
cout << "[ERROR] curl_easy_init" << endl; | |
curl_global_cleanup(); | |
return 0; | |
} | |
// ==== cURL Setting | |
// - URL, POST parameters, OAuth signing method, HTTP method, OAuth keys | |
char *cSignedUrl = oauth_sign_url2( | |
URL, NULL, OA_HMAC, "GET", | |
CONS_KEY, CONS_SEC, ATOK_KEY, ATOK_SEC | |
); | |
// - URL | |
curl_easy_setopt(curl, CURLOPT_URL, cSignedUrl); | |
// - User agent name | |
//curl_easy_setopt(curl, CURLOPT_USERAGENT, "ELC twitter streaming"); | |
// - HTTP STATUS >=400 ---> ERROR | |
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); | |
// - Timeout set | |
//curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); | |
// - Callback function | |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fnParser_callback); | |
// - Download data into chunk of memory. | |
// curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); | |
// ==== Execute | |
int error = curl_easy_perform(curl); | |
/* Check for errors */ | |
if(error != CURLE_OK) | |
wcout << "\n\n[ERROR] curl_easy_perform() failed: "<< error <<endl; | |
// ==== cURL Cleanup | |
curl_easy_cleanup(curl); | |
// free(chunk.memory); | |
curl_global_cleanup(); | |
//Curses | |
initscr(); | |
cbreak(); | |
for(;;) { | |
//fnParser(); | |
mvprintw(1,0, "Counting: %d", ctr); | |
if (ERR!=getch()) { | |
endwin(); | |
//Ending time measurement | |
Time t2(boost::posix_time::microsec_clock::local_time()); | |
//Result of Time measurement | |
TimeDuration td = t2 - t1; | |
//number of elapsed miliseconds | |
long msec = td.total_milliseconds(); | |
wcout<<"\n\nCounted msg: "<< ctr<<endl; | |
wcout<<"\n"<<msec/1000.0<<" ms"<<endl; | |
exit(EXIT_SUCCESS); | |
} | |
} | |
cout<<"DONE!!!\n "; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment