From 6b2067c236fa34a159a9606c6c435c4eb5ba00ec Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Wed, 10 Mar 2021 21:26:28 +0400 Subject: [PATCH] Reading category element from OPDS stream --- include/book.h | 4 ++++ src/book.cpp | 8 ++++++++ test/library.cpp | 18 +++++++++++++++--- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/include/book.h b/include/book.h index eff8a037f..f9e685f5f 100644 --- a/include/book.h +++ b/include/book.h @@ -95,6 +95,9 @@ class Book void setFaviconMimeType(const std::string& faviconMimeType) { m_faviconMimeType = faviconMimeType; } void setDownloadId(const std::string& downloadId) { m_downloadId = downloadId; } + private: + std::string getCategoryFromTags() const; + protected: std::string m_id; std::string m_downloadId; @@ -102,6 +105,7 @@ class Book bool m_pathValid = false; std::string m_title; std::string m_description; + std::string m_category; std::string m_language; std::string m_creator; std::string m_publisher; diff --git a/src/book.cpp b/src/book.cpp index f7f284036..a2f349384 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -127,6 +127,7 @@ void Book::updateFromXml(const pugi::xml_node& node, const std::string& baseDir) try { m_downloadId = ATTR("downloadId"); } catch(...) {} + m_category = getCategoryFromTags(); } #undef ATTR @@ -156,6 +157,8 @@ void Book::updateFromOpds(const pugi::xml_node& node, const std::string& urlHost m_name = VALUE("name"); m_flavour = VALUE("flavour"); m_tags = VALUE("tags"); + const auto catnode = node.child("category"); + m_category = catnode.empty() ? getCategoryFromTags() : catnode.child_value(); m_articleCount = strtoull(VALUE("articleCount"), 0, 0); m_mediaCount = strtoull(VALUE("mediaCount"), 0, 0); for(auto linkNode = node.child("link"); linkNode; @@ -221,6 +224,11 @@ bool Book::getTagBool(const std::string& tagName) const { } std::string Book::getCategory() const +{ + return m_category; +} + +std::string Book::getCategoryFromTags() const { try { diff --git a/test/library.cpp b/test/library.cpp index 322890f7f..7f34e008b 100644 --- a/test/library.cpp +++ b/test/library.cpp @@ -46,7 +46,7 @@ const char * sampleOpdsStream = R"( 2018-06-23T00:00::00:Z fra Tania Louis videos - youtube + youtube;_category:category_defined_via_tags_only Tania Louis @@ -61,6 +61,7 @@ const char * sampleOpdsStream = R"( 2019-06-05T00:00::00:Z fra Une page de Wikiquote, le recueil des citations libres. + category_defined_via_category_element_only wikiquote;nopic @@ -76,7 +77,8 @@ const char * sampleOpdsStream = R"( 2019-06-02T00:00::00:Z Une sélection d'articles de Wikipédia sur la géographie fra - wikipedia;nopic + category_element_overrides_tags + wikipedia;nopic;_category:tags_override_category_element Wikipedia @@ -91,7 +93,8 @@ const char * sampleOpdsStream = R"( 2019-05-13T00:00::00:Z fra Une - wikipedia;nopic + wikipedia;nopic;_category:tags_override_category_element + category_element_overrides_tags Wikipedia @@ -228,6 +231,15 @@ TEST_F(LibraryTest, sanityCheck) EXPECT_EQ(lib.getBooksPublishers().size(), 1U); } +TEST_F(LibraryTest, categoryHandling) +{ + EXPECT_EQ("", lib.getBookById("0c45160e-f917-760a-9159-dfe3c53cdcdd").getCategory()); + EXPECT_EQ("category_defined_via_tags_only", lib.getBookById("0d0bcd57-d3f6-cb22-44cc-a723ccb4e1b2").getCategory()); + EXPECT_EQ("category_defined_via_category_element_only", lib.getBookById("0ea1cde6-441d-6c58-f2c7-21c2838e659f").getCategory()); + EXPECT_EQ("category_element_overrides_tags", lib.getBookById("1123e574-6eef-6d54-28fc-13e4caeae474").getCategory()); + EXPECT_EQ("category_element_overrides_tags", lib.getBookById("14829621-c490-c376-0792-9de558b57efa").getCategory()); +} + TEST_F(LibraryTest, filterCheck) { auto bookIds = lib.filter(kiwix::Filter());