Infer format in openlibrary import

This commit is contained in:
Mouse Reeve
2021-09-29 12:21:19 -07:00
parent 47706b5353
commit 123b23728f
4 changed files with 63 additions and 53 deletions

View File

@ -9,6 +9,7 @@ from requests.exceptions import RequestException
from bookwyrm import activitypub, models, settings
from .connector_manager import load_more_data, ConnectorException
from .format_mappings import format_mappings
logger = logging.getLogger(__name__)
@ -312,3 +313,16 @@ class Mapping:
return self.formatter(value)
except: # pylint: disable=bare-except
return None
def infer_physical_format(format_text):
""" try to figure out what the standardized format is from the free value """
format_text = format_text.lower()
if format_text in format_mappings:
# try a direct match
return format_mappings[format_text]
else:
# failing that, try substring
matches = [v for k, v in format_mappings.items() if k in format_text]
if not matches:
return None
return matches[0]