Skip to content

Commit

Permalink
fix(sql): make date parsing more robust with a regex
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfikl committed Aug 2, 2023
1 parent f8c1e43 commit 20a50eb
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions papis_zotero/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
# FIXME: this should be handled by papis
ZOTERO_TAG_DELIMITER = " "

# fuzzy date matching
ISO_DATE_RE = re.compile(r"(?P<year>\d{4})-?(?P<month>\d{2})?-?(?P<day>\d{2})?")


ZOTERO_QUERY_ITEM_FIELD = """
SELECT
Expand Down Expand Up @@ -57,14 +60,14 @@ def get_fields(connection: sqlite3.Connection, item_id: str) -> Dict[str, str]:
# get year and month from date if available
date = fields.pop("date", None)
if date is not None:
from datetime import datetime

try:
d = datetime.strptime(date.split(" ")[0][:-3], "%Y-%m")
fields["year"] = d.year
fields["month"] = d.month
except Exception as exc:
logger.error("Failed to parse date.", exc_info=exc)
m = ISO_DATE_RE.match(date)
if m:
if m.group("year"):
fields["year"] = int(m.group("year"))
if m.group("month"):
fields["month"] = int(m.group("month"))
else:
# NOTE: didn't manage to match, so just save the whole date
fields["date"] = date

return fields
Expand Down

0 comments on commit 20a50eb

Please sign in to comment.