Compare commits

..

No commits in common. "2e85e022ab34f32c6a5c612996b8eadb709ba59e" and "0189bcc0d0131f8f891b03a91a3ca9aa1046d2f9" have entirely different histories.

3 changed files with 32 additions and 38 deletions

19
http.c
View file

@ -4,37 +4,36 @@
#include <curl/curl.h>
#include <stdlib.h>
static CURL* httpHandler = NULL;
int curl_setup = 0;
char* request(char *url)
{
// initialize curl on the first call of this function
if(!httpHandler)
if(curl_setup == 0)
{
httpHandler = curl_easy_init();
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_WRITEFUNCTION, httpResponseCallback);
curl_global_init(CURL_GLOBAL_DEFAULT);
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_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);
}
else
{
fprintf(stderr, "CURL Error\n");
free(httpResponse.response);
fprintf(stderr, "CURL Error");
return NULL;
}
if(return_code != CURLE_OK)
{
fprintf(stderr, "HTTP Error: %s (%d)\n",curl_easy_strerror(return_code), return_code);
free(httpResponse.response);
fprintf(stderr, "HTTP Error: %s",curl_easy_strerror(return_code));
return NULL;
}
return httpResponse.response;

View file

@ -4,7 +4,6 @@
#include <json-c/json_tokener.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
json_object* query_lichess(char* username)
{
@ -14,22 +13,20 @@ json_object* query_lichess(char* username)
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);
strcpy(user_url,api_base);
strcat(user_url,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);
printf("didn't receive HTTP response, we might be rate-limited. Waiting 60s");
sleep(60);
http_response = request(user_url);
retries--;
if(retries == 0 && http_response == NULL)
if(retries == 0)
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;
}

18
main.c
View file

@ -23,7 +23,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <json-c/json_object.h>
@ -42,12 +41,10 @@ int main(int argc, char **argv)
}
fputs("lichess_id,name\n",output);
char line [128];
int j=0;
while(fgets(line, 128, playersfile) != NULL)
while(fgets(line, sizeof line, playersfile) != NULL)
{
printf("%i: ",j);
j++;
char outline[256];
char name[128];
line[strlen(line)-1] = '\0';
json_object *profile = query_lichess(line);
if(profile == NULL)
@ -55,18 +52,19 @@ int main(int argc, char **argv)
printf("No profile for %s\n", line);
continue;
}
const char *firstName = json_object_get_string(json_object_object_get(profile,"firstName"));
const char *lastName = json_object_get_string(json_object_object_get(profile,"lastName"));
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\n",line);
json_object_put(profile);
continue;
}
sprintf(outline, "%s,%s %s\n",line,firstName,lastName);
strcpy(name,firstName);
strcat(name," ");
strcat(name,lastName);
sprintf(outline, "%s,%s\n",line,name);
printf("OK\n");
fputs(outline,output);
json_object_put(profile);
}
fclose(output);
fclose(playersfile);