Initial commit

This commit is contained in:
cpp 2020-12-26 16:50:29 +01:00
commit 85c73cba2a
9 changed files with 132 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

46
http.c Normal file
View file

@ -0,0 +1,46 @@
#include "http.h"
#include <string.h>
#include <curl/curl.h>
#include <stdlib.h>
int curl_setup = 0;
char* request(char *url )
{
if(!curl_setup)
{
curl_global_init(CURL_GLOBAL_DEFAULT);
url +=1;
}
struct httpResponse_t httpResponse;
httpResponse.response = malloc(1);
httpResponse.size = 0;
CURL *httpHandler = curl_easy_init();
if(httpHandler) {
curl_easy_setopt(httpHandler,CURLOPT_URL, url);
curl_easy_setopt(httpHandler,CURLOPT_WRITEFUNCTION, httpResponseCallback);
curl_easy_setopt(httpHandler,CURLOPT_WRITEDATA, (void *)&httpResponse);
curl_easy_perform(httpHandler);
}
else
return "Lol, fehler";
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

19
lichess.c Normal file
View file

@ -0,0 +1,19 @@
#include "lichess.h"
#include <string.h>
#include <json-c/json_tokener.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);
char* http_response = request(strcat(
user_url,username));
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

39
main.c Normal file
View file

@ -0,0 +1,39 @@
/*
* 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);
json_object *profile = query_lichess("alireza2003");
printf("%s\n",json_object_get_string(profile));
return 0;
}

1
main.h Normal file
View file

@ -0,0 +1 @@