Parse feed date-strings into epoch time

This commit is contained in:
Jaidyn Ann 2020-07-04 10:27:38 -05:00
parent 39d5842e7c
commit 11e7f34be2
7 changed files with 116 additions and 63 deletions

View File

@ -33,7 +33,7 @@ SRCS = \
src/Item.cpp, \ src/Item.cpp, \
src/ProtocolListener.cpp, \ src/ProtocolListener.cpp, \
src/Config.cpp, \ src/Config.cpp, \
src/webfetch.cpp, \ src/Util.cpp, \
src/parsing.cpp, \ src/parsing.cpp, \
src/Rifen.cpp src/Rifen.cpp

View File

@ -1,5 +1,5 @@
#include <cstdio>
#include <tinyxml2.h> #include <tinyxml2.h>
#include "Util.h"
#include "Channel.h" #include "Channel.h"
#include "Item.h" #include "Item.h"
#include "Config.h" #include "Config.h"
@ -26,31 +26,48 @@ Channel::Parse ( Config* cfg )
feedParser(&chan, cfg); feedParser(&chan, cfg);
} }
void Channel::SetTitle ( const char* titleStr ) { bool Channel::SetTitle ( const char* titleStr ) {
if ( titleStr != NULL ) title = BString( titleStr ); if ( titleStr != NULL ) title = BString( titleStr );
else return false;
return true;
} }
void Channel::SetTitle ( tinyxml2::XMLElement* elem ) { bool Channel::SetTitle ( tinyxml2::XMLElement* elem ) {
if ( elem != NULL ) SetTitle( elem->GetText() ); if ( elem != NULL ) return SetTitle( elem->GetText() );
else return false;
} }
void Channel::SetDesc ( const char* descStr ) { bool Channel::SetDesc ( const char* descStr ) {
if ( descStr != NULL ) description = BString( descStr ); if ( descStr != NULL ) description = BString( descStr );
else return false;
return true;
} }
void Channel::SetDesc ( tinyxml2::XMLElement* elem ) { bool Channel::SetDesc ( tinyxml2::XMLElement* elem ) {
if ( elem != NULL ) SetDesc( elem->GetText() ); if ( elem != NULL ) return SetDesc( elem->GetText() );
else return false;
} }
void Channel::SetHomePage ( const char* homepageStr ) { bool Channel::SetHomePage ( const char* homepageStr ) {
if ( homepageStr != NULL ) if ( homepageStr != NULL ) homePage = BString( homepageStr );
homePage = BString( homepageStr ); else return false;
return true;
} }
void Channel::SetHomePage ( tinyxml2::XMLElement* elem ) { bool Channel::SetHomePage ( tinyxml2::XMLElement* elem ) {
if ( elem != NULL ) SetHomePage( elem->GetText() ); if ( elem != NULL ) return SetHomePage( elem->GetText() );
else return false;
} }
void Channel::SetLastDate ( const char* dateStr ) { bool Channel::SetLastDate ( const char* dateCStr ) {
if ( dateStr != NULL ) lastDate = BString( dateStr ); if ( dateCStr == NULL )
return false;
BString dateStr = stringDateToBString( dateCStr );
if ( dateStr == NULL )
return false;
lastDate = dateStr;
return true;
} }
void Channel::SetLastDate ( tinyxml2::XMLElement* elem ) { bool Channel::SetLastDate ( tinyxml2::XMLElement* elem ) {
if ( elem != NULL ) SetLastDate( elem->GetText() ); if ( elem != NULL ) return SetLastDate( elem->GetText() );
else return false;
} }

View File

@ -29,14 +29,14 @@ public:
// Channel ( BUrl ); // Channel ( BUrl );
void Parse ( Config* ); void Parse ( Config* );
void SetTitle ( const char* ); bool SetTitle ( const char* );
void SetTitle ( tinyxml2::XMLElement* ); bool SetTitle ( tinyxml2::XMLElement* );
void SetDesc ( const char* ); bool SetDesc ( const char* );
void SetDesc ( tinyxml2::XMLElement* ); bool SetDesc ( tinyxml2::XMLElement* );
void SetLastDate ( const char* ); bool SetLastDate ( const char* );
void SetLastDate ( tinyxml2::XMLElement* ); bool SetLastDate ( tinyxml2::XMLElement* );
void SetHomePage ( const char* ); bool SetHomePage ( const char* );
void SetHomePage ( tinyxml2::XMLElement* ); bool SetHomePage ( tinyxml2::XMLElement* );
}; };
#endif #endif

61
src/Util.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <HttpRequest.h>
#include "ProtocolListener.h"
#include "Util.h"
// ----------------------------------------------------------------------------
int
stringDateToEpoch ( const char* dateCStr )
{
std::istringstream ss( dateCStr );
std::tm t = {};
if ( ss >> std::get_time( &t, "%a, %d %b %Y %H:%M:%S" ) )
return std::mktime( &t );
return -1;
}
BString
stringDateToBString ( const char* dateCStr )
{
std::istringstream ss( dateCStr );
std::ostringstream dateStream;
std::tm t = {};
if ( ss >> std::get_time( &t, "%a, %d %b %Y %H:%M:%S" ) ) {
dateStream << std::put_time( &t, "%c" );
std::string dateString = dateStream.str();
return BString( dateStream.str().c_str() );
}
return NULL;
}
// ----------------------------------------------------------------------------
int32
webFetch ( char* strUrl, BDataIO* reply )
{
return webFetch( BUrl(strUrl), reply );
}
int32
webFetch ( BUrl url, BDataIO* reply )
{
ProtocolListener listener(true);
BUrlContext context;
BHttpRequest request( url, true, "HTTP", &listener, &context );
listener.SetDownloadIO( reply );
thread_id thread = request.Run();
wait_for_thread( thread, NULL );
const BHttpResult& result = dynamic_cast<const BHttpResult&>( request.Result() );
return result.StatusCode();
}

13
src/Util.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef UTIL_H
#define UTIL_H
#include <Url.h>
#include "ProtocolListener.h"
int stringDateToEpoch ( const char* dateStr );
BString stringDateToBString ( const char* dateStr );
int32 webFetch ( BUrl, BDataIO* );
int32 webFetch ( char*, BDataIO* );
#endif

View File

@ -1,29 +0,0 @@
#include <HttpRequest.h>
#include "ProtocolListener.h"
#include "webfetch.h"
// ----------------------------------------------------------------------------
int32
webfetch ( char* strUrl, BDataIO* reply )
{
return webfetch( BUrl(strUrl), reply );
}
int32
webfetch ( BUrl url, BDataIO* reply )
{
ProtocolListener listener(true);
BUrlContext context;
BHttpRequest request( url, true, "HTTP", &listener, &context );
listener.SetDownloadIO( reply );
thread_id thread = request.Run();
wait_for_thread( thread, NULL );
const BHttpResult& result = dynamic_cast<const BHttpResult&>( request.Result() );
return result.StatusCode();
}

View File

@ -1,9 +0,0 @@
#ifndef WEBFETCH_H
#define WEBFETCH_H
#include <Url.h>
#include "ProtocolListener.h"
int32 webfetch ( BUrl, BDataIO* );
int32 webfetch ( char*, BDataIO* );
#endif