Skip to content

Commit

Permalink
re: slack convo, go ahead and import this and use get_author_config o…
Browse files Browse the repository at this point in the history
…ver hardcoded IDs
  • Loading branch information
pidgezero-one committed Dec 12, 2024
1 parent 82b198c commit 87d9f25
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion openlibrary/catalog/add_book/load_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def get_redirected_authors(authors: list["Author"]):
return authors

# Look for OL ID first.
if (key := author.get("key")) and (
if (key := author.get("ol_id")) and (
reply := list(web.ctx.site.things({"type": "/type/author", "key~": key}))
):
# Always match on OL ID, even if remote identifiers don't match.
Expand Down
48 changes: 26 additions & 22 deletions openlibrary/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
from openlibrary.core.ratings import Ratings
from openlibrary.core.vendors import get_amazon_metadata
from openlibrary.core.wikidata import WikidataEntity, get_wikidata_entity
from openlibrary.plugins.upstream.utils import get_author_config
from openlibrary.utils import extract_numeric_id_from_olid
from openlibrary.utils.isbn import canonical, isbn_13_to_isbn_10, to_isbn_13
from openlibrary.plugins.upstream.utils import get_author_config

from ..accounts import OpenLibraryAccount # noqa: F401 side effects may be needed
from ..plugins.upstream.utils import get_coverstore_public_url, get_coverstore_url
Expand Down Expand Up @@ -807,28 +807,32 @@ def get_edition_count(self):

def get_lists(self, limit=50, offset=0, sort=True):
return self._get_lists(limit=limit, offset=offset, sort=sort)

def merge_remote_ids(
self, incoming_ids: dict[str, str]
) -> tuple[dict[str, str], int]:
output = {**self.remote_ids}
if len(incoming_ids.items()) == 0:
return output, -1
matches = 0
conflicts = 0
for id in get_author_config():
identifier: str = id.name
if identifier in output and identifier in incoming_ids:
if output[identifier] != incoming_ids[identifier]:
conflicts = conflicts + 1
else:
output[identifier] = incoming_ids[identifier]
matches = matches + 1
if conflicts > matches:
# This means that the identifiers we already have for this author have too many conflicts with whichever identifiers we're trying to merge into it.
# TODO: Raise this to librarians, somehow.
return self.remote_ids, -1
return output, matches
self, incoming_ids: dict[str, str]
) -> tuple[dict[str, str], int]:
"""Returns the author's remote IDs merged with a given remote IDs object, as well as a count for how many IDs had conflicts.
If incoming_ids is empty, or if there are more conflicts than matches, no merge will be attempted, and the output will be (author.remote_ids, -1).
"""
output = {**self.remote_ids}
if len(incoming_ids.items()) == 0:
return output, -1
# Count
matches = 0
conflicts = 0
for id in get_author_config():
identifier: str = id.name
if identifier in output and identifier in incoming_ids:
if output[identifier] != incoming_ids[identifier]:
conflicts = conflicts + 1
else:
output[identifier] = incoming_ids[identifier]
matches = matches + 1
if conflicts > matches:
# TODO: Raise this to librarians, somehow.
return self.remote_ids, -1
return output, matches


class User(Thing):
DEFAULT_PREFERENCES = {
Expand Down

0 comments on commit 87d9f25

Please sign in to comment.