From 11e7f34be2a73921547a2c0892bf376b0ebb7a5f Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sat, 4 Jul 2020 10:27:38 -0500 Subject: [PATCH] Parse feed date-strings into epoch time --- Makefile | 2 +- src/Channel.cpp | 49 +++++++++++++++++++++++++------------- src/Channel.h | 16 ++++++------- src/Util.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Util.h | 13 +++++++++++ src/webfetch.cpp | 29 ----------------------- src/webfetch.h | 9 ------- 7 files changed, 116 insertions(+), 63 deletions(-) create mode 100644 src/Util.cpp create mode 100644 src/Util.h delete mode 100644 src/webfetch.cpp delete mode 100644 src/webfetch.h diff --git a/Makefile b/Makefile index 4873ff0..305aed4 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ SRCS = \ src/Item.cpp, \ src/ProtocolListener.cpp, \ src/Config.cpp, \ - src/webfetch.cpp, \ + src/Util.cpp, \ src/parsing.cpp, \ src/Rifen.cpp diff --git a/src/Channel.cpp b/src/Channel.cpp index 2a26eb7..eab225f 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -1,5 +1,5 @@ -#include #include +#include "Util.h" #include "Channel.h" #include "Item.h" #include "Config.h" @@ -26,31 +26,48 @@ Channel::Parse ( Config* cfg ) feedParser(&chan, cfg); } -void Channel::SetTitle ( const char* titleStr ) { +bool Channel::SetTitle ( const char* titleStr ) { if ( titleStr != NULL ) title = BString( titleStr ); + else return false; + return true; } -void Channel::SetTitle ( tinyxml2::XMLElement* elem ) { - if ( elem != NULL ) SetTitle( elem->GetText() ); +bool Channel::SetTitle ( tinyxml2::XMLElement* elem ) { + 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 ); + else return false; + return true; } -void Channel::SetDesc ( tinyxml2::XMLElement* elem ) { - if ( elem != NULL ) SetDesc( elem->GetText() ); +bool Channel::SetDesc ( tinyxml2::XMLElement* elem ) { + if ( elem != NULL ) return SetDesc( elem->GetText() ); + else return false; } -void Channel::SetHomePage ( const char* homepageStr ) { - if ( homepageStr != NULL ) - homePage = BString( homepageStr ); +bool Channel::SetHomePage ( const char* homepageStr ) { + if ( homepageStr != NULL ) homePage = BString( homepageStr ); + else return false; + return true; } -void Channel::SetHomePage ( tinyxml2::XMLElement* elem ) { - if ( elem != NULL ) SetHomePage( elem->GetText() ); +bool Channel::SetHomePage ( tinyxml2::XMLElement* elem ) { + if ( elem != NULL ) return SetHomePage( elem->GetText() ); + else return false; } -void Channel::SetLastDate ( const char* dateStr ) { - if ( dateStr != NULL ) lastDate = BString( dateStr ); +bool Channel::SetLastDate ( const char* dateCStr ) { + if ( dateCStr == NULL ) + return false; + + BString dateStr = stringDateToBString( dateCStr ); + if ( dateStr == NULL ) + return false; + lastDate = dateStr; + return true; } -void Channel::SetLastDate ( tinyxml2::XMLElement* elem ) { - if ( elem != NULL ) SetLastDate( elem->GetText() ); +bool Channel::SetLastDate ( tinyxml2::XMLElement* elem ) { + if ( elem != NULL ) return SetLastDate( elem->GetText() ); + + else return false; } diff --git a/src/Channel.h b/src/Channel.h index 3a2d64a..5ed91ba 100644 --- a/src/Channel.h +++ b/src/Channel.h @@ -29,14 +29,14 @@ public: // Channel ( BUrl ); void Parse ( Config* ); - void SetTitle ( const char* ); - void SetTitle ( tinyxml2::XMLElement* ); - void SetDesc ( const char* ); - void SetDesc ( tinyxml2::XMLElement* ); - void SetLastDate ( const char* ); - void SetLastDate ( tinyxml2::XMLElement* ); - void SetHomePage ( const char* ); - void SetHomePage ( tinyxml2::XMLElement* ); + bool SetTitle ( const char* ); + bool SetTitle ( tinyxml2::XMLElement* ); + bool SetDesc ( const char* ); + bool SetDesc ( tinyxml2::XMLElement* ); + bool SetLastDate ( const char* ); + bool SetLastDate ( tinyxml2::XMLElement* ); + bool SetHomePage ( const char* ); + bool SetHomePage ( tinyxml2::XMLElement* ); }; #endif diff --git a/src/Util.cpp b/src/Util.cpp new file mode 100644 index 0000000..30636d7 --- /dev/null +++ b/src/Util.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#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( request.Result() ); + return result.StatusCode(); +} diff --git a/src/Util.h b/src/Util.h new file mode 100644 index 0000000..d1580e7 --- /dev/null +++ b/src/Util.h @@ -0,0 +1,13 @@ +#ifndef UTIL_H +#define UTIL_H + +#include +#include "ProtocolListener.h" + +int stringDateToEpoch ( const char* dateStr ); +BString stringDateToBString ( const char* dateStr ); +int32 webFetch ( BUrl, BDataIO* ); +int32 webFetch ( char*, BDataIO* ); + + +#endif diff --git a/src/webfetch.cpp b/src/webfetch.cpp deleted file mode 100644 index 2d97442..0000000 --- a/src/webfetch.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#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( request.Result() ); - return result.StatusCode(); -} diff --git a/src/webfetch.h b/src/webfetch.h deleted file mode 100644 index c6d480d..0000000 --- a/src/webfetch.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef WEBFETCH_H -#define WEBFETCH_H -#include -#include "ProtocolListener.h" - -int32 webfetch ( BUrl, BDataIO* ); -int32 webfetch ( char*, BDataIO* ); - -#endif