Only one subpage

This commit is contained in:
Love 2025-01-22 13:36:57 +01:00
parent 2295fdc3ce
commit 360a85b7bf
2 changed files with 25 additions and 36 deletions

View File

@ -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::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);
}
return ret;
}
Page &Page::operator+=(int) {
number++;
m_number++;
refresh();
return *this;
}
bool Page::refresh() {
std::vector<std::string> 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<std::string> 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<std::string> Page::fetchSubpages() const {
throw std::runtime_error("Could not evaluate XPath expression.");
}
std::vector<std::string> 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<const char *>(content));
page = reinterpret_cast<const char *>(content);
xmlFree(content);
break;
}
}
@ -106,16 +106,5 @@ std::vector<std::string> Page::fetchSubpages() const {
xmlXPathFreeContext(xpathCtx);
xmlFreeDoc(doc);
return pages;
}
bool Page::contentEquals(const std::vector<std::string> &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;
}

View File

@ -18,8 +18,8 @@ static constexpr uint_fast8_t MAX_WHITESPACE = 2;
class Page
{
private:
uint_fast8_t number{};
std::vector<std::string> 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<std::string> fetchSubpages() const;
[[nodiscard]] std::string fetchSubpage() const;
[[nodiscard]] bool contentEquals(const std::vector<std::string> &subpagesOther) const;
};