Hash cache entries; replace string/unixdate attrs with TIME 'when' attr
This commit is contained in:
parent
f43195204d
commit
0438c097bf
2
Makefile
2
Makefile
|
@ -69,7 +69,7 @@ RSRCS = \
|
||||||
# - if your library does not follow the standard library naming scheme,
|
# - if your library does not follow the standard library naming scheme,
|
||||||
# you need to specify the path to the library and it's name.
|
# you need to specify the path to the library and it's name.
|
||||||
# (e.g. for mylib.a, specify "mylib.a" or "path/mylib.a")
|
# (e.g. for mylib.a, specify "mylib.a" or "path/mylib.a")
|
||||||
LIBS = be tracker shared tinyxml2 bnetapi network $(STDCPPLIBS)
|
LIBS = be tracker bnetapi network shared tinyxml2 $(STDCPPLIBS)
|
||||||
|
|
||||||
# Specify additional paths to directories following the standard libXXX.so
|
# Specify additional paths to directories following the standard libXXX.so
|
||||||
# or libXXX.a naming scheme. You can specify full paths or paths relative
|
# or libXXX.a naming scheme. You can specify full paths or paths relative
|
||||||
|
|
42
src/Feed.cpp
42
src/Feed.cpp
|
@ -10,7 +10,7 @@ Feed::Feed ( BString path, Config* cfg )
|
||||||
description = BString( "Nondescript, N/A." );
|
description = BString( "Nondescript, N/A." );
|
||||||
homeUrl = BString("");
|
homeUrl = BString("");
|
||||||
xmlUrl = BString("");
|
xmlUrl = BString("");
|
||||||
|
updated = true;
|
||||||
filePath = GetCachePath( path, cfg );
|
filePath = GetCachePath( path, cfg );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,28 +24,48 @@ Feed::Feed ( ) {
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
BString
|
BString
|
||||||
Feed::GetCachePath ( BString falsePath, Config* cfg )
|
Feed::GetCachePath ( BString givenPath, Config* cfg )
|
||||||
{
|
{
|
||||||
BUrl falseUrl = BUrl(falsePath);
|
BUrl givenUrl = BUrl( givenPath );
|
||||||
BString protocol = falseUrl.Protocol().String();
|
BString protocol = givenUrl.Protocol().String();
|
||||||
|
|
||||||
if ( protocol == NULL && falseUrl.UrlString() != NULL )
|
if ( protocol == NULL && givenUrl.UrlString() != NULL )
|
||||||
return falsePath;
|
return givenPath;
|
||||||
if ( protocol != BString("http") && protocol != BString("https") )
|
if ( protocol != BString("http") && protocol != BString("https") )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
BString splitName = falseUrl.Host( );
|
return FetchRemoteFeed( givenPath, cfg );
|
||||||
splitName.Append( falseUrl.Path() );
|
}
|
||||||
|
|
||||||
|
BString
|
||||||
|
Feed::FetchRemoteFeed ( BString givenPath, Config* cfg )
|
||||||
|
{
|
||||||
|
BUrl givenUrl = BUrl( givenPath );
|
||||||
|
BString* newHash = new BString();
|
||||||
|
char oldHash[41];
|
||||||
|
|
||||||
|
BString splitName = givenUrl.Host( );
|
||||||
|
splitName.Append( givenUrl.Path() );
|
||||||
splitName.ReplaceAll("/", "_");
|
splitName.ReplaceAll("/", "_");
|
||||||
|
|
||||||
BString filename = cfg->cacheDir;
|
BString filename = cfg->cacheDir;
|
||||||
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 );
|
||||||
|
|
||||||
if ( cfg->verbose )
|
cacheFile->ReadAttr( "FeedSum", B_STRING_TYPE, 0,
|
||||||
printf( "Saving %s to %s...\n", falsePath.String(), filename.String() );
|
oldHash, 41 );
|
||||||
|
|
||||||
|
if ( cfg->verbose )
|
||||||
|
printf( "Saving %s...\n", givenPath.String() );
|
||||||
|
|
||||||
|
webFetch( givenUrl, cacheFile, newHash );
|
||||||
|
|
||||||
|
cacheFile->WriteAttr( "FeedSum", B_STRING_TYPE, 0,
|
||||||
|
newHash->String(), newHash->CountChars() );
|
||||||
|
|
||||||
|
if ( *(newHash) == BString(oldHash) )
|
||||||
|
updated = false;
|
||||||
|
|
||||||
webFetch( falseUrl, cacheFile );
|
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,11 +16,13 @@ public:
|
||||||
BString title;
|
BString title;
|
||||||
BString description;
|
BString description;
|
||||||
BDateTime date;
|
BDateTime date;
|
||||||
|
BDateTime lastDate;
|
||||||
BString homeUrl;
|
BString homeUrl;
|
||||||
BString xmlUrl;
|
BString xmlUrl;
|
||||||
BString filePath;
|
BString filePath;
|
||||||
BString outputDir;
|
BString outputDir;
|
||||||
BList entries;
|
BList entries;
|
||||||
|
bool updated;
|
||||||
|
|
||||||
void Parse ( Config* );
|
void Parse ( Config* );
|
||||||
|
|
||||||
|
@ -38,6 +40,7 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
BString GetCachePath ( BString, Config* );
|
BString GetCachePath ( BString, Config* );
|
||||||
|
BString FetchRemoteFeed ( BString, Config* );
|
||||||
int xmlCountSiblings ( tinyxml2::XMLElement*, const char* );
|
int xmlCountSiblings ( tinyxml2::XMLElement*, const char* );
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -148,14 +148,12 @@ processFeed ( void* feedArg )
|
||||||
BList entries;
|
BList entries;
|
||||||
|
|
||||||
if ( testFeed->IsAtom() ) {
|
if ( testFeed->IsAtom() ) {
|
||||||
printf("Atom\n");
|
|
||||||
AtomFeed* feed = (AtomFeed*)malloc( sizeof(AtomFeed) );
|
AtomFeed* feed = (AtomFeed*)malloc( sizeof(AtomFeed) );
|
||||||
feed = new AtomFeed( testFeed->filePath, main_cfg );
|
feed = new AtomFeed( testFeed->filePath, main_cfg );
|
||||||
feed->Parse(main_cfg);
|
feed->Parse(main_cfg);
|
||||||
entries = feed->entries;
|
entries = feed->entries;
|
||||||
}
|
}
|
||||||
if ( testFeed->IsRss() ) {
|
if ( testFeed->IsRss() ) {
|
||||||
printf("RSS\n");
|
|
||||||
RssFeed* feed = (RssFeed*)malloc( sizeof(RssFeed) );
|
RssFeed* feed = (RssFeed*)malloc( sizeof(RssFeed) );
|
||||||
feed = new RssFeed( testFeed->filePath, main_cfg );
|
feed = new RssFeed( testFeed->filePath, main_cfg );
|
||||||
feed->Parse(main_cfg);
|
feed->Parse(main_cfg);
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <UrlProtocolListener.h>
|
#include <UrlProtocolListener.h>
|
||||||
#include <Url.h>
|
#include <Url.h>
|
||||||
|
#include <boost/uuid/detail/sha1.hpp>
|
||||||
#include "ProtocolListener.h"
|
#include "ProtocolListener.h"
|
||||||
|
|
||||||
ProtocolListener::ProtocolListener ( bool traceLogging )
|
ProtocolListener::ProtocolListener ( bool traceLogging )
|
||||||
|
@ -14,8 +17,10 @@ ProtocolListener::~ProtocolListener ( )
|
||||||
void
|
void
|
||||||
ProtocolListener::DataReceived ( BUrlRequest* caller, const char* data, off_t position, ssize_t size )
|
ProtocolListener::DataReceived ( BUrlRequest* caller, const char* data, off_t position, ssize_t size )
|
||||||
{
|
{
|
||||||
if (fDownloadIO != NULL)
|
if ( fDownloadIO != NULL )
|
||||||
fDownloadIO->Write(data, size);
|
fDownloadIO->Write( data, size );
|
||||||
|
if ( fSha1 != NULL )
|
||||||
|
fSha1->process_bytes( data, size );
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -29,3 +34,30 @@ ProtocolListener::GetDownloadIO ( )
|
||||||
{
|
{
|
||||||
return fDownloadIO;
|
return fDownloadIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ProtocolListener::SetSha1 ( boost::uuids::detail::sha1* sha1 )
|
||||||
|
{
|
||||||
|
fSha1 = sha1;
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::uuids::detail::sha1*
|
||||||
|
ProtocolListener::GetSha1 ( )
|
||||||
|
{
|
||||||
|
return fSha1;
|
||||||
|
}
|
||||||
|
|
||||||
|
BString
|
||||||
|
ProtocolListener::GetHash ( )
|
||||||
|
{
|
||||||
|
unsigned int hashInt[5];
|
||||||
|
fSha1->get_digest( hashInt );
|
||||||
|
|
||||||
|
std::ostringstream hashStr;
|
||||||
|
for(std::size_t i=0; i<sizeof(hashInt)/sizeof(hashInt[0]); ++i) {
|
||||||
|
hashStr << std::hex <<hashInt[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return BString( hashStr.str().c_str() );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <UrlProtocolListener.h>
|
#include <UrlProtocolListener.h>
|
||||||
#include <Url.h>
|
#include <Url.h>
|
||||||
|
#include <boost/uuid/detail/sha1.hpp>
|
||||||
|
|
||||||
class ProtocolListener : public BUrlProtocolListener
|
class ProtocolListener : public BUrlProtocolListener
|
||||||
{
|
{
|
||||||
|
@ -11,11 +12,17 @@ public:
|
||||||
|
|
||||||
virtual ~ProtocolListener();
|
virtual ~ProtocolListener();
|
||||||
virtual void DataReceived(BUrlRequest*, const char*, off_t, ssize_t);
|
virtual void DataReceived(BUrlRequest*, const char*, off_t, ssize_t);
|
||||||
|
|
||||||
void SetDownloadIO ( BDataIO* );
|
void SetDownloadIO ( BDataIO* );
|
||||||
BDataIO* GetDownloadIO ( );
|
BDataIO* GetDownloadIO ( );
|
||||||
|
|
||||||
|
void SetSha1 ( boost::uuids::detail::sha1* );
|
||||||
|
boost::uuids::detail::sha1* GetSha1 ( );
|
||||||
|
BString GetHash ( );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BDataIO* fDownloadIO;
|
BDataIO* fDownloadIO;
|
||||||
|
boost::uuids::detail::sha1* fSha1;
|
||||||
bool fTraceLogging;
|
bool fTraceLogging;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
12
src/Util.cpp
12
src/Util.cpp
|
@ -4,6 +4,7 @@
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <DateTime.h>
|
#include <DateTime.h>
|
||||||
#include <HttpRequest.h>
|
#include <HttpRequest.h>
|
||||||
|
#include <boost/uuid/detail/sha1.hpp>
|
||||||
#include "ProtocolListener.h"
|
#include "ProtocolListener.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
|
|
||||||
|
@ -73,25 +74,30 @@ withinDateRange ( BDateTime minDate, BDateTime nowDate, BDateTime maxDate )
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
int32
|
int32
|
||||||
webFetch ( char* strUrl, BDataIO* reply )
|
webFetch ( BUrl url, BDataIO* reply )
|
||||||
{
|
{
|
||||||
return webFetch( BUrl(strUrl), reply );
|
BString* ignored = new BString();
|
||||||
|
return webFetch( url, reply, ignored );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int32
|
int32
|
||||||
webFetch ( BUrl url, BDataIO* reply )
|
webFetch ( BUrl url, BDataIO* reply, BString* hash )
|
||||||
{
|
{
|
||||||
ProtocolListener listener(true);
|
ProtocolListener listener(true);
|
||||||
BUrlContext context;
|
BUrlContext context;
|
||||||
|
boost::uuids::detail::sha1 sha1;
|
||||||
|
|
||||||
BHttpRequest request( url, true, "HTTP", &listener, &context );
|
BHttpRequest request( url, true, "HTTP", &listener, &context );
|
||||||
|
|
||||||
listener.SetDownloadIO( reply );
|
listener.SetDownloadIO( reply );
|
||||||
|
listener.SetSha1( &sha1 );
|
||||||
|
|
||||||
thread_id thread = request.Run();
|
thread_id thread = request.Run();
|
||||||
wait_for_thread( thread, NULL );
|
wait_for_thread( thread, NULL );
|
||||||
|
|
||||||
|
*(hash) = listener.GetHash();
|
||||||
|
|
||||||
const BHttpResult& result = dynamic_cast<const BHttpResult&>( request.Result() );
|
const BHttpResult& result = dynamic_cast<const BHttpResult&>( request.Result() );
|
||||||
return result.StatusCode();
|
return result.StatusCode();
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,7 @@ BString dateTo3339String ( BDateTime );
|
||||||
|
|
||||||
bool withinDateRange ( BDateTime, BDateTime, BDateTime );
|
bool withinDateRange ( BDateTime, BDateTime, BDateTime );
|
||||||
|
|
||||||
|
int32 webFetch ( BUrl, BDataIO*, BString* );
|
||||||
int32 webFetch ( BUrl, BDataIO* );
|
int32 webFetch ( BUrl, BDataIO* );
|
||||||
int32 webFetch ( char*, BDataIO* );
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Ŝarĝante…
Reference in New Issue