Add --update option

This commit is contained in:
Jaidyn Ann 2020-08-14 01:59:15 -05:00
parent 5cc00b8df3
commit 956d7786af
6 changed files with 51 additions and 24 deletions

View File

@ -97,7 +97,7 @@ Both -t and -T use the ISO 8601 format for specifying datetimes:
NOTE: This message doesn't reflect reality. This is more of a spec of NOTE: This message doesn't reflect reality. This is more of a spec of
what I hope this program will be. As of now, running Pogger what I hope this program will be. As of now, running Pogger
without a file/url free-argument is invalid, as the daemon without a file/url free-argument is invalid, as the daemon
isn't implemented at all. As such, -D -u and -C are non-functional. isn't implemented at all. As such, -D and -C are non-functional.
But it sure can turn an XML feed into files! Lol. But it sure can turn an XML feed into files! Lol.

View File

@ -19,18 +19,26 @@ AtomFeed::AtomFeed ( BString path, Config* cfg )
void void
AtomFeed::Parse ( Config* cfg ) AtomFeed::Parse ( Config* cfg )
{ {
BFile* feedFile = new BFile( filePath.String(), B_READ_ONLY );
entries = BList(); entries = BList();
tinyxml2::XMLDocument xml; tinyxml2::XMLDocument xml;
xml.LoadFile( filePath.String() ); xml.LoadFile( filePath.String() );
time_t tt_lastDate = 0;
BDateTime attrLastDate = BDateTime();
feedFile->ReadAttr( "LastDate", B_TIME_TYPE, 0, &tt_lastDate, sizeof(time_t) );
if ( tt_lastDate > 0 && cfg->minDate == NULL && cfg->updateFeeds == true ) {
attrLastDate.SetTime_t( tt_lastDate );
cfg->minDate = attrLastDate;
}
tinyxml2::XMLElement* xfeed = xml.FirstChildElement("feed"); tinyxml2::XMLElement* xfeed = xml.FirstChildElement("feed");
RootParse( cfg, xfeed ); RootParse( cfg, xfeed );
ParseEntries( cfg, xfeed ); ParseEntries( cfg, xfeed );
time_t tt_lastDate = lastDate.Time_t(); tt_lastDate = lastDate.Time_t();
BFile* cacheFile = new BFile( filePath, B_READ_WRITE ); feedFile->WriteAttr( "LastDate", B_TIME_TYPE, 0, &tt_lastDate, sizeof(time_t) );
cacheFile->WriteAttr( "LastDate", B_TIME_TYPE, 0, &tt_lastDate, sizeof(time_t) );
} }
void void
@ -80,7 +88,7 @@ AtomFeed::EntryParse ( Config* cfg, tinyxml2::XMLElement* xentry )
set = newEntry->SetDate( xentry->FirstChildElement("updated") ); set = newEntry->SetDate( xentry->FirstChildElement("updated") );
if ( !set ) set = newEntry->SetDate( xentry->FirstChildElement("published") ); if ( !set ) set = newEntry->SetDate( xentry->FirstChildElement("published") );
if ( lastDate != NULL || lastDate < newEntry->date ) if ( lastDate == NULL || lastDate < newEntry->date )
lastDate = newEntry->date; lastDate = newEntry->date;
if ( xcontent ) { if ( xcontent ) {
@ -88,11 +96,7 @@ AtomFeed::EntryParse ( Config* cfg, tinyxml2::XMLElement* xentry )
newEntry->SetContent( xprinter.CStr() ); newEntry->SetContent( xprinter.CStr() );
} }
if ( cfg->verbose ) AddEntry( cfg, newEntry );
printf("\t%s\n", newEntry->title.String());
if ( withinDateRange( cfg->minDate, newEntry->date, cfg->maxDate ) )
entries.AddItem( newEntry );
} }
void void

View File

