Compare commits
No commits in common. "master" and "master" have entirely different histories.
3 changed files with 32 additions and 38 deletions
25
http.c
25
http.c
|
@ -4,37 +4,36 @@
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static CURL* httpHandler = NULL;
|
int curl_setup = 0;
|
||||||
|
|
||||||
char* request(char *url)
|
char* request(char *url)
|
||||||
{
|
{
|
||||||
// initialize curl on the first call of this function
|
// initialize curl on the first call of this function
|
||||||
if(!httpHandler)
|
if(curl_setup == 0)
|
||||||
{
|
{
|
||||||
httpHandler = curl_easy_init();
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||||
curl_easy_setopt(httpHandler,CURLOPT_USERAGENT, "cWikiBot/0.1 (https://git.zom.bi/cpp/cWikiBot; cpp@zom.bi) libcurl4/7.64.0");
|
curl_setup=1;
|
||||||
curl_easy_setopt(httpHandler,CURLOPT_WRITEFUNCTION, httpResponseCallback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct httpResponse_t httpResponse;
|
struct httpResponse_t httpResponse;
|
||||||
httpResponse.response = malloc(1);
|
httpResponse.response = malloc(1);
|
||||||
httpResponse.size = 0;
|
httpResponse.size = 0;
|
||||||
|
CURL *httpHandler = curl_easy_init();
|
||||||
CURLcode return_code;
|
CURLcode return_code;
|
||||||
if(httpHandler) {
|
if(httpHandler) {
|
||||||
curl_easy_setopt(httpHandler,CURLOPT_URL, url);
|
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);
|
curl_easy_setopt(httpHandler,CURLOPT_WRITEDATA, (void *)&httpResponse);
|
||||||
return_code = curl_easy_perform(httpHandler);
|
return_code = curl_easy_perform(httpHandler);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "CURL Error\n");
|
fprintf(stderr, "CURL Error");
|
||||||
free(httpResponse.response);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if(return_code != CURLE_OK)
|
if(return_code != CURLE_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "HTTP Error: %s (%d)\n",curl_easy_strerror(return_code), return_code);
|
fprintf(stderr, "HTTP Error: %s",curl_easy_strerror(return_code));
|
||||||
free(httpResponse.response);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return httpResponse.response;
|
return httpResponse.response;
|
||||||
|
@ -44,7 +43,7 @@ size_t httpResponseCallback(char *data, size_t wordlength, size_t bytecount, voi
|
||||||
// wordlength should be always 1, but this appears to be more secure.
|
// wordlength should be always 1, but this appears to be more secure.
|
||||||
size_t size = wordlength * bytecount;
|
size_t size = wordlength * bytecount;
|
||||||
struct httpResponse_t *mem = (struct httpResponse_t *) out;
|
struct httpResponse_t *mem = (struct httpResponse_t *) out;
|
||||||
|
|
||||||
char *newData = realloc(mem->response, mem->size + size +1);
|
char *newData = realloc(mem->response, mem->size + size +1);
|
||||||
if(newData == NULL)
|
if(newData == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -53,6 +52,6 @@ size_t httpResponseCallback(char *data, size_t wordlength, size_t bytecount, voi
|
||||||
mem->size += size;
|
mem->size += size;
|
||||||
// Null-terminate the byte chunk, to effectively have a C-String.
|
// Null-terminate the byte chunk, to effectively have a C-String.
|
||||||
mem->response[mem-> size] = 0;
|
mem->response[mem-> size] = 0;
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
13
lichess.c
13
lichess.c
|
@ -4,7 +4,6 @@
|
||||||
#include <json-c/json_tokener.h>
|
#include <json-c/json_tokener.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
json_object* query_lichess(char* username)
|
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* api_base = "https://lichess.org/api/user/";
|
||||||
char user_url[128];
|
char user_url[128];
|
||||||
int retries = 5;
|
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);
|
printf("querying %s\n",user_url);
|
||||||
char* http_response = request(user_url);
|
char* http_response = request(user_url);
|
||||||
while(http_response == NULL)
|
while(http_response == NULL)
|
||||||
{
|
{
|
||||||
printf("didn't receive HTTP response, we might be rate-limited. Waiting 5s.\n");
|
printf("didn't receive HTTP response, we might be rate-limited. Waiting 60s");
|
||||||
sleep(5);
|
sleep(60);
|
||||||
http_response = request(user_url);
|
http_response = request(user_url);
|
||||||
retries--;
|
retries--;
|
||||||
if(retries == 0 && http_response == NULL)
|
if(retries == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
json_object *lichessUser = json_tokener_parse(http_response);
|
json_object *lichessUser = json_tokener_parse(http_response);
|
||||||
free(http_response);
|
|
||||||
json_object *profile = json_object_object_get(lichessUser,"profile");
|
json_object *profile = json_object_object_get(lichessUser,"profile");
|
||||||
json_object_get(profile);
|
|
||||||
json_object_put(lichessUser);
|
|
||||||
return profile;
|
return profile;
|
||||||
}
|
}
|
||||||
|
|
32
main.c
32
main.c
|
@ -1,29 +1,28 @@
|
||||||
/*
|
/*
|
||||||
* main.c
|
* main.c
|
||||||
*
|
*
|
||||||
* Copyright 2020 <cpp@zom.bi>
|
* Copyright 2020 <cpp@zom.bi>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation; either version 3 of the License, or
|
* the Free Software Foundation; either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program; if not, write to the Free Software
|
* along with this program; if not, write to the Free Software
|
||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
* MA 02110-1301, USA.
|
* MA 02110-1301, USA.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <json-c/json_object.h>
|
#include <json-c/json_object.h>
|
||||||
|
|
||||||
|
@ -42,12 +41,10 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
fputs("lichess_id,name\n",output);
|
fputs("lichess_id,name\n",output);
|
||||||
char line [128];
|
char line [128];
|
||||||
int j=0;
|
while(fgets(line, sizeof line, playersfile) != NULL)
|
||||||
while(fgets(line, 128, playersfile) != NULL)
|
|
||||||
{
|
{
|
||||||
printf("%i: ",j);
|
|
||||||
j++;
|
|
||||||
char outline[256];
|
char outline[256];
|
||||||
|
char name[128];
|
||||||
line[strlen(line)-1] = '\0';
|
line[strlen(line)-1] = '\0';
|
||||||
json_object *profile = query_lichess(line);
|
json_object *profile = query_lichess(line);
|
||||||
if(profile == NULL)
|
if(profile == NULL)
|
||||||
|
@ -55,18 +52,19 @@ int main(int argc, char **argv)
|
||||||
printf("No profile for %s\n", line);
|
printf("No profile for %s\n", line);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const char *firstName = json_object_get_string(json_object_object_get(profile,"firstName"));
|
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 *lastName = json_object_get_string(json_object_object_get(profile,"lastName"));
|
||||||
if(firstName == NULL || lastName == NULL)
|
if(firstName == NULL || lastName == NULL)
|
||||||
{
|
{
|
||||||
printf("No name for %s\n",line);
|
printf("No name for %s\n",line);
|
||||||
json_object_put(profile);
|
|
||||||
continue;
|
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");
|
printf("OK\n");
|
||||||
fputs(outline,output);
|
fputs(outline,output);
|
||||||
json_object_put(profile);
|
|
||||||
}
|
}
|
||||||
fclose(output);
|
fclose(output);
|
||||||
fclose(playersfile);
|
fclose(playersfile);
|
||||||
|
|
Loading…
Reference in a new issue