#include "lichess.h" #include #include #include #include #include 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]; int retries = 5; int user_url_length = sprintf(user_url,"%s%s",api_base,username); printf("querying %s\n",user_url); char* http_response = request(user_url); while(http_response == NULL) { printf("didn't receive HTTP response, we might be rate-limited. Waiting 5s.\n"); sleep(5); http_response = request(user_url); retries--; if(retries == 0 && http_response == NULL) return NULL; } json_object *lichessUser = json_tokener_parse(http_response); free(http_response); json_object *profile = json_object_object_get(lichessUser,"profile"); json_object_get(profile); json_object_put(lichessUser); return profile; }