From 8bd06434e39ca71d5e5c216683a2fc0d1294a6e5 Mon Sep 17 00:00:00 2001 From: Jonas <cpp@zom.bi> Date: Sat, 26 Dec 2020 20:44:56 +0100 Subject: [PATCH 1/2] Set User Agent --- http.c | 1 + 1 file changed, 1 insertion(+) diff --git a/http.c b/http.c index bbfc027..ead8ee3 100644 --- a/http.c +++ b/http.c @@ -22,6 +22,7 @@ char* request(char *url) if(httpHandler) { curl_easy_setopt(httpHandler,CURLOPT_URL, url); curl_easy_setopt(httpHandler,CURLOPT_WRITEFUNCTION, httpResponseCallback); + curl_easy_setopt(httpHandler,CURLOPT_USERAGENT, "cWikiBot/0.1 (https://git.zom.bi/cpp/cWikiBot; cpp@zom.bi) libcurl4/7.64.0"); curl_easy_setopt(httpHandler,CURLOPT_WRITEDATA, (void *)&httpResponse); return_code = curl_easy_perform(httpHandler); } From 0189bcc0d0131f8f891b03a91a3ca9aa1046d2f9 Mon Sep 17 00:00:00 2001 From: Jonas <cpp@zom.bi> Date: Sat, 26 Dec 2020 20:49:36 +0100 Subject: [PATCH 2/2] Retry up to 5 times, when an error occurs. --- lichess.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lichess.c b/lichess.c index 8519bbc..dd023ec 100644 --- a/lichess.c +++ b/lichess.c @@ -12,15 +12,19 @@ json_object* query_lichess(char* username) */ char* api_base = "https://lichess.org/api/user/"; char user_url[128]; + int retries = 5; strcpy(user_url,api_base); strcat(user_url,username); printf("querying %s\n",user_url); char* http_response = request(user_url); - if(http_response == NULL) + while(http_response == NULL) { printf("didn't receive HTTP response, we might be rate-limited. Waiting 60s"); sleep(60); - return NULL; + http_response = request(user_url); + retries--; + if(retries == 0) + return NULL; } json_object *lichessUser = json_tokener_parse(http_response); json_object *profile = json_object_object_get(lichessUser,"profile");