From 360a85b7bf873083b3f1408c84ee0004719479b4 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Wed, 22 Jan 2025 13:36:57 +0100 Subject: [PATCH] Only one subpage --- src/Page.cpp | 55 +++++++++++++++++++++------------------------------- src/Page.hpp | 6 +++--- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/Page.cpp b/src/Page.cpp index 05b7875..ebe23f4 100644 --- a/src/Page.cpp +++ b/src/Page.cpp @@ -17,19 +17,19 @@ #include "libxml/xpath.h" -Page::Page(const uint_fast8_t number): number(number), subpages(fetchSubpages()) { +Page::Page(const uint_fast8_t number): m_number(number), m_subpage(fetchSubpage()) { } Page Page::operator--(int) const { - return Page(number - 1); + return Page(m_number - 1); } Page Page::operator++(int) const { - return Page(number + 1); + return Page(m_number + 1); } Page &Page::operator-=(int) { - number--; + m_number--; refresh(); return *this; } @@ -37,38 +37,36 @@ Page &Page::operator-=(int) { std::string Page::str() const { std::string ret; - for (const std::string &pageText: subpages) { - std::istringstream stream(pageText); - std::string line; - while (std::getline(stream, line)) - ret += line + "\n"; + std::istringstream stream(m_subpage); + std::string line; + while (std::getline(stream, line)) + ret += line + "\n"; - string_utils::limitConsecutiveWhitespace(ret, MAX_WHITESPACE); - string_utils::removeTrailingWhitespace(ret); - } + string_utils::limitConsecutiveWhitespace(ret, MAX_WHITESPACE); + string_utils::removeTrailingWhitespace(ret); return ret; } Page &Page::operator+=(int) { - number++; + m_number++; refresh(); return *this; } bool Page::refresh() { - std::vector newSubpages = fetchSubpages(); - const bool replace = !contentEquals(newSubpages); + std::string newSubpage = fetchSubpage(); + const bool replace = newSubpage != m_subpage; if (replace) - subpages = newSubpages; + m_subpage = newSubpage; return replace; } std::string Page::url() const { - return std::format("https://www.svt.se/svttext/web/pages/{}.html", number); + return std::format("https://www.svt.se/svttext/web/pages/{}.html", m_number); } -std::vector Page::fetchSubpages() const { +std::string Page::fetchSubpage() const { const cpr::Response response = cpr::Get(cpr::Url{url()}); if (response.status_code / 100 != 2) throw std::runtime_error("Page not found"); @@ -91,14 +89,16 @@ std::vector Page::fetchSubpages() const { throw std::runtime_error("Could not evaluate XPath expression."); } - std::vector pages; + // There's only one valid page + std::string page; if (const xmlNodeSetPtr nodes = xpathObj->nodesetval) { - for (int i = 0; i < nodes->nodeNr; ++i) { + for (int i = 0; i < nodes->nodeNr; i++) { xmlChar *content = xmlNodeGetContent(nodes->nodeTab[i]); if (!content) continue; - pages.emplace_back(reinterpret_cast(content)); + page = reinterpret_cast(content); xmlFree(content); + break; } } @@ -106,16 +106,5 @@ std::vector Page::fetchSubpages() const { xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); - return pages; -} - -bool Page::contentEquals(const std::vector &subpagesOther) const { - if (subpagesOther.size() != subpages.size()) - return false; - - for (std::size_t i = 0; i < subpages.size(); i++) - if (subpages[i] != subpagesOther[i]) - return false; - - return true; + return page; } diff --git a/src/Page.hpp b/src/Page.hpp index 6ea3936..76c0884 100644 --- a/src/Page.hpp +++ b/src/Page.hpp @@ -18,8 +18,8 @@ static constexpr uint_fast8_t MAX_WHITESPACE = 2; class Page { private: - uint_fast8_t number{}; - std::vector subpages; + uint_fast8_t m_number{}; + std::string m_subpage; public: explicit Page(uint_fast8_t number = DEFAULT_NUMBER); @@ -39,7 +39,7 @@ public: private: [[nodiscard]] std::string url() const; - [[nodiscard]] std::vector fetchSubpages() const; + [[nodiscard]] std::string fetchSubpage() const; [[nodiscard]] bool contentEquals(const std::vector &subpagesOther) const; };