2020-07-08 03:20:03 -05:00
|
|
|
#include <tinyxml2.h>
|
|
|
|
#include "Entry.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "Feed.h"
|
|
|
|
|
2020-07-13 12:31:52 -05:00
|
|
|
Feed::Feed ( BString path, Config* cfg )
|
2020-07-08 03:20:03 -05:00
|
|
|
{
|
2020-07-13 12:31:52 -05:00
|
|
|
title = BString( "Untitled Feed" );
|
|
|
|
description = BString( "Nondescript, N/A." );
|
2020-07-08 03:20:03 -05:00
|
|
|
homeUrl = BString("");
|
|
|
|
xmlUrl = BString("");
|
2020-08-02 02:05:20 -05:00
|
|
|
updated = true;
|
2020-07-13 12:31:52 -05:00
|
|
|
filePath = GetCachePath( path, cfg );
|
2020-07-08 03:20:03 -05:00
|
|
|
}
|
|
|
|
|
2020-08-11 13:29:49 -05:00
|
|
|
Feed::Feed ( )
|
|
|
|
{
|
2020-07-08 04:43:35 -05:00
|
|
|
title = BString("");
|
|
|
|
description = BString("");
|
|
|
|
homeUrl = BString("");
|
|
|
|
xmlUrl = BString("");
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-13 12:31:52 -05:00
|
|
|
BString
|
2020-08-02 02:05:20 -05:00
|
|
|
Feed::GetCachePath ( BString givenPath, Config* cfg )
|
2020-07-13 12:31:52 -05:00
|
|
|
{
|
2020-08-02 02:05:20 -05:00
|
|
|
BUrl givenUrl = BUrl( givenPath );
|
|
|
|
BString protocol = givenUrl.Protocol().String();
|
2020-07-13 12:31:52 -05:00
|
|
|
|
2020-08-02 02:05:20 -05:00
|
|
|
if ( protocol == NULL && givenUrl.UrlString() != NULL )
|
|
|
|
return givenPath;
|
2020-07-13 12:31:52 -05:00
|
|
|
if ( protocol != BString("http") && protocol != BString("https") )
|
|
|
|
return NULL;
|
|
|
|
|
2020-08-02 02:05:20 -05:00
|
|
|
return FetchRemoteFeed( givenPath, cfg );
|
|
|
|
}
|
|
|
|
|
|
|
|
BString
|
|
|
|
Feed::FetchRemoteFeed ( BString givenPath, Config* cfg )
|
|
|
|
{
|
|
|
|
BUrl givenUrl = BUrl( givenPath );
|
2020-08-14 01:59:15 -05:00
|
|
|
time_t tt_lastDate = 0;
|
|
|
|
BDateTime* lastDate = new BDateTime();
|
2020-08-02 02:05:20 -05:00
|
|
|
BString* newHash = new BString();
|
|
|
|
char oldHash[41];
|
|
|
|
|
|
|
|
BString splitName = givenUrl.Host( );
|
|
|
|
splitName.Append( givenUrl.Path() );
|
2020-07-13 12:31:52 -05:00
|
|
|
splitName.ReplaceAll("/", "_");
|
|
|
|
|
|
|
|
BString filename = cfg->cacheDir;
|
|
|
|
filename.Append(splitName);
|
|
|
|
BFile* cacheFile = new BFile( filename, B_READ_WRITE | B_CREATE_FILE );
|
|
|
|
|
2020-08-14 01:59:15 -05:00
|
|
|
cacheFile->ReadAttr( "LastHash", B_STRING_TYPE, 0, oldHash, 41 );
|
2020-08-02 02:05:20 -05:00
|
|
|
|
2020-07-13 12:31:52 -05:00
|
|
|
if ( cfg->verbose )
|
2020-08-02 02:05:20 -05:00
|
|
|
printf( "Saving %s...\n", givenPath.String() );
|
|
|
|
|
|
|
|
webFetch( givenUrl, cacheFile, newHash );
|
|
|
|
|
2020-08-11 13:29:49 -05:00
|
|
|
cacheFile->WriteAttr( "LastHash", B_STRING_TYPE, 0,
|
2020-08-02 02:05:20 -05:00
|
|
|
newHash->String(), newHash->CountChars() );
|
|
|
|
|
|
|
|
if ( *(newHash) == BString(oldHash) )
|
|
|
|
updated = false;
|
2020-07-13 12:31:52 -05:00
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-08 04:43:35 -05:00
|
|
|
bool
|
|
|
|
Feed::IsRss ( )
|
|
|
|
{
|
|
|
|
tinyxml2::XMLDocument xml;
|
|
|
|
xml.LoadFile( filePath.String() );
|
|
|
|
|
|
|
|
if ( xml.FirstChildElement("rss") )
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Feed::IsAtom ( )
|
|
|
|
{
|
|
|
|
tinyxml2::XMLDocument xml;
|
|
|
|
xml.LoadFile( filePath.String() );
|
|
|
|
|
|
|
|
if ( xml.FirstChildElement("feed") )
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
int
|
|
|
|
Feed::xmlCountSiblings ( tinyxml2::XMLElement* xsibling, const char* sibling_name )
|
2020-07-08 03:20:03 -05:00
|
|
|
{
|
2020-07-08 04:43:35 -05:00
|
|
|
int count = 0;
|
|
|
|
while ( xsibling ) {
|
|
|
|
count++;
|
|
|
|
xsibling = xsibling->NextSiblingElement(sibling_name);
|
|
|
|
}
|
|
|
|
return count;
|
2020-07-08 03:20:03 -05:00
|
|
|
}
|
|
|
|
|
2020-07-08 04:43:35 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-08-14 01:59:15 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-08 03:20:03 -05:00
|
|
|
bool Feed::SetTitle ( const char* titleStr ) {
|
|
|
|
if ( titleStr != NULL ) title = BString( titleStr );
|
|
|
|
else return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Feed::SetTitle ( tinyxml2::XMLElement* elem ) {
|
|
|
|
if ( elem != NULL ) return SetTitle( elem->GetText() );
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Feed::SetDesc ( const char* descStr ) {
|
|
|
|
if ( descStr != NULL ) description = BString( descStr );
|
|
|
|
else return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Feed::SetDesc ( tinyxml2::XMLElement* elem ) {
|
|
|
|
if ( elem != NULL ) return SetDesc( elem->GetText() );
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Feed::SetHomeUrl ( const char* homepageStr ) {
|
|
|
|
if ( homepageStr != NULL ) homeUrl = BString( homepageStr );
|
|
|
|
else return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Feed::SetHomeUrl ( tinyxml2::XMLElement* elem ) {
|
|
|
|
if ( elem != NULL ) return SetHomeUrl( elem->GetText() );
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Feed::SetDate ( const char* dateCStr ) {
|
|
|
|
if ( dateCStr == NULL )
|
|
|
|
return false;
|
|
|
|
BDateTime newDate = feedDateToBDate( dateCStr );
|
|
|
|
if ( newDate == NULL )
|
|
|
|
return false;
|
|
|
|
date = newDate;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Feed::SetDate ( tinyxml2::XMLElement* elem ) {
|
|
|
|
if ( elem != NULL ) return SetDate( elem->GetText() );
|
|
|
|
else return false;
|
|
|
|
}
|
2020-07-08 04:43:35 -05:00
|
|
|
|