sfeed_jsonfeed.c - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       sfeed_jsonfeed.c (3724B)
       ---
            1 #include <stdio.h>
            2 #include <stdlib.h>
            3 #include <string.h>
            4 #include <unistd.h>
            5 
            6 #include "util.h"
            7 
            8 static char *line;
            9 static size_t linesize;
           10 static int firstitem = 1;
           11 
           12 /* Unescape / decode fields printed by string_print_encoded() */
           13 static void
           14 printcontent(const char *s)
           15 {
           16         for (; *s; s++) {
           17                 switch (*s) {
           18                 case '\\':
           19                         s++;
           20                         switch (*s) {
           21                         case 'n':  fputs("\\n",  stdout); break;
           22                         case '\\': fputs("\\\\", stdout); break;
           23                         case 't':  fputs("\\t",  stdout); break;
           24                         }
           25                         break; /* ignore invalid escape sequence */
           26                 case '"':  fputs("\\\"", stdout); break;
           27                 default:
           28                         putchar(*s);
           29                         break;
           30                 }
           31         }
           32 }
           33 
           34 static void
           35 printfield(const char *s)
           36 {
           37         for (; *s; s++) {
           38                 if (*s == '\\')
           39                         fputs("\\\\", stdout);
           40                 else if (*s == '"')
           41                         fputs("\\\"", stdout);
           42                 else
           43                         putchar(*s);
           44         }
           45 }
           46 
           47 static void
           48 printfeed(FILE *fp, const char *feedname)
           49 {
           50         char *fields[FieldLast], timebuf[32];
           51         struct tm parsedtm, *tm;
           52         time_t parsedtime;
           53         ssize_t linelen;
           54         int ch;
           55         char *p, *s;
           56 
           57         while ((linelen = getline(&line, &linesize, fp)) > 0 &&
           58                !ferror(stdout)) {
           59                 if (line[linelen - 1] == '\n')
           60                         line[--linelen] = '\0';
           61                 parseline(line, fields);
           62 
           63                 if (!firstitem)
           64                         fputs(",\n", stdout);
           65                 firstitem = 0;
           66 
           67                 fputs("{\n\t\"id\": \"", stdout);
           68                 printfield(fields[FieldId]);
           69                 fputs("\"", stdout);
           70 
           71                 parsedtime = 0;
           72                 if (!strtotime(fields[FieldUnixTimestamp], &parsedtime) &&
           73                     (tm = gmtime_r(&parsedtime, &parsedtm)) &&
           74                     strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ", tm)) {
           75                         fputs(",\n\t\"date_published\": \"", stdout);
           76                             fputs(timebuf, stdout);
           77                         fputs("\"", stdout);
           78                 }
           79 
           80                 fputs(",\n\t\"title\": \"", stdout);
           81                 if (feedname[0]) {
           82                         fputs("[", stdout);
           83                         printfield(feedname);
           84                         fputs("] ", stdout);
           85                 }
           86                 printfield(fields[FieldTitle]);
           87                 fputs("\"", stdout);
           88 
           89                 if (fields[FieldLink][0]) {
           90                         fputs(",\n\t\"url\": \"", stdout);
           91                         printfield(fields[FieldLink]);
           92                         fputs("\"", stdout);
           93                 }
           94 
           95                 if (fields[FieldAuthor][0]) {
           96                         fputs(",\n\t\"authors\": [{\"name\": \"", stdout);
           97                         printfield(fields[FieldAuthor]);
           98                         fputs("\"}]", stdout);
           99                 }
          100 
          101                 if (fields[FieldCategory][0]) {
          102                         fputs(",\n\t\"tags\": [", stdout);
          103 
          104                         for (p = s = fields[FieldCategory]; ; s++) {
          105                                 if (*s == '|' || *s == '\0') {
          106                                         if (p != fields[FieldCategory])
          107                                                 fputs(", ", stdout);
          108                                         ch = *s;
          109                                         *s = '\0'; /* temporary NUL terminate */
          110                                         fputs("\"", stdout);
          111                                         printfield(p);
          112                                         fputs("\"", stdout);
          113                                         *s = ch; /* restore */
          114                                         p = s + 1;
          115                                 }
          116                                 if (*s == '\0')
          117                                         break;
          118                         }
          119                         fputs("]", stdout);
          120                 }
          121 
          122                 if (fields[FieldEnclosure][0]) {
          123                         fputs(",\n\t\"attachments\": [{\"url:\": \"", stdout);
          124                         printfield(fields[FieldEnclosure]);
          125                         fputs("\"}]", stdout);
          126                 }
          127 
          128                 if (!strcmp(fields[FieldContentType], "html"))
          129                         fputs(",\n\t\"content_html\": \"", stdout);
          130                 else
          131                         fputs(",\n\t\"content_text\": \"", stdout);
          132                 printcontent(fields[FieldContent]);
          133                 fputs("\"\n}", stdout);
          134         }
          135 }
          136 
          137 int
          138 main(int argc, char *argv[])
          139 {
          140         FILE *fp;
          141         char *name;
          142         int i;
          143 
          144         if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1)
          145                 err(1, "pledge");
          146 
          147         fputs("{\n"
          148               "\"version\": \"https://jsonfeed.org/version/1.1\",\n"
          149               "\"title\": \"sfeed to jsonfeed 1.1\",\n"
          150               "\"items\": [\n", stdout);
          151 
          152         if (argc == 1) {
          153                 printfeed(stdin, "");
          154                 checkfileerror(stdin, "<stdin>", 'r');
          155         } else {
          156                 for (i = 1; i < argc; i++) {
          157                         if (!(fp = fopen(argv[i], "r")))
          158                                 err(1, "fopen: %s", argv[i]);
          159                         name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
          160                         printfeed(fp, name);
          161                         checkfileerror(fp, argv[i], 'r');
          162                         checkfileerror(stdout, "<stdout>", 'w');
          163                         fclose(fp);
          164                 }
          165         }
          166         fputs("]\n}\n", stdout);
          167 
          168         checkfileerror(stdout, "<stdout>", 'w');
          169 
          170         return 0;
          171 }