Introduce readCategoriesFromFeed()

Added a function to load categories stored in an OPDS stream
This commit is contained in:
Nikhil Tanwar 2023-07-11 15:44:27 +05:30
parent a28c2973e9
commit 94d6bef402
2 changed files with 38 additions and 0 deletions

View File

@ -28,6 +28,7 @@
namespace kiwix { namespace kiwix {
typedef std::pair<std::string, std::string> LangNameCodePair; typedef std::pair<std::string, std::string> LangNameCodePair;
typedef std::vector<LangNameCodePair> FeedLanguages; typedef std::vector<LangNameCodePair> FeedLanguages;
typedef std::vector<std::string> FeedCategories;
/** /**
* Return the current directory. * Return the current directory.
@ -235,5 +236,13 @@ std::string beautifyFileSize(uint64_t number);
* @return vector containing pairs of language code and their corresponding full language name. * @return vector containing pairs of language code and their corresponding full language name.
*/ */
FeedLanguages readLanguagesFromFeed(const std::string& content); FeedLanguages readLanguagesFromFeed(const std::string& content);
/**
* Load categories stored in an OPDS stream .
*
* @param content the OPDS stream.
* @return vector containing category strings.
*/
FeedCategories readCategoriesFromFeed(const std::string& content);
} }
#endif // KIWIX_TOOLS_H #endif // KIWIX_TOOLS_H

View File

@ -22,6 +22,20 @@ FeedLanguages parseLanguages(const pugi::xml_document& doc)
return langs; return langs;
} }
FeedCategories parseCategories(const pugi::xml_document& doc)
{
pugi::xml_node feedNode = doc.child("feed");
FeedCategories categories;
for (pugi::xml_node entryNode = feedNode.child("entry"); entryNode;
entryNode = entryNode.next_sibling("entry")) {
auto title = VALUE("title");
categories.push_back(title);
}
return categories;
}
} // unnamed namespace } // unnamed namespace
FeedLanguages readLanguagesFromFeed(const std::string& content) FeedLanguages readLanguagesFromFeed(const std::string& content)
@ -38,4 +52,19 @@ FeedLanguages readLanguagesFromFeed(const std::string& content)
return FeedLanguages(); return FeedLanguages();
} }
FeedCategories readCategoriesFromFeed(const std::string& content)
{
pugi::xml_document doc;
pugi::xml_parse_result result
= doc.load_buffer((void*)content.data(), content.size());
FeedCategories categories;
if (result) {
categories = parseCategories(doc);
return categories;
}
return categories;
}
} // namespace kiwix } // namespace kiwix