minrss/minrss.c

142 lines
3.3 KiB
C
Raw Normal View History

/*
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
© 2021 dogeystamp <dogeystamp@disroot.org>
*/
2021-08-02 11:41:15 -04:00
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "config.h"
#include "util.h"
#include "net.h"
#include "xml.h"
void
itemAction(itemStruct *item, const char *folder)
{
2021-09-22 18:50:42 -04:00
// Receives a link list of articles to process.
2021-08-02 11:41:15 -04:00
itemStruct *cur = item;
unsigned long long int newItems = 0;
2022-05-23 11:13:13 -04:00
size_t folderLen = strlen(folder);
size_t extLen = strlen(fileExt);
2021-08-02 11:41:15 -04:00
while (cur) {
char *filePath;
char *fileName = san(cur->title, 1);
2022-05-23 11:13:13 -04:00
size_t fileNameLen = strlen(fileName);
2021-08-02 11:41:15 -04:00
itemStruct *prev = cur;
2022-05-23 11:13:13 -04:00
// +1 for null terminator and +1 for path separator
size_t pathLen = folderLen + fileNameLen + extLen + 2;
2021-08-02 11:41:15 -04:00
if (fileName[0])
2022-05-23 11:13:13 -04:00
filePath = ecalloc(pathLen, sizeof(char));
2021-08-02 11:41:15 -04:00
else {
logMsg(1, "Invalid article title.\n");
cur = cur->next;
freeItem(prev);
continue;
}
2022-05-23 11:13:13 -04:00
memcpy(filePath, folder, folderLen * sizeof(char));
2021-08-02 11:41:15 -04:00
2022-05-23 11:13:13 -04:00
filePath[folderLen] = fsep();
2022-05-23 12:55:56 -04:00
filePath[pathLen - 1] = '\0';
2021-08-02 11:41:15 -04:00
2022-05-23 11:13:13 -04:00
memcpy(filePath + folderLen + 1, fileName, fileNameLen * sizeof(char));
memcpy(filePath + pathLen - extLen - 1, fileExt, extLen * sizeof(char));
2021-08-11 17:22:07 -04:00
2021-08-02 11:41:15 -04:00
FILE *itemFile = fopen(filePath, "a");
2022-05-23 12:55:56 -04:00
2021-08-02 11:41:15 -04:00
free(filePath);
2022-05-23 12:55:56 -04:00
free(fileName);
2021-08-02 11:41:15 -04:00
// Do not overwrite files
if (!ftell(itemFile)) {
newItems++;
2021-08-11 17:22:07 -04:00
fprintf(itemFile, "<h1>%s</h1><br>\n", cur->title);
fprintf(itemFile, "<a href=\"%s\">Link</a><br>\n", san(cur->link, 0));
fprintf(itemFile, "%s", san(cur->description, 0));
2021-08-02 11:41:15 -04:00
}
fclose(itemFile);
cur = cur->next;
freeItem(prev);
}
if (newItems)
2022-05-23 12:27:00 -04:00
logMsg(2, "%s : %d new articles\n", folder, newItems);
2021-08-02 11:41:15 -04:00
}
void
finish(char *url, long responseCode)
{
2021-09-22 18:50:42 -04:00
// Executed after a download finishes
2021-08-02 11:41:15 -04:00
if (responseCode == 200)
logMsg(4, "Finished downloading %s\n", url);
else if (!responseCode)
2022-05-23 12:04:36 -04:00
logMsg(1, "Can not reach %s: ensure the protocol is enabled and the site is accessible.\n", url);
2021-08-02 11:41:15 -04:00
else
logMsg(1, "HTTP %ld for %s\n", responseCode, url);
}
int
2022-05-23 12:27:00 -04:00
main(int argc, char *argv[])
2021-08-02 11:41:15 -04:00
{
2021-08-02 13:55:46 -04:00
if (argc == 2 && !strcmp("-v", argv[1]))
logMsg(0, "MinRSS %s\n", VERSION);
else if (argc != 1)
logMsg(0, "Usage: minrss [-v]\n");
2021-08-06 14:32:04 -04:00
unsigned int i = 0;
2021-08-02 11:41:15 -04:00
2021-08-06 14:32:04 -04:00
initCurl();
2021-08-02 11:41:15 -04:00
2021-08-06 14:32:04 -04:00
outputStruct outputs[LEN(links)];
memset(outputs, 0, sizeof(outputs));
2021-08-02 11:41:15 -04:00
2021-08-06 14:32:04 -04:00
for (i = 0; i < LEN(links); i++) {
2021-08-02 11:41:15 -04:00
if (links[0].url[0] == '\0')
logMsg(0, "No feeds, add them in config.def.h\n");
2021-08-06 14:32:04 -04:00
logMsg(4, "Requesting %s\n", links[i].url);
createRequest(links[i].url, &outputs[i]);
}
2021-08-02 11:41:15 -04:00
2021-08-06 14:32:04 -04:00
performRequests(finish);
2021-08-02 11:41:15 -04:00
2021-08-06 14:32:04 -04:00
logMsg(3, "Finished downloads.\n");
2021-08-02 11:41:15 -04:00
2021-08-06 14:32:04 -04:00
for (i = 0; i < LEN(links); i++) {
2022-05-23 12:27:00 -04:00
logMsg(5, "Parsing %s\n", links[i].url);
2021-08-02 11:41:15 -04:00
if (outputs[i].buffer && outputs[i].buffer[0]) {
readDoc(outputs[i].buffer, links[i].feedName, itemAction);
free(outputs[i].buffer);
}
2021-08-06 14:32:04 -04:00
}
2021-08-02 11:41:15 -04:00
2021-08-06 14:32:04 -04:00
logMsg(3, "Finished parsing feeds.\n");
2021-08-02 11:41:15 -04:00
2021-08-06 14:32:04 -04:00
return 0;
2021-08-02 11:41:15 -04:00
}