Compare commits

...

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

4 changed files with 60867 additions and 12 deletions

19
http.c
View file

@ -7,24 +7,35 @@
int curl_setup = 0; int curl_setup = 0;
char* request(char *url ) char* request(char *url )
{ {
if(!curl_setup) // initialize curl on the first call of this function
if(curl_setup == 0)
{ {
curl_global_init(CURL_GLOBAL_DEFAULT); curl_global_init(CURL_GLOBAL_DEFAULT);
url +=1; curl_setup=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);
curl_easy_perform(httpHandler); return_code = 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,6 +2,7 @@
#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)
{ {
@ -11,8 +12,10 @@ 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);
char* http_response = request(strcat( strcat(user_url,username);
user_url,username)); printf("%s",user_url);
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,8 +32,33 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// json_object *profile = malloc(1); // json_object *profile = malloc(1);
json_object *profile = query_lichess("alireza2003"); FILE *output = fopen("output.csv","w");
printf("%s\n",json_object_get_string(profile)); FILE *playersfile = fopen("players.csv","r");
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 Normal file

File diff suppressed because it is too large Load diff