cWikiBot/lichess.c

36 lines
1 KiB
C
Raw Normal View History

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