Skip to content

Commit

Permalink
Implement viewing loan in Libby
Browse files Browse the repository at this point in the history
  • Loading branch information
ping committed Jul 3, 2023
1 parent 18c1a1a commit 7f321be
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 130 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Changelog

Unreleased
- Add feature to return loans
- Add feature to auto tag books downloaded by the plugin
- Remove Verbose Logs option from config
- Improve implementation for Download, Refresh buttons
- New: View loans in Libby (from context menu)
- New: Return loans (from context menu)
- New: Auto tag books downloaded by the plugin
- Removed Verbose Logs option from config
- Improved implementation for Download, Refresh buttons
- Detect invalid Libby setup code format early
- Reduce zip file size by removing unneeded translation files
- Reduced zip file size by removing unneeded translation files

Version 0.1.2 - 2023-07-02
- Plugin now remembers the size of the main loans window
Expand Down
53 changes: 42 additions & 11 deletions calibre-plugin/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
QMenu,
QCursor,
QModelIndex,
QUrl,
QDesktopServices,
)

from . import logger, PLUGIN_NAME, PLUGIN_ICON, __version__
Expand Down Expand Up @@ -87,7 +89,7 @@ def apply_settings(self):


class DataWorker(QObject):
finished = pyqtSignal(list)
finished = pyqtSignal(dict)

def __int__(self):
super().__init__()
Expand All @@ -101,9 +103,9 @@ def run(self):
client = LibbyClient(
identity_token=libby_token, max_retries=1, timeout=30, logger=logger
)
loans = client.get_loans()
synced_state = client.sync()
logger.info("Request took %f seconds" % (timer() - start))
self.finished.emit(loans)
self.finished.emit(synced_state)


def get_loan_title(loan: Dict) -> str:
Expand Down Expand Up @@ -250,7 +252,7 @@ def resizeEvent(self, e):
logger.debug("Saved new UI height preference: %d", new_height)

def do_refresh(self):
self.model.refresh_loans([])
self.model.refresh_loans({})
self.fetch_loans()

def fetch_loans(self):
Expand Down Expand Up @@ -433,12 +435,33 @@ def loan_view_context_menu(self, pos):
if not selection_model.hasSelection():
return
indices = selection_model.selectedRows()
menu = QMenu()
menu = QMenu(self)
menu.addSection("Actions")
view_action = menu.addAction(_("View in Libby"))
view_action.triggered.connect(lambda: self.open_loan_in_libby(indices))
return_action = menu.addAction(_("Return %d selected loan(s)") % len(indices))
return_action.triggered.connect(lambda: self.return_selection(indices))
menu.exec(QCursor.pos())

def open_loan_in_libby(self, indices):
for index in indices:
loan = index.data(Qt.UserRole)
library_key = next(
iter(
[
c["advantageKey"]
for c in self.model._cards
if c["cardId"] == loan["cardId"]
]
),
"",
)
QDesktopServices.openUrl(
QUrl(
f'https://libbyapp.com/library/{library_key}/everything/page-1/{loan["id"]}'
)
)

def return_selection(self, indices):
for index in reversed(indices):
loan = index.data(Qt.UserRole)
Expand Down Expand Up @@ -483,18 +506,26 @@ class LibbyLoansModel(QAbstractTableModel):
column_count = len(column_headers)
filter_hide_books_already_in_library = False

def __init__(self, parent, loans=[], db=None):
def __init__(self, parent, synced_state=None, db=None):
super().__init__(parent)
self.db = db
self._loans = sorted(loans, key=lambda ln: ln["checkoutDate"], reverse=True)
self._cards = []
self._loans = []
self.filtered_loans = []
self.filter_hide_books_already_in_library = PREFS[
PreferenceKeys.HIDE_BOOKS_ALREADY_IN_LIB
]
self.filter_loans()

def refresh_loans(self, loans=[]):
self._loans = sorted(loans, key=lambda ln: ln["checkoutDate"], reverse=True)
self.refresh_loans(synced_state)

def refresh_loans(self, synced_state=None):
if not synced_state:
synced_state = {}
self._cards = synced_state.get("cards", [])
self._loans = sorted(
synced_state.get("loans", []),
key=lambda ln: ln["checkoutDate"],
reverse=True,
)
self.filter_loans()

def filter_loans(self):
Expand Down
48 changes: 26 additions & 22 deletions calibre-plugin/translations/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2023-07-03 18:37+0800\n"
"POT-Creation-Date: 2023-07-03 19:45+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand All @@ -19,75 +19,79 @@ msgstr ""
msgid "Import loans from your OverDrive Libby account"
msgstr ""

#: calibre-plugin/action.py:61
#: calibre-plugin/action.py:63
msgid "Run the OverDrive Libby client UI"
msgstr ""

#: calibre-plugin/action.py:152
#: calibre-plugin/action.py:138
msgid "OverDrive Libby v%s"
msgstr ""

#: calibre-plugin/action.py:157
#: calibre-plugin/action.py:143
msgid "Refresh"
msgstr ""

#: calibre-plugin/action.py:159
#: calibre-plugin/action.py:145
msgid "Get latest loans"
msgstr ""

#: calibre-plugin/action.py:195
#: calibre-plugin/action.py:181
msgid "Download"
msgstr ""

