#include "ofScraperWebConnection.h"




//-----------------------------------------------------------------
static char errorBuffer[CURL_ERROR_SIZE];
string buffer;

static int stringWriter(char *data, size_t size, size_t nmemb, std::string *buffer);
static int stringWriter(char *data, size_t size, size_t nmemb,
                  std::string *buffer){
  	int result = 0;
	if (buffer != NULL){
    	buffer->append(data, size * nmemb);
    	result = size * nmemb;
  	}
  return result;
}

static int fileWriter(void *ptr, size_t size, size_t nmemb, void *stream);
static int fileWriter(void *ptr, size_t size, size_t nmemb, void *stream)
{
	return (int)fwrite(ptr, size, nmemb, (FILE *)stream);
}


//-----------------------------------------------------------------

string ofScraperWebConnection::getString(){
	return buffer;
}

//-----------------------------------------------------------------
void ofScraperWebConnection::getWebPageAsString(string url){
	
	
	buffer = "";
	
	 // Our curl objects
    CURL *curl;
    CURLcode result;

    // Create our curl handle
    curl = curl_easy_init();

    if (curl)
    {
		char * userAgent = "Googlebot/2.1 (http://www.googlebot.com/bot.html)";
		curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent);
		curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
		curl_easy_setopt(curl, CURLOPT_HEADER, 0);
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, stringWriter);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
		result = curl_easy_perform(curl);
		curl_easy_cleanup(curl);	
		if (result == CURLE_OK){
			// ok 
		} else {
			// not ok
		}
	} else {
		// wtf?
	}
}


//-----------------------------------------------------------------
void ofScraperWebConnection::getWebPageToFile(string url, string filename){
	
	
	FILE *fd;
	
	//filename =  data path... add me
	
	 // Our curl objects
    CURL *curl;
    CURLcode result;

    // Create our curl handle
    curl = curl_easy_init();

    if (curl)
    {
		fd = fopen(filename.c_str(), "wb");
		
		char * userAgent = "Googlebot/2.1 (http://www.googlebot.com/bot.html)";
		curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent);
		curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
		curl_easy_setopt(curl, CURLOPT_HEADER, 0);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fileWriter);
		curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fd);
		curl_easy_setopt(curl, CURLOPT_FILETIME, 1);
		curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
		curl_easy_setopt(curl, CURLOPT_TIMEOUT, 45);
		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
		result = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
		fclose(fd);	
		if (result == CURLE_OK){
			// ok 
			// can do something with mode time here
			//long modTime;
			//curl_easy_getinfo(curl, CURLINFO_FILETIME, &modTime);
			
		} else {
			// not ok
		}
	} else {
		// wtf?
	}
}
//-----------------------------------------------------------------
