commit 85c73cba2a3e8ef3227e8b51dd1f021830966625 Author: Jonas Date: Sat Dec 26 16:50:29 2020 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4d5cbd --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cWikiBot diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..7c582b2 --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +gcc -o ./cWikiBot main.c lichess.c http.c -lcurl -ljson-c diff --git a/clean.sh b/clean.sh new file mode 100644 index 0000000..d1c10d1 --- /dev/null +++ b/clean.sh @@ -0,0 +1 @@ +rm -r cWikiBot *.out diff --git a/http.c b/http.c new file mode 100644 index 0000000..6f8dba9 --- /dev/null +++ b/http.c @@ -0,0 +1,46 @@ +#include "http.h" + +#include +#include +#include + +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; +} diff --git a/http.h b/http.h new file mode 100644 index 0000000..59ea4f9 --- /dev/null +++ b/http.h @@ -0,0 +1,15 @@ +#ifndef WIKIBOT_HTTP_H +#define WIKIBOT_HTTP_H + +#include + +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 diff --git a/lichess.c b/lichess.c new file mode 100644 index 0000000..5129745 --- /dev/null +++ b/lichess.c @@ -0,0 +1,19 @@ +#include "lichess.h" + +#include +#include + +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; +} diff --git a/lichess.h b/lichess.h new file mode 100644 index 0000000..e9820b2 --- /dev/null +++ b/lichess.h @@ -0,0 +1,9 @@ +#ifndef WIKIBOT_LICHESS_H +#define WIKIBOT_LICHESS_H + +#include "http.h" +#include + +json_object* query_lichess(char* username); + +#endif // WIKIBOT_LICHESS_H diff --git a/main.c b/main.c new file mode 100644 index 0000000..efd93d8 --- /dev/null +++ b/main.c @@ -0,0 +1,39 @@ +/* + * main.c + * + * Copyright 2020 + * + * 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 +#include +#include +#include + +#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; +} diff --git a/main.h b/main.h new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/main.h @@ -0,0 +1 @@ +