neocities.h

a C library for interacting with Neocities' API
git clone https://github.com/tanguyandreani/neocities.h
Log | Files | Refs | README | LICENSE

commit f76f28422e428a8b4043f74981fc1540aa96c104
parent 8fcb275b3130da83ec5ca43b548f8d0cbc0bc1e8
Author: Tanguy Andreani <dev@tanguy.space>
Date:   Fri, 22 Feb 2019 09:29:34 +0100

Added: batch commit

- the case when domain can be null
- info, upload and list command line tools
- no more example.c

Diffstat:
MMakefile | 14++++++++++----
Ainfo.c | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alist.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
Mneocities.h | 30++++++++++++++++++++----------
Aupload.c | 37+++++++++++++++++++++++++++++++++++++
5 files changed, 190 insertions(+), 14 deletions(-)

diff --git a/Makefile b/Makefile @@ -2,10 +2,16 @@ CC=gcc # clang FLAGS=-lcurl -ljson-c -I/usr/include/json-c DEBUG=-g -DNEOCITIES_DEBUG -all: example +all: info list upload -example: example.c neocities.h dtparser.c dtparser.h +info: info.c neocities.h dtparser.c dtparser.h + $(CC) -o $(<:.c=) dtparser.c $< $(FLAGS) $(DEBUG) + +list: list.c neocities.h dtparser.c dtparser.h + $(CC) -o $(<:.c=) dtparser.c $< $(FLAGS) $(DEBUG) + +upload: upload.c neocities.h dtparser.c dtparser.h $(CC) -o $(<:.c=) dtparser.c $< $(FLAGS) $(DEBUG) clean: - rm -rf ./example- \ No newline at end of file + rm info list example+ \ No newline at end of file diff --git a/info.c b/info.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <curl/curl.h> +#include <json.h> + +#include "dtparser.h" + +#include "neocities.h" + +#define CURL_ERROR NEOCITIES_LLVL_ERR_CURL_GLOBAL_INIT +#define OK NEOCITIES_LLVL_OK + +int main(int argc, char *argv[]) +{ + neocities_res res; + + int i, err; + + char date_ca[33] = { 0 }; + char date_lu[33] = { 0 }; + + if (argc != 1 && argc != 2) { + fprintf(stderr, "Usage: %s [sitename]\n", argv[0]); + exit(200); // no enum member share this number + } + + if (curl_global_init(CURL_GLOBAL_SSL) != 0) + return CURL_ERROR; + + if ((err = + neocities_api_ex(APIKEY, INFO, ((argc == 1) ? "" : argv[1]), + &res)) != OK) + return err; + + if (res.data.info.sitename != NULL) + printf("sitename %s\n", res.data.info.sitename); + + if (res.data.info.views != -1) + printf("views %d\n", res.data.info.views); + + if (res.data.info.views != -1) + printf("hits %d\n", res.data.info.hits); + + if (res.data.info.created_at != 0) { + rfc5322_date_create(res.data.info.created_at, date_ca, 32); + printf("created_at %s\n", date_ca); + } + + if (res.data.info.last_updated != 0) { + rfc5322_date_create(res.data.info.last_updated, date_lu, 32); + printf("last_updated %s\n", date_lu); + } + + if (res.data.info.domain != NULL) { + printf("domain %s\n", res.data.info.domain); + } + + if (res.data.info.tags[0] != NULL) { + printf("tags ["); + i = 0; + while (res.data.info.tags[i + 1] != NULL) { + printf("\"%s\", ", res.data.info.tags[i]); + i++; + } + printf("\"%s\"]\n", res.data.info.tags[i]); + } + + neocities_destroy_info(&res); + + curl_global_cleanup(); + + return 0; +} diff --git a/list.c b/list.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <curl/curl.h> +#include <json.h> + +#include "dtparser.h" + +#include "neocities.h" + +#define CURL_ERROR NEOCITIES_LLVL_ERR_CURL_GLOBAL_INIT +#define OK NEOCITIES_LLVL_OK + +int main(int argc, char *argv[]) +{ + neocities_res res; + + int i, err; + + char date[33] = { 0 }; + + if (argc != 1 && argc != 2) { + fprintf(stderr, "Usage: %s [path] \n", argv[0]); + exit(200); // no enum member share this number + } + + if (curl_global_init(CURL_GLOBAL_SSL) != 0) + return CURL_ERROR; + + if ((err = + neocities_api_ex(APIKEY, LIST, (argc == 2) ? argv[1] : "", + &res)) != OK) + return err; + + for (i = 0; i < res.data.list.length; i++) { + printf("%s", res.data.list.files[i].path); + if (res.data.list.files[i].is_directory == 1) + putchar('/'); + putchar('\n'); + } + + neocities_destroy_list(&res); + + curl_global_cleanup(); + + return 0; +} diff --git a/neocities.h b/neocities.h @@ -342,14 +342,16 @@ void neocities_destroy_info(neocities_res * res) return; } -void neocities_destroy_error(neocities_res *res) { +void neocities_destroy_error(neocities_res * res) +{ res->data.error.type = -1; return; } enum neocities_low_level_error neocities_json_to_struct(json_object * jobj, - neocities_res * res) + neocities_res * + res) { json_object_iter jobj_iter, jobj_iter_info, jobj_iter_list; @@ -404,7 +406,8 @@ enum neocities_low_level_error neocities_json_to_struct(json_object * jobj, } - } else if (strcmp(jobj_iter.key, "info") == 0 + } else if (jobj_iter.key != NULL + && strcmp(jobj_iter.key, "info") == 0 && current_type == NEOCITIES_NO_TYPE_YET) { current_type = NEOCITIES_INFO_STRUCT; @@ -439,11 +442,15 @@ enum neocities_low_level_error neocities_json_to_struct(json_object * jobj, } else if (strcmp(jobj_iter_info.key, "domain") == 0) { - res->data.info.domain = - strdup(json_object_get_string(jobj_iter_info.val)); + tmp_string = + json_object_get_string(jobj_iter_info.val); - if (res->data.info.domain == NULL) - return NEOCITIES_LLVL_ERR_MALLOC_FAIL; + if (tmp_string != NULL) { + res->data.info.domain = strdup(tmp_string); + + if (res->data.info.domain == NULL) + return NEOCITIES_LLVL_ERR_MALLOC_FAIL; + } } else if (strcmp(jobj_iter_info.key, "created_at") == 0) { @@ -465,7 +472,9 @@ enum neocities_low_level_error neocities_json_to_struct(json_object * jobj, tmp_array = json_object_get_array(jobj_iter_info.val); - for (i = 0; i < tmp_array->length; i++) { + i = 0; + + for (; i < tmp_array->length; i++) { res->data.info.tags[i] = strdup(json_object_get_string @@ -559,7 +568,7 @@ enum neocities_low_level_error neocities_json_to_struct(json_object * jobj, break; } else { - if (res->result == 0) // UPLOAD + if (res->result == 0) // UPLOAD return NEOCITIES_LLVL_OK; @@ -581,7 +590,8 @@ enum neocities_low_level_error neocities_api_ex(const char *apikey, json_object *jobj = NULL; if ((err = - neocities_api(apikey, action, params, &jobj)) != NEOCITIES_LLVL_OK) + neocities_api(apikey, action, params, + &jobj)) != NEOCITIES_LLVL_OK) return err; err = neocities_json_to_struct(jobj, res); diff --git a/upload.c b/upload.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <curl/curl.h> +#include <json.h> + +#include "dtparser.h" + +#include "neocities.h" + +#define CURL_ERROR NEOCITIES_LLVL_ERR_CURL_GLOBAL_INIT +#define OK NEOCITIES_LLVL_OK + +int main(int argc, char *argv[]) +{ + neocities_res res; + + int i, err; + + char date[33] = { 0 }; + + if (argc != 2) { + fprintf(stderr, "Usage: %s filename\n", argv[0]); + exit(200); // no enum member share this number + } + + if (curl_global_init(CURL_GLOBAL_SSL) != 0) + return CURL_ERROR; + + if ((err = neocities_api_ex(APIKEY, UPLOAD, argv[1], &res)) != OK) + return err; + + curl_global_cleanup(); + + return 0; +}