Skip to content

Commit

Permalink
Languages OPDS feed includes book counts
Browse files Browse the repository at this point in the history
  • Loading branch information
veloman-yunkan authored and kelson42 committed Jul 31, 2021
1 parent a316466 commit 6cd950f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
9 changes: 9 additions & 0 deletions include/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class Library

public:
typedef std::vector<std::string> BookIdCollection;
typedef std::map<std::string, int> AttributeCounts;

public:
Library();
Expand Down Expand Up @@ -242,6 +243,13 @@ class Library
*/
std::vector<std::string> getBooksLanguages() const;

/**
* Get all languagues of the books in the library with counts.
*
* @return A list of languages with the count of books in each language.
*/
AttributeCounts getBooksLanguagesWithCounts() const;

/**
* Get all categories of the books in the library.
*
Expand Down Expand Up @@ -345,6 +353,7 @@ class Library
typedef const std::string& (Book::*BookStrPropMemFn)() const;

private: // functions
AttributeCounts getBookAttributeCounts(BookStrPropMemFn p) const;
std::vector<std::string> getBookPropValueSet(BookStrPropMemFn p) const;
BookIdCollection filterViaBookDB(const Filter& filter) const;
void updateBookDB(const Book& book);
Expand Down
21 changes: 17 additions & 4 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,25 +208,38 @@ bool Library::writeBookmarksToFile(const std::string& path) const
return writeTextFile(path, dumper.dumpLibXMLBookmark());
}

std::vector<std::string> Library::getBookPropValueSet(BookStrPropMemFn p) const
Library::AttributeCounts Library::getBookAttributeCounts(BookStrPropMemFn p) const
{
std::set<std::string> propValues;
AttributeCounts propValueCounts;

for (const auto& pair: m_books) {
const auto& book = pair.second;
if (book.getOrigId().empty()) {
propValues.insert((book.*p)());
propValueCounts[(book.*p)()] += 1;
}
}
return propValueCounts;
}

return std::vector<std::string>(propValues.begin(), propValues.end());
std::vector<std::string> Library::getBookPropValueSet(BookStrPropMemFn p) const
{
std::vector<std::string> result;
for ( const auto& kv : getBookAttributeCounts(p) ) {
result.push_back(kv.first);
}
return result;
}

std::vector<std::string> Library::getBooksLanguages() const
{
return getBookPropValueSet(&Book::getLanguage);
}

Library::AttributeCounts Library::getBooksLanguagesWithCounts() const
{
return getBookAttributeCounts(&Book::getLanguage);
}

std::vector<std::string> Library::getBooksCategories() const
{
std::set<std::string> categories;
Expand Down
5 changes: 4 additions & 1 deletion src/opds_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,14 @@ std::string OPDSDumper::languagesOPDSFeed() const
{
const auto now = gen_date_str();
kainjow::mustache::list languageData;
for ( const auto& languageCode : library->getBooksLanguages() ) {
for ( const auto& langAndBookCount : library->getBooksLanguagesWithCounts() ) {
const std::string languageCode = langAndBookCount.first;
const int bookCount = langAndBookCount.second;
const auto languageSelfName = getLanguageSelfName(languageCode);
languageData.push_back(kainjow::mustache::object{
{"lang_code", languageCode},
{"lang_self_name", languageSelfName},
{"book_count", to_string(bookCount)},
{"updated", now},
{"id", gen_uuid(libraryId + "/languages/" + languageCode)}
});
Expand Down
1 change: 1 addition & 0 deletions static/templates/catalog_v2_languages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<entry>
<title>{{lang_self_name}}</title>
<dc:language>{{{lang_code}}}</dc:language>
<thr:count>{{book_count}}</thr:count>
<link rel="subsection"
href="{{endpoint_root}}/entries?lang={{{lang_code}}}"
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>
Expand Down
3 changes: 3 additions & 0 deletions test/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ TEST_F(LibraryServerTest, catalog_v2_languages)
<entry>
<title>English</title>
<dc:language>eng</dc:language>
<thr:count>1</thr:count>
<link rel="subsection"
href="/catalog/v2/entries?lang=eng"
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>
Expand All @@ -1044,6 +1045,7 @@ TEST_F(LibraryServerTest, catalog_v2_languages)
<entry>
<title>français</title>
<dc:language>fra</dc:language>
<thr:count>1</thr:count>
<link rel="subsection"
href="/catalog/v2/entries?lang=fra"
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>
Expand All @@ -1053,6 +1055,7 @@ TEST_F(LibraryServerTest, catalog_v2_languages)
<entry>
<title>русский</title>
<dc:language>rus</dc:language>
<thr:count>1</thr:count>
<link rel="subsection"
href="/catalog/v2/entries?lang=rus"
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>
Expand Down

0 comments on commit 6cd950f

Please sign in to comment.