Compare commits

..

No commits in common. "2697a5f7fca61eb64fccc6892e94d53852125245" and "85c73cba2a3e8ef3227e8b51dd1f021830966625" have entirely different histories.

4 changed files with 12 additions and 60867 deletions

19
http.c
View file

@ -7,35 +7,24 @@
int curl_setup = 0; int curl_setup = 0;
char* request(char *url ) char* request(char *url )
{ {
// initialize curl on the first call of this function if(!curl_setup)
if(curl_setup == 0)
{ {
curl_global_init(CURL_GLOBAL_DEFAULT); curl_global_init(CURL_GLOBAL_DEFAULT);
curl_setup=1; url +=1;
} }
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(); CURL *httpHandler = curl_easy_init();
CURLcode return_code;
if(httpHandler) { if(httpHandler) {
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_WRITEFUNCTION, httpResponseCallback);
curl_easy_setopt(httpHandler,CURLOPT_WRITEDATA, (void *)&httpResponse); curl_easy_setopt(httpHandler,CURLOPT_WRITEDATA, (void *)&httpResponse);
return_code = curl_easy_perform(httpHandler); curl_easy_perform(httpHandler);
} }
else else
{ return "Lol, fehler";
fprintf(stderr, "CURL Error");
return NULL;
}
printf("%i\n",httpResponse.size);
if(return_code != CURLE_OK)
{
fprintf(stderr, "HTTP Error: %s",curl_easy_strerror(return_code));
return NULL;
}
return httpResponse.response; return httpResponse.response;
} }
size_t httpResponseCallback(char *data, size_t wordlength, size_t bytecount, void *out) size_t httpResponseCallback(char *data, size_t wordlength, size_t bytecount, void *out)

View file

@ -2,7 +2,6 @@
#include <string.h> #include <string.h>
#include <json-c/json_tokener.h> #include <json-c/json_tokener.h>
#include <stdio.h>
json_object* query_lichess(char* username) json_object* query_lichess(char* username)
{ {
@ -12,10 +11,8 @@ 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];
strcpy(user_url,api_base); strcpy(user_url,api_base);
strcat(user_url,username); char* http_response = request(strcat(
printf("%s",user_url); user_url,username));
char* http_response = request(user_url);
printf("\n%p\n",http_response);
json_object *lichessUser = json_tokener_parse(http_response); json_object *lichessUser = json_tokener_parse(http_response);
json_object *profile = json_object_object_get(lichessUser,"profile"); json_object *profile = json_object_object_get(lichessUser,"profile");
return profile; return profile;

31
main.c
View file

@ -32,33 +32,8 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// json_object *profile = malloc(1); // json_object *profile = malloc(1);
FILE *output = fopen("output.csv","w"); json_object *profile = query_lichess("alireza2003");
FILE *playersfile = fopen("players.csv","r"); printf("%s\n",json_object_get_string(profile));
if(playersfile == NULL)
{
fputs("Couldn't open players.csv\n",stdout);
return 1;
}
fputs("lichess_id,name\n",output);
char line [128];
while(fgets(line, sizeof line, playersfile) != NULL)
{
char outline[256];
char name[128];
json_object *profile = query_lichess(line);
char *firstName = json_object_get_string(json_object_object_get(profile,"firstName"));
char *lastName = json_object_get_string(json_object_object_get(profile,"lastName"));
if(firstName == NULL || lastName == NULL)
{
printf("No name for %s",line);
printf("object is: %s",json_object_get_string(profile));
continue;
}
strcpy(name,firstName);
strcat(name," ");
strcat(name,lastName);
sprintf(outline, "%s,%s\n",line,name);
fputs(outline,output);
}
return 0; return 0;
} }

60816
players.csv

File diff suppressed because it is too large Load diff