add summaryFormat option
this should help scripts find new articles without having to parse human-readable output
This commit is contained in:
parent
b199aacdfe
commit
25f01b702a
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
PREFIX = ~/.local
|
||||
VERSION = 0.3
|
||||
VERSION = 0.3.1
|
||||
|
||||
PKG_CONFIG = pkg-config
|
||||
|
||||
|
27
config.def.h
27
config.def.h
@ -63,3 +63,30 @@ enum outputFormats {
|
||||
|
||||
// When saving, sets the format of the saved file.
|
||||
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;
|
||||
|
63
handlers.c
63
handlers.c
@ -104,7 +104,7 @@ FILE *
|
||||
openFile(const char *folder, char *fileName, char *fileExt)
|
||||
{
|
||||
// [folder]/[fileName][fileExt]
|
||||
// caller's responsibility to sanitize names, but frees fileName
|
||||
// caller's responsibility to sanitize names and free fileName
|
||||
|
||||
if (!folder) {
|
||||
logMsg(LOG_ERROR, "NULL folder");
|
||||
@ -143,7 +143,6 @@ openFile(const char *folder, char *fileName, char *fileExt)
|
||||
|
||||
FILE *itemFile = fopen(filePath, "a");
|
||||
free (filePath);
|
||||
free (fileName);
|
||||
|
||||
return itemFile;
|
||||
}
|
||||
@ -191,18 +190,12 @@ outputJson(itemStruct *item, FILE *f)
|
||||
}
|
||||
#endif // JSON
|
||||
|
||||
void
|
||||
itemAction(itemStruct *item, const char *folder)
|
||||
int
|
||||
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;
|
||||
itemStruct *prev;
|
||||
|
||||
unsigned long long int newItems = 0;
|
||||
|
||||
while (cur) {
|
||||
prev = cur;
|
||||
int ret = 0;
|
||||
|
||||
char fileExt[10];
|
||||
void (*outputFunction)(itemStruct *, FILE *);
|
||||
@ -221,36 +214,66 @@ itemAction(itemStruct *item, const char *folder)
|
||||
|
||||
default:
|
||||
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) {
|
||||
logMsg(LOG_ERROR, "Could not open file '%s/%s/%s'.\n",
|
||||
folder,
|
||||
san(cur->fields[FIELD_TITLE]),
|
||||
basename,
|
||||
fileExt
|
||||
);
|
||||
|
||||
cur = cur->next;
|
||||
freeItem(prev);
|
||||
continue;
|
||||
ret = -1;
|
||||
goto cleanup_basename;
|
||||
}
|
||||
|
||||
// Do not overwrite files
|
||||
if (!ftell(itemFile)) {
|
||||
outputFunction(cur, itemFile);
|
||||
newItems++;
|
||||
outputFunction(item, itemFile);
|
||||
ret = 1;
|
||||
if (summaryFormat == SUMMARY_FILES)
|
||||
logMsg(LOG_OUTPUT, "%s%c%s%s\n", folder, fsep(), basename, fileExt);
|
||||
}
|
||||
|
||||
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;
|
||||
freeItem(prev);
|
||||
}
|
||||
|
||||
switch (summaryFormat) {
|
||||
case SUMMARY_HUMAN_READABLE:
|
||||
if (newItems)
|
||||
logMsg(LOG_OUTPUT, "%s : %d new articles\n", folder, newItems);
|
||||
break;
|
||||
case SUMMARY_FILES:
|
||||
// print output after saving each file
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
Loading…
Reference in New Issue
Block a user