#: calibre-plugin/action.py:197
#: calibre-plugin/action.py:183
msgid "Download selected loans"
msgstr ""

#: calibre-plugin/action.py:275
msgid "Return %d selected loan(s)"
msgstr ""

#: calibre-plugin/action.py:293
#: calibre-plugin/action.py:261
msgid "Fetching loans..."
msgstr ""

#: calibre-plugin/action.py:327
#: calibre-plugin/action.py:295
msgid "Please select at least 1 loan."
msgstr ""

#: calibre-plugin/action.py:389 calibre-plugin/action.py:436
#: calibre-plugin/action.py:357 calibre-plugin/action.py:404
msgid "Downloading %s"
msgstr ""

#: calibre-plugin/action.py:466
#: calibre-plugin/action.py:440
msgid "View in Libby"
msgstr ""

#: calibre-plugin/action.py:442
msgid "Return %d selected loan(s)"
msgstr ""

#: calibre-plugin/action.py:473
msgid "Returning %s"
msgstr ""

#: calibre-plugin/action.py:485
#: calibre-plugin/action.py:492
msgid "Failed to return loan"
msgstr ""

#: calibre-plugin/action.py:488
#: calibre-plugin/action.py:495
msgid "finished"
msgstr ""

#: calibre-plugin/action.py:493
#: calibre-plugin/action.py:500
msgid "Title"
msgstr ""

#: calibre-plugin/action.py:494
#: calibre-plugin/action.py:501
msgid "Author"
msgstr ""

#: calibre-plugin/action.py:495
#: calibre-plugin/action.py:502
msgid "Checkout Date"
msgstr ""

#: calibre-plugin/action.py:496
#: calibre-plugin/action.py:503
msgid "Type"
msgstr ""

#: calibre-plugin/action.py:497
#: calibre-plugin/action.py:504
msgid "Format"
msgstr ""

Expand Down
50 changes: 27 additions & 23 deletions calibre-plugin/translations/ja.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2023-07-03 18:37+0800\n"
"PO-Revision-Date: 2023-07-03 18:43+0800\n"
"POT-Creation-Date: 2023-07-03 19:45+0800\n"
"PO-Revision-Date: 2023-07-03 19:47+0800\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: ja\n"
Expand All @@ -20,75 +20,79 @@ msgstr ""
msgid "Import loans from your OverDrive Libby account"
msgstr "OverDrive Libbyアカウントからローンをインポート"

#: calibre-plugin/action.py:61
#: calibre-plugin/action.py:63
msgid "Run the OverDrive Libby client UI"
msgstr "OverDrive LibbyクライアントUIを実行します"

#: calibre-plugin/action.py:152
#: calibre-plugin/action.py:138
msgid "OverDrive Libby v%s"
msgstr ""

#: calibre-plugin/action.py:157
#: calibre-plugin/action.py:143
msgid "Refresh"
msgstr "更新"

#: calibre-plugin/action.py:159
#: calibre-plugin/action.py:145
msgid "Get latest loans"
msgstr ""

#: calibre-plugin/action.py:195
#: calibre-plugin/action.py:181
msgid "Download"
msgstr "ダウンロード"

#: calibre-plugin/action.py:197
#: calibre-plugin/action.py:183
msgid "Download selected loans"
msgstr "選択した書籍ローンをダウンロードする"

#: calibre-plugin/action.py:275
msgid "Return %d selected loan(s)"
msgstr ""

#: calibre-plugin/action.py:293
#: calibre-plugin/action.py:261
msgid "Fetching loans..."
msgstr ""

#: calibre-plugin/action.py:327
#: calibre-plugin/action.py:295
msgid "Please select at least 1 loan."
msgstr ""

#: calibre-plugin/action.py:389 calibre-plugin/action.py:436
#: calibre-plugin/action.py:357 calibre-plugin/action.py:404
msgid "Downloading %s"
msgstr "%s をダウンロード中"

#: calibre-plugin/action.py:466
#: calibre-plugin/action.py:440
msgid "View in Libby"
msgstr ""

#: calibre-plugin/action.py:442
msgid "Return %d selected loan(s)"
msgstr ""

#: calibre-plugin/action.py:473
msgid "Returning %s"
msgstr ""

#: calibre-plugin/action.py:485
#: calibre-plugin/action.py:492
msgid "Failed to return loan"
msgstr ""

#: calibre-plugin/action.py:488
#: calibre-plugin/action.py:495
msgid "finished"
msgstr "完了"

#: calibre-plugin/action.py:493
#: calibre-plugin/action.py:500
msgid "Title"
msgstr "タイトル"

#: calibre-plugin/action.py:494
#: calibre-plugin/action.py:501
msgid "Author"
msgstr "著者"

#: calibre-plugin/action.py:495
#: calibre-plugin/action.py:502
msgid "Checkout Date"
msgstr ""

#: calibre-plugin/action.py:496
#: calibre-plugin/action.py:503
msgid "Type"
msgstr "タイプ"

#: calibre-plugin/action.py:497
#: calibre-plugin/action.py:504
msgid "Format"
msgstr "形式"

Expand Down
Loading

0 comments on commit 7f321be

Please sign in to comment.