From 6446070da557bf8b56fa44b2bbdc4690edf490a9 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Wed, 22 Aug 2018 16:23:52 +0200 Subject: add sfeed_twtxt format program: format to a twtxt feed --- Makefile | 1 + README | 3 ++- sfeed_twtxt.1 | 30 +++++++++++++++++++++++ sfeed_twtxt.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 sfeed_twtxt.1 create mode 100644 sfeed_twtxt.c diff --git a/Makefile b/Makefile index 5c72001..eed1299 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ BIN = \ sfeed_opml_import\ sfeed_plain\ sfeed_tail\ + sfeed_twtxt\ sfeed_web\ sfeed_xmlenc SCRIPTS = \ diff --git a/README b/README index afe01d4..5d3df96 100644 --- a/README +++ b/README @@ -111,7 +111,8 @@ sfeed_opml_export - Generate an OPML XML file from a sfeedrc config file. sfeed_opml_import - Generate a sfeedrc config file from an OPML XML file. sfeed_mbox - Format feed data (TSV) to mbox. sfeed_plain - Format feed data (TSV) to a plain-text list. -sfeed_tail - format unseen feed data (TSV) to a plain-text list. +sfeed_tail - Format unseen feed data (TSV) to a plain-text list. +sfeed_twtxt - Format feed data (TSV) to a twtxt feed. sfeed_update - Update feeds and merge with old feeds in the directory $HOME/.sfeed/feeds by default. sfeed_web - Find urls to RSS/Atom feed from a webpage. diff --git a/sfeed_twtxt.1 b/sfeed_twtxt.1 new file mode 100644 index 0000000..14da7f2 --- /dev/null +++ b/sfeed_twtxt.1 @@ -0,0 +1,30 @@ +.Dd August 22, 2018 +.Dt SFEED_TWTXT 1 +.Os +.Sh NAME +.Nm sfeed_twtxt +.Nd format feed data to a twtxt feed +.Sh SYNOPSIS +.Nm +.Op Ar file... +.Sh DESCRIPTION +.Nm +formats feed data (TSV) from +.Xr sfeed 1 +from stdin or +.Ar file +to stdout as a twtxt feed. +If one or more +.Ar file +are specified, the basename of the +.Ar file +is used as the feed name in the output. +If no +.Ar file +parameters are specified and so the data is read from stdin the feed name +is empty. +.Sh SEE ALSO +.Xr sfeed 1 , +.Xr sfeed_plain 1 +.Sh AUTHORS +.An Hiltjo Posthuma Aq Mt hiltjo@codemadness.org diff --git a/sfeed_twtxt.c b/sfeed_twtxt.c new file mode 100644 index 0000000..5df0fa1 --- /dev/null +++ b/sfeed_twtxt.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include + +#include "util.h" + +static char *line; +static size_t linesize; + +static void +printfeed(FILE *fp, const char *feedname) +{ + char *fields[FieldLast]; + struct tm *tm; + time_t parsedtime; + ssize_t linelen; + + while ((linelen = getline(&line, &linesize, fp)) > 0) { + if (line[linelen - 1] == '\n') + line[--linelen] = '\0'; + if (!parseline(line, fields)) + break; + + parsedtime = 0; + if (strtotime(fields[FieldUnixTimestamp], &parsedtime)) + continue; + if (!(tm = gmtime(&parsedtime))) + err(1, "localtime"); + + fprintf(stdout, "%04d-%02d-%02dT%02d:%02d:%02dZ\t", + tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + if (feedname[0]) + printf("[%s] ", feedname); + fputs(fields[FieldTitle], stdout); + if (fields[FieldLink][0]) { + fputs(" @<", stdout); + if (fields[FieldAuthor][0]) { + fputs(fields[FieldAuthor], stdout); + fputs(" ", stdout); + } + fputs(fields[FieldLink], stdout); + fputs(">", stdout); + } + putchar('\n'); + } +} + +int +main(int argc, char *argv[]) +{ + FILE *fp; + char *name; + int i; + + if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1) + err(1, "pledge"); + + if (argc == 1) { + printfeed(stdin, ""); + } else { + for (i = 1; i < argc; i++) { + if (!(fp = fopen(argv[i], "r"))) + err(1, "fopen: %s", argv[i]); + name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i]; + printfeed(fp, name); + if (ferror(fp)) + err(1, "ferror: %s", argv[i]); + fclose(fp); + } + } + return 0; +} -- cgit v1.2.3