add summaryFormat option

this should help scripts find new articles without having to parse
human-readable output
This commit is contained in:
dogeystamp 2023-02-06 20:13:38 -05:00
parent b199aacdfe
commit 25f01b702a
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
3 changed files with 96 additions and 46 deletions

View File

@ -1,5 +1,5 @@
PREFIX = ~/.local PREFIX = ~/.local
VERSION = 0.3 VERSION = 0.3.1
PKG_CONFIG = pkg-config PKG_CONFIG = pkg-config

View File

@ -63,3 +63,30 @@ enum outputFormats {
// When saving, sets the format of the saved file. // When saving, sets the format of the saved file.
static const enum outputFormats outputFormat = OUTPUT_HTML; static const enum outputFormats outputFormat = OUTPUT_HTML;
// Controls what is printed to stdout after each update.
enum summaryFormats {
/*
Prints the number of new articles for each feed.
Example:
feed1: 2 new articles
feed2: 2 new articles
*/
SUMMARY_HUMAN_READABLE,
/*
Prints relative paths of new articles.
Example:
feed1/article1.json
feed1/article2.json
feed2/something.json
feed2/otherthing.json
*/
SUMMARY_FILES,
};
static const enum summaryFormats summaryFormat = SUMMARY_HUMAN_READABLE;

View File

@ -104,7 +104,7 @@ FILE *
openFile(const char *folder, char *fileName, char *fileExt) openFile(const char *folder, char *fileName, char *fileExt)
{ {
// [folder]/[fileName][fileExt] // [folder]/[fileName][fileExt]
// caller's responsibility to sanitize names, but frees fileName // caller's responsibility to sanitize names and free fileName
if (!folder) { if (!folder) {
logMsg(LOG_ERROR, "NULL folder"); logMsg(LOG_ERROR, "NULL folder");
@ -143,7 +143,6 @@ openFile(const char *folder, char *fileName, char *fileExt)
FILE *itemFile = fopen(filePath, "a"); FILE *itemFile = fopen(filePath, "a");
free (filePath); free (filePath);
free (fileName);
return itemFile; return itemFile;
} }
@ -191,18 +190,12 @@ outputJson(itemStruct *item, FILE *f)
} }
#endif // JSON #endif // JSON
void int
itemAction(itemStruct *item, const char *folder) processItem(itemStruct *item, const char *folder)
{ {
// Receives a linked list of articles to process. // Returns 1 if the article is new, 0 if not, -1 for error.
itemStruct *cur = item; int ret = 0;
itemStruct *prev;
unsigned long long int newItems = 0;
while (cur) {
prev = cur;
char fileExt[10]; char fileExt[10];
void (*outputFunction)(itemStruct *, FILE *); void (*outputFunction)(itemStruct *, FILE *);
@ -221,36 +214,66 @@ itemAction(itemStruct *item, const char *folder)
default: default:
logMsg(LOG_FATAL, "Output format is invalid.\n"); logMsg(LOG_FATAL, "Output format is invalid.\n");
break; return -1;
} }
FILE *itemFile = openFile(folder, san(cur->fields[FIELD_TITLE]), fileExt); char *basename = san(item->fields[FIELD_TITLE]);
FILE *itemFile = openFile(folder, basename, fileExt);
if (!itemFile) { if (!itemFile) {
logMsg(LOG_ERROR, "Could not open file '%s/%s/%s'.\n", logMsg(LOG_ERROR, "Could not open file '%s/%s/%s'.\n",
folder, folder,
san(cur->fields[FIELD_TITLE]), basename,
fileExt fileExt
); );
cur = cur->next; ret = -1;
freeItem(prev); goto cleanup_basename;
continue;
} }
// Do not overwrite files // Do not overwrite files
if (!ftell(itemFile)) { if (!ftell(itemFile)) {
outputFunction(cur, itemFile); outputFunction(item, itemFile);
newItems++; ret = 1;
if (summaryFormat == SUMMARY_FILES)
logMsg(LOG_OUTPUT, "%s%c%s%s\n", folder, fsep(), basename, fileExt);
} }
fclose(itemFile); fclose(itemFile);
cleanup_basename:
free(basename);
return ret;
}
void
itemAction(itemStruct *item, const char *folder)
{
// Receives a linked list of articles to process.
itemStruct *cur = item;
itemStruct *prev;
unsigned long long int newItems = 0;
while (cur) {
prev = cur;
int res = processItem(cur, folder);
if (res == 1)
newItems++;
cur = cur->next; cur = cur->next;
freeItem(prev); freeItem(prev);
} }
switch (summaryFormat) {
case SUMMARY_HUMAN_READABLE:
if (newItems) if (newItems)
logMsg(LOG_OUTPUT, "%s : %d new articles\n", folder, newItems); logMsg(LOG_OUTPUT, "%s : %d new articles\n", folder, newItems);
break;
case SUMMARY_FILES:
// print output after saving each file
break;
}
} }
void void