Compare commits

...

1 commit

Author SHA1 Message Date
2e85e022ab Fix memory leaks 2020-12-27 00:13:27 +01:00
3 changed files with 36 additions and 41 deletions

23
http.c
View file

@ -4,40 +4,37 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <stdlib.h> #include <stdlib.h>
int curl_setup = 0; static CURL* httpHandler = NULL;
char* request(char *url) char* request(char *url)
{ {
// initialize curl on the first call of this function // initialize curl on the first call of this function
if(curl_setup == 0) if(!httpHandler)
{ {
curl_global_init(CURL_GLOBAL_DEFAULT); httpHandler = curl_easy_init();
curl_setup=1; curl_easy_setopt(httpHandler,CURLOPT_USERAGENT, "cWikiBot/0.1 (https://git.zom.bi/cpp/cWikiBot; cpp@zom.bi) libcurl4/7.64.0");
curl_easy_setopt(httpHandler,CURLOPT_WRITEFUNCTION, httpResponseCallback);
} }
struct httpResponse_t httpResponse; struct httpResponse_t httpResponse;
httpResponse.response = malloc(1); httpResponse.response = malloc(1);
httpResponse.size = 0; httpResponse.size = 0;
CURL *httpHandler = curl_easy_init();
CURLcode return_code; CURLcode return_code;
if(httpHandler) { if(httpHandler) {
for(int i=0;i<128;i++)
{
fprintf(stderr,"%x",url[i]);
}
curl_easy_setopt(httpHandler,CURLOPT_URL, url); curl_easy_setopt(httpHandler,CURLOPT_URL, url);
curl_easy_setopt(httpHandler,CURLOPT_WRITEFUNCTION, httpResponseCallback);
curl_easy_setopt(httpHandler,CURLOPT_USERAGENT, "cWikiBot/0.1 (https://git.zom.bi/cpp/cWikiBot; cpp@zom.bi) libcurl4/7.64.0");
curl_easy_setopt(httpHandler,CURLOPT_WRITEDATA, (void *)&httpResponse); curl_easy_setopt(httpHandler,CURLOPT_WRITEDATA, (void *)&httpResponse);
return_code = curl_easy_perform(httpHandler); return_code = curl_easy_perform(httpHandler);
} }
else else
{ {
fprintf(stderr, "CURL Error"); fprintf(stderr, "CURL Error\n");
free(httpResponse.response);
return NULL; return NULL;
} }
if(return_code != CURLE_OK) if(return_code != CURLE_OK)
{ {
fprintf(stderr, "HTTP Error: %s\n",curl_easy_strerror(return_code)); fprintf(stderr, "HTTP Error: %s (%d)\n",curl_easy_strerror(return_code), return_code);
free(httpResponse.response);
return NULL; return NULL;
} }
return httpResponse.response; return httpResponse.response;

View file

@ -4,6 +4,7 @@
#include <json-c/json_tokener.h> #include <json-c/json_tokener.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
json_object* query_lichess(char* username) json_object* query_lichess(char* username)
{ {
@ -13,22 +14,22 @@ json_object* query_lichess(char* username)
char* api_base = "https://lichess.org/api/user/"; char* api_base = "https://lichess.org/api/user/";
char user_url[128]; char user_url[128];
int retries = 5; int retries = 5;
//strcpy(user_url,api_base); int user_url_length = sprintf(user_url,"%s%s",api_base,username);
//strcat(user_url,username);
int user_url_length = sprintf(user_url,"%s%s\0",api_base,username);
user_url[user_url_length]=0;
printf("querying %s\n",user_url); printf("querying %s\n",user_url);
char* http_response = request(user_url); char* http_response = request(user_url);
while(http_response == NULL) while(http_response == NULL)
{ {
printf("didn't receive HTTP response, we might be rate-limited. Waiting 60s"); printf("didn't receive HTTP response, we might be rate-limited. Waiting 5s.\n");
sleep(60); sleep(5);
http_response = request(user_url); http_response = request(user_url);
retries--; retries--;
if(retries == 0) if(retries == 0 && http_response == NULL)
return NULL; return NULL;
} }
json_object *lichessUser = json_tokener_parse(http_response); json_object *lichessUser = json_tokener_parse(http_response);
free(http_response);
json_object *profile = json_object_object_get(lichessUser,"profile"); json_object *profile = json_object_object_get(lichessUser,"profile");
json_object_get(profile);
json_object_put(lichessUser);
return profile; return profile;
} }

19
main.c
View file

@ -23,6 +23,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#include <json-c/json_object.h> #include <json-c/json_object.h>
@ -42,12 +43,11 @@ int main(int argc, char **argv)
fputs("lichess_id,name\n",output); fputs("lichess_id,name\n",output);
char line [128]; char line [128];
int j=0; int j=0;
while(fgets(line, sizeof line, playersfile) != NULL) while(fgets(line, 128, playersfile) != NULL)
{ {
printf("%i",j); printf("%i: ",j);
j++; j++;
char outline[256]; char outline[256];
char name[128];
line[strlen(line)-1] = '\0'; line[strlen(line)-1] = '\0';
json_object *profile = query_lichess(line); json_object *profile = query_lichess(line);
if(profile == NULL) if(profile == NULL)
@ -55,21 +55,18 @@ int main(int argc, char **argv)
printf("No profile for %s\n", line); printf("No profile for %s\n", line);
continue; continue;
} }
char *firstName = json_object_get_string(json_object_object_get(profile,"firstName")); const char *firstName = json_object_get_string(json_object_object_get(profile,"firstName"));
char *lastName = json_object_get_string(json_object_object_get(profile,"lastName")); const char *lastName = json_object_get_string(json_object_object_get(profile,"lastName"));
if(firstName == NULL || lastName == NULL) if(firstName == NULL || lastName == NULL)
{ {
printf("No name for %s\n",line); printf("No name for %s\n",line);
json_object_put(profile);
continue; continue;
} }
strcpy(name,firstName); sprintf(outline, "%s,%s %s\n",line,firstName,lastName);
strcat(name," ");
strcat(name,lastName);
sprintf(outline, "%s,%s\n",line,name);
printf("OK\n"); printf("OK\n");
fputs(outline,output); fputs(outline,output);
fclose(output); json_object_put(profile);
fopen("output.csv","a");
} }
fclose(output); fclose(output);
fclose(playersfile); fclose(playersfile);