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;
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);
url +=1;
curl_setup=1;
}
struct httpResponse_t httpResponse;
httpResponse.response = malloc(1);
httpResponse.size = 0;
CURL *httpHandler = curl_easy_init();
CURLcode return_code;
if(httpHandler) {
curl_easy_setopt(httpHandler,CURLOPT_URL, url);
curl_easy_setopt(httpHandler,CURLOPT_WRITEFUNCTION, httpResponseCallback);
curl_easy_setopt(httpHandler,CURLOPT_WRITEDATA, (void *)&httpResponse);
curl_easy_perform(httpHandler);
return_code = curl_easy_perform(httpHandler);
}
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;
}
size_t httpResponseCallback(char *data, size_t wordlength, size_t bytecount, void *out)

View file

@ -2,17 +2,20 @@
#include <string.h>
#include <json-c/json_tokener.h>
#include <stdio.h>
json_object* query_lichess(char* username)
{
/* This function queries the lichess api and returns a pointer to a
* json_object, that contains the profile of the queried player
*/
char* api_base = "https://lichess.org/api/user/";
char user_url[128];
strcpy(user_url,api_base);
char* http_response = request(strcat(
user_url,username));
char* api_base = "https://lichess.org/api/user/";
char user_url[128];
strcpy(user_url,api_base);
strcat(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 *profile = json_object_object_get(lichessUser,"profile");
return profile;

31
main.c
View file

@ -32,8 +32,33 @@
int main(int argc, char **argv)
{
// json_object *profile = malloc(1);
json_object *profile = query_lichess("alireza2003");
printf("%s\n",json_object_get_string(profile));
FILE *output = fopen("output.csv","w");
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;
}

60816
players.csv Normal file

File diff suppressed because it is too large Load diff