Initial commit

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

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
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

57
http.c Normal file
View file

@ -0,0 +1,57 @@
#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;
}
printf("%i\n",httpResponse.size);
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

22
lichess.c Normal file
View file

@ -0,0 +1,22 @@
#include "lichess.h"
#include <string.h>
#include <json-c/json_tokener.h>
#include <stdio.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("%s",user_url);
char* http_response = request(user_url);
printf("\n%p\n",http_response);
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

64
main.c Normal file
View file

@ -0,0 +1,64 @@
/*
* 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];
json_object *profile = query_lichess(line);
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",line);
printf("object is: %s",json_object_get_string(profile));
continue;
}
strcpy(name,firstName);
strcat(name," ");
strcat(name,lastName);
sprintf(outline, "%s,%s\n",line,name);
fputs(outline,output);
}
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