@ -42,6 +42,8 @@ BString
Feed::FetchRemoteFeed ( BString givenPath, Config* cfg ) Feed::FetchRemoteFeed ( BString givenPath, Config* cfg )
{ {
BUrl givenUrl = BUrl( givenPath ); BUrl givenUrl = BUrl( givenPath );
time_t tt_lastDate = 0;
BDateTime* lastDate = new BDateTime();
BString* newHash = new BString(); BString* newHash = new BString();
char oldHash[41]; char oldHash[41];
@ -53,8 +55,7 @@ Feed::FetchRemoteFeed ( BString givenPath, Config* cfg )
filename.Append(splitName); filename.Append(splitName);
BFile* cacheFile = new BFile( filename, B_READ_WRITE | B_CREATE_FILE ); BFile* cacheFile = new BFile( filename, B_READ_WRITE | B_CREATE_FILE );
cacheFile->ReadAttr( "LastHash", B_STRING_TYPE, 0, cacheFile->ReadAttr( "LastHash", B_STRING_TYPE, 0, oldHash, 41 );
oldHash, 41 );
if ( cfg->verbose ) if ( cfg->verbose )
printf( "Saving %s...\n", givenPath.String() ); printf( "Saving %s...\n", givenPath.String() );
@ -109,6 +110,18 @@ Feed::xmlCountSiblings ( tinyxml2::XMLElement* xsibling, const char* sibling_nam
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool
Feed::AddEntry ( Config* cfg, Entry* newEntry )
{
if ( !withinDateRange( cfg->minDate, newEntry->date, cfg->maxDate ) )
return false;
if ( cfg->verbose == true )
printf( "\t%s\n", newEntry->title.String() );
entries.AddItem( newEntry );
return true;
}
bool Feed::SetTitle ( const char* titleStr ) { bool Feed::SetTitle ( const char* titleStr ) {
if ( titleStr != NULL ) title = BString( titleStr ); if ( titleStr != NULL ) title = BString( titleStr );
else return false; else return false;
@ -150,7 +163,6 @@ bool Feed::SetDate ( const char* dateCStr ) {
} }
bool Feed::SetDate ( tinyxml2::XMLElement* elem ) { bool Feed::SetDate ( tinyxml2::XMLElement* elem ) {
if ( elem != NULL ) return SetDate( elem->GetText() ); if ( elem != NULL ) return SetDate( elem->GetText() );
else return false; else return false;
} }

View File

@ -6,6 +6,7 @@
#include <String.h> #include <String.h>
#include <List.h> #include <List.h>
#include <Url.h> #include <Url.h>
#include "Entry.h"
#include "Config.h" #include "Config.h"
class Feed { class Feed {
@ -26,6 +27,7 @@ public:
void Parse ( Config* ); void Parse ( Config* );
bool AddEntry ( Config*, Entry* );
bool SetTitle ( const char* ); bool SetTitle ( const char* );
bool SetTitle ( tinyxml2::XMLElement* ); bool SetTitle ( tinyxml2::XMLElement* );
bool SetDesc ( const char* ); bool SetDesc ( const char* );

View File

@ -52,12 +52,13 @@ invocation ( int argc, char** argv, Config** cfgPtr )
{ "after", required_argument, 0, 'T' }, { "after", required_argument, 0, 'T' },
{ "output", required_argument, 0, 'O' }, { "output", required_argument, 0, 'O' },
{ "foreground", no_argument, 0, 'D' }, { "foreground", no_argument, 0, 'D' },
{ "update", no_argument, 0, 'u' },
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
while (true) { while (true) {
opterr = 0; opterr = 0;
int c = getopt_long(argc, argv, "+hsvDm:O:T:t:c:C:", sLongOptions, NULL); int c = getopt_long(argc, argv, "+hsuvDm:O:T:t:c:C:", sLongOptions, NULL);
switch (c) { switch (c) {
case -1: case -1:
@ -94,6 +95,9 @@ invocation ( int argc, char** argv, Config** cfgPtr )
case 'O': case 'O':
cfg->outDir = BString( optarg ); cfg->outDir = BString( optarg );
break; break;
case 'u':
cfg->updateFeeds = true;
break;
case 'v': case 'v':
cfg->verbose = true; cfg->verbose = true;
break; break;

View File

@ -19,18 +19,27 @@ RssFeed::RssFeed ( BString path, Config* cfg )
void void
RssFeed::Parse ( Config* cfg ) RssFeed::Parse ( Config* cfg )
{ {
entries = BList(); BFile* feedFile = new BFile( filePath.String(), B_READ_ONLY );
tinyxml2::XMLDocument xml; tinyxml2::XMLDocument xml;
xml.LoadFile( filePath.String() ); entries = BList();
time_t tt_lastDate = 0;
BDateTime attrLastDate = BDateTime();
feedFile->ReadAttr( "LastDate", B_TIME_TYPE, 0, &tt_lastDate, sizeof(time_t) );
if ( tt_lastDate > 0 && cfg->minDate == NULL && cfg->updateFeeds == true ) {
attrLastDate.SetTime_t( tt_lastDate );
cfg->minDate = attrLastDate;
}
xml.LoadFile( filePath.String() );
tinyxml2::XMLElement* xchan = xml.FirstChildElement("rss")->FirstChildElement("channel"); tinyxml2::XMLElement* xchan = xml.FirstChildElement("rss")->FirstChildElement("channel");
RootParse( cfg, xchan ); RootParse( cfg, xchan );
ParseEntries( cfg, xchan ); ParseEntries( cfg, xchan );
time_t tt_lastDate = lastDate.Time_t(); tt_lastDate = lastDate.Time_t();
BFile* cacheFile = new BFile( filePath, B_READ_WRITE ); feedFile->WriteAttr( "LastDate", B_TIME_TYPE, 0, &tt_lastDate, sizeof(time_t) );
cacheFile->WriteAttr( "LastDate", B_TIME_TYPE, 0, &tt_lastDate, sizeof(time_t) );
} }
// ------------------------------------- // -------------------------------------
@ -59,14 +68,10 @@ RssFeed::EntryParse ( Config* cfg, tinyxml2::XMLElement* xitem )
newEntry->SetPostUrl ( xitem->FirstChildElement("link") ); newEntry->SetPostUrl ( xitem->FirstChildElement("link") );
newEntry->SetContent ( xitem->FirstChildElement("content:encoded") ); newEntry->SetContent ( xitem->FirstChildElement("content:encoded") );
if (cfg->verbose )
printf("\t%s\n", newEntry->title.String());
if ( lastDate == NULL || lastDate < newEntry->date ) if ( lastDate == NULL || lastDate < newEntry->date )
lastDate = newEntry->date; lastDate = newEntry->date;
if ( withinDateRange( cfg->minDate, newEntry->date, cfg->maxDate ) ) AddEntry( cfg, newEntry );
entries.AddItem( newEntry );
} }
void void