From 093b71d339a89e6a25e2ec0d21d1c0b36f72aa76 Mon Sep 17 00:00:00 2001 From: ping Date: Thu, 28 Sep 2023 18:44:43 +0800 Subject: [PATCH] Add media format and fiction/non-fiction options for Advanced Search --- calibre-plugin/dialog/advanced_search.py | 55 ++++++++++++++++++++++++ calibre-plugin/overdrive/client.py | 8 ++++ 2 files changed, 63 insertions(+) diff --git a/calibre-plugin/dialog/advanced_search.py b/calibre-plugin/dialog/advanced_search.py index 5071836..3381ddc 100644 --- a/calibre-plugin/dialog/advanced_search.py +++ b/calibre-plugin/dialog/advanced_search.py @@ -93,6 +93,7 @@ def __init__(self, *args): self.identifier_txt.setClearButtonEnabled(True) form_fields_layout.addRow(_("ISBN"), self.identifier_txt) + rb_layout_spacing = 10 self.availability_btn_group = QButtonGroup(self) self.availability_all_rb = QRadioButton(_("All"), self) self.availability_only_available_rb = QRadioButton(_("Available now"), self) @@ -103,11 +104,47 @@ def __init__(self, *args): self.availability_only_prelease_rb, ) self.availability_rb_layout = QHBoxLayout() + self.availability_rb_layout.setSpacing(rb_layout_spacing) for rb in availability_rb: self.availability_btn_group.addButton(rb) self.availability_rb_layout.addWidget(rb) form_fields_layout.addRow(_("Availability"), self.availability_rb_layout) + if PREFS[PreferenceKeys.INCL_NONDOWNLOADABLE_TITLES]: + self.media_type_btn_group = QButtonGroup(self) + self.media_all_rb = QRadioButton(_("Any"), self) + self.media_ebook_rb = QRadioButton(_("Book"), self) + self.media_audiobook_rb = QRadioButton(_("Audiobook"), self) + self.media_magazine_rb = QRadioButton(_("Magazine"), self) + media_type_rb = ( + self.media_all_rb, + self.media_ebook_rb, + self.media_audiobook_rb, + self.media_magazine_rb, + ) + self.media_rb_layout = QHBoxLayout() + self.media_rb_layout.setSpacing(rb_layout_spacing) + for rb in media_type_rb: + self.media_type_btn_group.addButton(rb) + self.media_rb_layout.addWidget(rb) + form_fields_layout.addRow(_("Media"), self.media_rb_layout) + + self.subject_btn_group = QButtonGroup(self) + self.subject_all_rb = QRadioButton(_("Any"), self) + self.subject_fiction_rb = QRadioButton(_("Fiction"), self) + self.subject_nonfiction_rb = QRadioButton(_("Nonfiction"), self) + subject_rb = ( + self.subject_all_rb, + self.subject_fiction_rb, + self.subject_nonfiction_rb, + ) + self.subject_rb_layout = QHBoxLayout() + self.subject_rb_layout.setSpacing(rb_layout_spacing) + for rb in subject_rb: + self.subject_btn_group.addButton(rb) + self.subject_rb_layout.addWidget(rb) + form_fields_layout.addRow(_("Subject"), self.subject_rb_layout) + # Search button self.adv_search_btn = DefaultQPushButton( _c("Search"), self.resources[PluginImages.Search], self @@ -312,6 +349,22 @@ def adv_search_btn_clicked(self): LibbyFormats.AudioBookMP3, LibbyFormats.AudioBookOverDrive, ] + + media_type = "" + if PREFS[PreferenceKeys.INCL_NONDOWNLOADABLE_TITLES]: + if self.media_ebook_rb.isChecked(): + media_type = "ebook" + elif self.media_audiobook_rb.isChecked(): + media_type = "audiobook" + elif self.media_magazine_rb.isChecked(): + media_type = "magazine" + + subject_id = "" + if self.subject_nonfiction_rb.isChecked(): + subject_id = "111" + elif self.subject_fiction_rb.isChecked(): + subject_id = "26" + query = LibraryMediaSearchParams( query=self.adv_query_txt.text(), title=self.title_txt.text(), @@ -320,6 +373,8 @@ def adv_search_btn_clicked(self): show_only_available=self.availability_only_available_rb.isChecked(), show_only_prelease=self.availability_only_prelease_rb.isChecked(), formats=formats, + media_type=media_type, + subject_id=subject_id, per_page=PREFS[PreferenceKeys.SEARCH_RESULTS_MAX], ) if query.is_empty(): diff --git a/calibre-plugin/overdrive/client.py b/calibre-plugin/overdrive/client.py index abc52c0..b3894a1 100644 --- a/calibre-plugin/overdrive/client.py +++ b/calibre-plugin/overdrive/client.py @@ -52,6 +52,8 @@ class LibraryMediaSearchParams: sort_by: str = SearchSortBy.RELEVANCE show_only_available: bool = False show_only_prelease: bool = False + media_type: str = "" + subject_id: str = "" title_ids: List[str] = field(default_factory=list) def is_empty(self) -> bool: @@ -63,6 +65,8 @@ def is_empty(self) -> bool: or self.show_only_available or self.show_only_prelease or self.title_ids + or self.media_type + or self.subject_id ) def convert_bool(self, value: bool): @@ -87,6 +91,10 @@ def to_dict(self) -> Dict: result[a] = str(v).strip() if self.title_ids: result["titleIds"] = ",".join(self.title_ids) + if self.media_type: + result["mediaTypes"] = self.media_type + if self.subject_id: + result["subject"] = self.subject_id return result