From 7aefee6d48011ed8dc0bde423755fb960453e8d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20H=C3=BCgel?= Date: Tue, 24 Dec 2024 14:02:01 +0000 Subject: [PATCH] Strip "/api" when parsing local API links --- src/pyzotero/zotero.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/pyzotero/zotero.py b/src/pyzotero/zotero.py index 844b956..450b7a0 100644 --- a/src/pyzotero/zotero.py +++ b/src/pyzotero/zotero.py @@ -24,8 +24,16 @@ import zipfile from collections import OrderedDict from functools import wraps -from pathlib import Path -from urllib.parse import parse_qs, parse_qsl, quote, urlencode, urlparse, urlunparse +from pathlib import Path, PurePosixPath +from urllib.parse import ( + parse_qs, + parse_qsl, + quote, + unquote, + urlencode, + urlparse, + urlunparse, +) import bibtexparser import feedparser @@ -272,8 +280,10 @@ def __init__( """Store Zotero credentials""" if not local: self.endpoint = "https://api.zotero.org" + self.local = False else: self.endpoint = "http://localhost:23119/api" + self.local = True if library_id and library_type: self.library_id = library_id # library_type determines whether query begins w. /users or /groups @@ -321,6 +331,15 @@ def __init__( self.backoff = False self.backoff_duration = 0.0 + def _striplocal(self, url): + if self.local: + parsed = urlparse(url) + purepath = PurePosixPath(unquote(parsed.path)) + newpath = "/".join(purepath.parts[2:]) + replaced = parsed._replace(path="/" + newpath) + return urlunparse(replaced) + return url + def _set_backoff(self, duration): """ Set a backoff @@ -845,10 +864,10 @@ def all_top(self, **kwargs): @retrieve def follow(self): """Return the result of the call to the URL in the 'Next' link""" - if self.links.get("next"): - return self.links.get("next") - else: - return + if n := self.links.get("next"): + newurl = self._striplocal(n) + return newurl + return def iterfollow(self): """Generator for self.follow()"""