Public feed #890244

FreeRice.com Bot

public cpp v1 · immutable
Raw Edit
·published 2008-02-03 16:10 UTC
rendered paste body
//----------- main.h -----------#pragma comment(lib, "libcurl.lib")#include <iostream>#include <curl/curl.h>char* ProcessPage(char* pageData);char* SubmitAnswer(char* past, char* info, char* info2, char* info3, unsigned int answer);char* ProcessNextPage(char* curPage);char* GetInitialPage();unsigned int GoogleDefinition(char* keyword, char* option[4]);unsigned int Thesaurus(char* keyword, char* option[4]);unsigned int Dictionary(char* keyword, char* option[4]);unsigned int Wiktionary(char* keyword, char* option[4]);void *myrealloc(void *ptr, size_t size);size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data);//----------- main.cpp -----------#include "main.h"CURL *curl;unsigned int curAttempts;struct MemoryStruct {  char *memory;  size_t size;};void *myrealloc(void *ptr, size_t size){  if(ptr)    return realloc(ptr, size);  else    return malloc(size);}size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data){  size_t realsize = size * nmemb;  struct MemoryStruct *mem = (struct MemoryStruct *)data;  mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);  if(mem->memory) {    memcpy(&(mem->memory[mem->size]), ptr, realsize);    mem->size += realsize;    mem->memory[mem->size] = 0;  }  return realsize;}unsigned int Thesaurus(char* keyword, char* option[4]){	char urlBuffer[255];	sprintf_s(urlBuffer, 255, "http://thesaurus.reference.com/browse/%s", keyword);	struct MemoryStruct chunk;	chunk.memory = NULL;	chunk.size = 0;	curl_easy_reset(curl);  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);	curl_easy_setopt(curl, CURLOPT_URL, urlBuffer);	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk);	curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");	curl_easy_perform(curl);	unsigned int retVal = rand() % 4;	if(chunk.memory == NULL){		return retVal;	}	char* definitionList = chunk.memory;	char* temp;	unsigned int foundCount = 0;	bool found[4];	for(unsigned int i = 0; i < 4; i++){		found[i] = false;		temp = strstr(definitionList, option[i]);		if(temp != NULL){found[i] = true; foundCount++; break;}	}	if(foundCount == 1){		for(unsigned int i = 0; i < 4; i++){			if(found[i]){				retVal = i;				goto done;			}		}			}else{		retVal = Dictionary(keyword, option);		goto done;	}done:	if(chunk.memory) free(chunk.memory);	return retVal;}unsigned int Dictionary(char* keyword, char* option[4]){	char urlBuffer[255];	sprintf_s(urlBuffer, 255, "http://dictionary.reference.com/browse/%s", keyword);	struct MemoryStruct chunk;	chunk.memory = NULL;	chunk.size = 0;	curl_easy_reset(curl);  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);	curl_easy_setopt(curl, CURLOPT_URL, urlBuffer);	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk);	curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");	curl_easy_perform(curl);	unsigned int retVal = rand() % 4;	if(chunk.memory == NULL){		return retVal;	}	char* definitionList = chunk.memory;	char* temp;	unsigned int foundCount = 0;	bool found[4];	for(unsigned int i = 0; i < 4; i++){		found[i] = false;		temp = strstr(definitionList, option[i]);		if(temp != NULL){found[i] = true; foundCount++; break;}	}	if(foundCount == 1){		for(unsigned int i = 0; i < 4; i++){			if(found[i]){				retVal = i;				goto done;			}		}			}else{		retVal = Wiktionary(keyword, option);		goto done;	}done:	if(chunk.memory) free(chunk.memory);	return retVal;}unsigned int Wiktionary(char* keyword, char* option[4]){	char urlBuffer[255];	sprintf_s(urlBuffer, 255, "http://en.wiktionary.org/wiki/%s", keyword);	struct MemoryStruct chunk;	chunk.memory = NULL;	chunk.size = 0;	curl_easy_reset(curl);  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);	curl_easy_setopt(curl, CURLOPT_URL, urlBuffer);	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk);	curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");	curl_easy_perform(curl);	unsigned int retVal = rand() % 4;	if(chunk.memory == NULL){		return retVal;	}	char* definitionList = chunk.memory;	char* temp;	unsigned int foundCount = 0;	bool found[4];	for(unsigned int i = 0; i < 4; i++){		found[i] = false;		temp = strstr(definitionList, option[i]);		if(temp != NULL){found[i] = true; foundCount++; break;}	}	if(foundCount == 1){		for(unsigned int i = 0; i < 4; i++){			if(found[i]){				retVal = i;				goto done;			}		}			}else{		retVal = GoogleDefinition(keyword, option);		goto done;	}done:	if(chunk.memory) free(chunk.memory);	return retVal;}unsigned int GoogleDefinition(char* keyword, char* option[4]){	char urlBuffer[255];	sprintf_s(urlBuffer, 255, "http://www.google.co.uk/search?q=define:%s", keyword);		struct MemoryStruct chunk;	chunk.memory = NULL;	chunk.size = 0;	curl_easy_reset(curl);  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);	curl_easy_setopt(curl, CURLOPT_URL, urlBuffer);	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk);	curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");	curl_easy_perform(curl);	unsigned int retVal = rand() % 4;	if(chunk.memory == NULL){		return retVal;	}	char* definitionList = strstr(chunk.memory, "<ul type=\"disc\">");	if(!definitionList) goto done;	definitionList += strlen("<ul type=\"disc\">");	*(definitionList-1) = 0;	char* temp;	unsigned int foundCount = 0;	bool found[4];	for(unsigned int i = 0; i < 4; i++){		found[i] = false;		temp = strstr(definitionList, option[i]);		if(temp != NULL){found[i] = true; foundCount++;}	}	if(foundCount == 1){		for(unsigned int i = 0; i < 4; i++){			if(found[i]){				retVal = i;				goto done;			}		}			}done:	if(chunk.memory) free(chunk.memory);	return retVal;}char* GetInitialPage(){	struct MemoryStruct chunk;	chunk.memory = NULL;	chunk.size = 0;	curl_easy_reset(curl);  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);	curl_easy_setopt(curl, CURLOPT_URL, "http://www.freerice.com/index.php");	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);	curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");	curl_easy_perform(curl);	char* nextPage = ProcessPage(chunk.memory);	if(chunk.memory) free(chunk.memory);	return nextPage;}char* ProcessNextPage(char* curPage){	char* nextPage = ProcessPage(curPage);	free(curPage);	return nextPage;}char* SubmitAnswer(char* past, char* info, char* info2, char* info3, unsigned int answer){	struct curl_httppost *formpost=NULL;  struct curl_httppost *lastptr=NULL;	struct MemoryStruct chunk;	chunk.memory = NULL;	chunk.size = 0;	curl_easy_reset(curl);  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);  curl_formadd(&formpost, &lastptr,							CURLFORM_COPYNAME, "PAST",							CURLFORM_COPYCONTENTS, past,							CURLFORM_END);  curl_formadd(&formpost, &lastptr,							CURLFORM_COPYNAME, "INFO",							CURLFORM_COPYCONTENTS, info,							CURLFORM_END);  curl_formadd(&formpost, &lastptr,							CURLFORM_COPYNAME, "INFO2",							CURLFORM_COPYCONTENTS, info2,							CURLFORM_END);  curl_formadd(&formpost, &lastptr,							CURLFORM_COPYNAME, "INFO3",							CURLFORM_COPYCONTENTS, info3,							CURLFORM_END);	char buffer[8];	sprintf_s(buffer, 8, "%u", answer+1);  curl_formadd(&formpost, &lastptr,							CURLFORM_COPYNAME, "SELECTED",							CURLFORM_COPYCONTENTS, buffer,							CURLFORM_END);	curl_easy_setopt(curl, CURLOPT_URL, "http://www.freerice.com/index.php");	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);	curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");	curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);	curl_easy_perform(curl);	curl_formfree(formpost);	return chunk.memory;}char* ProcessPage(char* pageData){	char* temp = strstr(pageData, " grains of rice.");	unsigned int donatedAmount = 0;	unsigned int vocabLevel = 0;	if(temp != NULL){		char* temp2;		char tempChar = 0;		unsigned int maxSize = 5;		tempChar = *temp;		temp2 = temp;		*temp = 0;		temp -= maxSize;		for(unsigned int i = 0; i < maxSize; i++){			if(temp[i] < '0' || temp[i] > '9'){				temp++;				i--;				maxSize--;			}		}		donatedAmount = atoi(temp);		*temp2 = tempChar;	}	temp = strstr(pageData, "VOCAB LEVEL: ");	if(temp != NULL){		temp += strlen("VOCAB LEVEL: ");		char* temp2 = strstr(temp, "<br>");		*temp2 = 0;		vocabLevel = atoi(temp);		*temp2 = '<';	}	char* past = strstr(pageData, "name=\"PAST\"") + strlen("name=\"PAST\" value='");	temp = strstr(past, "' />");	*temp = 0;	temp++;	char* info = strstr(temp, "name=\"INFO\"") + strlen("name=\"INFO\" value=");	temp = strstr(info, " />");	*temp = 0;	temp++;	char* info2 = strstr(temp, "name=\"INFO2\"") + strlen("name=\"INFO2\" value=");	temp = strstr(info2, " />");	*temp = 0;	temp++;	char* info3 = strstr(temp, "name=\"INFO3\"") + strlen("name=\"INFO3\" value='");	temp = strstr(info3, "' />");	*temp = 0;	temp++;	char* wordlist = strstr(temp, "<!-- end wordSelection-->");	if(wordlist == 0){		char* test = strstr(temp, "unable");		if(test != NULL){			printf("Shit bitch, were too fast!\n");			system("pause");			exit(-1);		}else{			printf("=========WTF HAX============\n%s\n", temp);		}		exit(-1);	}	*wordlist = 0;	wordlist = strstr(temp, "class=\"wordSelection\"");	char* keyword = strstr(wordlist, "<li><strong>");	keyword += strlen("<li><strong>");	temp = strstr(keyword, "</strong>");	*temp = 0;	wordlist = temp+1;		if(curAttempts != 0){		if(vocabLevel != 0)			printf("Donated Rice: %d Vocab Level: %d (%4.2f%% success rate.)      \r", donatedAmount, vocabLevel, (100.0f / (float)curAttempts) * ((float)donatedAmount / 20));		else			printf("Donated Rice: %d (%4.2f%% success rate.)                      \r", donatedAmount, (100.0f / (float)curAttempts) * ((float)donatedAmount / 20));			}		char* option[4];	for(unsigned int i = 0; i < 4; i++){		option[i] = strstr(wordlist, "')\">") + strlen("')\">");		temp = strstr(option[i], "</a></li>");		*temp = 0;		wordlist = temp+1;		//printf("Option %u: %s\n", i, option[i]);	}		unsigned int answer = Thesaurus(keyword, option);	//printf("Correct Answer: %d\n", answer);	return SubmitAnswer(past, info, info2, info3, answer);}int main(int argc, char** argv){  curl_global_init(CURL_GLOBAL_ALL);  curl = curl_easy_init();	if(!curl){		printf("Error initiating CURL!\n");		return 0;	}	char* lastPage = GetInitialPage();	curAttempts = 1;	while(1){		Sleep(500);		lastPage = ProcessNextPage(lastPage);		curAttempts++;	}  curl_easy_cleanup(curl);	return 0;}