neocities.h

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

info.c (2131B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include <curl/curl.h>
      6 #include <json.h>
      7 
      8 #include "dtparser.h"
      9 
     10 #include "neocities.h"
     11 #include "print_error_msg.h"
     12 
     13 #include "apikey.h"
     14 
     15 #define CURL_ERROR NEOCITIES_LLVL_ERR_CURL_GLOBAL_INIT
     16 #define OK NEOCITIES_LLVL_OK
     17 
     18 int main(int argc, char *argv[])
     19 {
     20     neocities_res res;
     21 
     22     int i, err;
     23 
     24     char date_ca[33] = { 0 };
     25     char date_lu[33] = { 0 };
     26 
     27     if (argc != 1 && argc != 2) {
     28         fprintf(stderr, "Usage: %s [sitename]\n", argv[0]);
     29         exit(200);              // no enum member share this number
     30     }
     31 
     32     if ((err = neocities_global_init()) != 0)
     33         return err;
     34 
     35     if ((err =
     36          neocities_api_ex(APIKEY, info, ((argc == 1) ? "" : argv[1]),
     37                           &res)) != 0 || res.result == -1) {
     38         neocities_print_error_message(err);
     39         if (res.type == -1)     // reserved for errors
     40             neocities_print_error_message_api(res.data.error.type);
     41         else
     42             neocities_destroy(&res);
     43         return err;
     44     }
     45 
     46     if (res.data.info.sitename != NULL)
     47         printf("sitename     %s\n", res.data.info.sitename);
     48 
     49     if (res.data.info.views != -1)
     50         printf("views        %d\n", res.data.info.views);
     51 
     52     if (res.data.info.hits != -1)
     53         printf("hits         %d\n", res.data.info.hits);
     54 
     55     if (res.data.info.created_at != 0) {
     56         rfc5322_date_create(res.data.info.created_at, date_ca, 32);
     57         printf("created_at   %s\n", date_ca);
     58     }
     59 
     60     if (res.data.info.last_updated != 0) {
     61         rfc5322_date_create(res.data.info.last_updated, date_lu, 32);
     62         printf("last_updated %s\n", date_lu);
     63     }
     64 
     65     if (res.data.info.domain != NULL) {
     66         printf("domain       %s\n", res.data.info.domain);
     67     }
     68 
     69     if (res.data.info.tags[0] != NULL) {
     70         printf("tags         [");
     71         i = 0;
     72         while (res.data.info.tags[i + 1] != NULL) {
     73             printf("\"%s\", ", res.data.info.tags[i]);
     74             i++;
     75         }
     76         printf("\"%s\"]\n", res.data.info.tags[i]);
     77     }
     78 
     79     neocities_destroy(&res);
     80 
     81     neocities_global_cleanup();
     82 
     83     return 0;
     84 }