2020-12-26 16:50:29 +01:00
|
|
|
#include "lichess.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <json-c/json_tokener.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.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];
|
2020-12-26 20:49:36 +01:00
|
|
|
int retries = 5;
|
2020-12-26 23:09:38 +01:00
|
|
|
//strcpy(user_url,api_base);
|
|
|
|
//strcat(user_url,username);
|
|
|
|
int user_url_length = sprintf(user_url,"%s%s\0",api_base,username);
|
|
|
|
user_url[user_url_length]=0;
|
2020-12-26 16:50:29 +01:00
|
|
|
printf("querying %s\n",user_url);
|
|
|
|
char* http_response = request(user_url);
|
2020-12-26 20:49:36 +01:00
|
|
|
while(http_response == NULL)
|
2020-12-26 16:50:29 +01:00
|
|
|
{
|
|
|
|
printf("didn't receive HTTP response, we might be rate-limited. Waiting 60s");
|
|
|
|
sleep(60);
|
2020-12-26 20:49:36 +01:00
|
|
|
http_response = request(user_url);
|
|
|
|
retries--;
|
|
|
|
if(retries == 0)
|
|
|
|
return NULL;
|
2020-12-26 16:50:29 +01:00
|
|
|
}
|
|
|
|
json_object *lichessUser = json_tokener_parse(http_response);
|
|
|
|
json_object *profile = json_object_object_get(lichessUser,"profile");
|
|
|
|
return profile;
|
|
|
|
}
|