Initial commit

This commit is contained in:
cpp 2020-12-26 16:50:29 +01:00
commit f65cede700
12 changed files with 61018 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
cWikiBot
output.csv

12
Dockerfile Normal file
View File

@ -0,0 +1,12 @@
FROM debian:stable
MAINTAINER Linuro <cpp@zom.bi>
RUN apt-get update && apt-get install build-essential gcc libjson-c-dev libcurl4-openssl-dev
ADD . /code
WORKDIR /code
RUN ./build.sh
CMD [cWikiBot]

1
build.sh Executable file
View File

@ -0,0 +1 @@
gcc -o ./cWikiBot main.c lichess.c http.c -lcurl -ljson-c

1
clean.sh Normal file
View File

@ -0,0 +1 @@
rm -r cWikiBot *.out

5
docker-compose.yml Normal file
View File

@ -0,0 +1,5 @@
version: ' 2.4'
services:
cWikiBot:
build: .

56
http.c Normal file
View File

@ -0,0 +1,56 @@
#include "http.h"
#include <string.h>
#include <curl/curl.h>
#include <stdlib.h>
int curl_setup = 0;
char* request(char *url)
{
// initialize curl on the first call of this function
if(curl_setup == 0)
{
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_WRITEDATA, (void *)&httpResponse);
return_code = curl_easy_perform(httpHandler);
}
else
{
fprintf(stderr, "CURL Error");
return NULL;
}
if(return_code != CURLE_OK)
{
fprintf(stderr, "HTTP Error: %s",curl_easy_strerror(return_code));
return NULL;
}
return httpResponse.response;
}
size_t httpResponseCallback(char *data, size_t wordlength, size_t bytecount, void *out)
{
// wordlength should be always 1, but this appears to be more secure.
size_t size = wordlength * bytecount;
struct httpResponse_t *mem = (struct httpResponse_t *) out;
char *newData = realloc(mem->response, mem->size + size +1);
if(newData == NULL)
return 0;
mem->response = newData;
memcpy(&(mem->response[mem->size]), data, size);
mem->size += size;
// Null-terminate the byte chunk, to effectively have a C-String.
mem->response[mem-> size] = 0;
return size;
}

15
http.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef WIKIBOT_HTTP_H
#define WIKIBOT_HTTP_H
#include <stddef.h>
struct httpResponse_t {
char *response;
size_t size;
};
size_t httpResponseCallback(char *data, size_t wordlength, size_t bytecount, void *out);
char* request(char *url );
#endif //WIKIBOT_HTTP_H

28
lichess.c Normal file
View File

@ -0,0 +1,28 @@
#include "lichess.h"
#include <string.h>
#include <json-c/json_tokener.h>
#include <stdio.h>
#include <unistd.h>
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];
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)
{
printf("didn't receive HTTP response, we might be rate-limited. Waiting 60s");
sleep(60);
return NULL;
}
json_object *lichessUser = json_tokener_parse(http_response);
json_object *profile = json_object_object_get(lichessUser,"profile");
return profile;
}

9
lichess.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef WIKIBOT_LICHESS_H
#define WIKIBOT_LICHESS_H
#include "http.h"
#include <json-c/json_object.h>
json_object* query_lichess(char* username);
#endif // WIKIBOT_LICHESS_H

72
main.c Normal file
View File

@ -0,0 +1,72 @@
/*
* main.c
*
* Copyright 2020 <cpp@zom.bi>
*
* 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
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <json-c/json_object.h>
#include "lichess.h"
#include "http.h"
int main(int argc, char **argv)
{
// json_object *profile = malloc(1);
FILE *output = fopen("output.csv","w");
FILE *playersfile = fopen("players.csv","r");
if(playersfile == NULL)
{
fputs("Couldn't open players.csv\n",stdout);
return 1;
}
fputs("lichess_id,name\n",output);
char line [128];
while(fgets(line, sizeof line, playersfile) != NULL)
{
char outline[256];
char name[128];
line[strlen(line)-1] = '\0';
json_object *profile = query_lichess(line);
if(profile == NULL)
{
printf("No profile for %s\n", line);
continue;
}
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);
continue;
}
strcpy(name,firstName);
strcat(name," ");
strcat(name,lastName);
sprintf(outline, "%s,%s\n",line,name);
printf("OK\n");
fputs(outline,output);
}
fclose(output);
fclose(playersfile);
return 0;
}

1
main.h Normal file
View File

@ -0,0 +1 @@

60816
players.csv Normal file

File diff suppressed because it is too large Load Diff