summaryrefslogtreecommitdiffstats
path: root/gather.py
diff options
context:
space:
mode:
Diffstat (limited to 'gather.py')
-rwxr-xr-xgather.py35
1 files changed, 5 insertions, 30 deletions
diff --git a/gather.py b/gather.py
index 05f35f4..795c409 100755
--- a/gather.py
+++ b/gather.py
@@ -40,6 +40,7 @@ class Borrow(Base):
__tablename__ = "borrows"
id = Column(Integer, primary_key=True, nullable=False, doc="id in transaction element of acsm or in filename of acsm on http")
isbn = Column(ForeignKey("books.isbn"), nullable=False, doc="foreign key that leads to a book")
+ transaction = Column(String, nullable=True, doc="transaction element content, but only if it couldn't be derived from format ACS-BIBL-L-{acsm_id}, otherwise Null")
purchase = Column(String, nullable=True, doc="acsm purchase element: iso8601 of purchase of book, including timezone")
expiration = Column(String, nullable=True, doc="acsm expiration element: iso8601 of expiration of acsm, including timezone")
obtained = Column(BigInteger, nullable=False, doc="UNIX timestamp when this borrow was obtained as acsm from http")
@@ -117,9 +118,11 @@ try:
except FeatureNotFound:
raise FeatureNotFound("pip3 install lxml")
ft = acsm.fulfillmentToken
+ transaction = None
expected = f"ACS-BIBL-L-{acsm_id}"
if ft.transaction.string != expected:
- raise ValueError(f"expected {expected} in transaction.string, but instead received {ft.transaction.string} in acsm {acsm_id}")
+ transaction = ft.transaction.string
+ logger.info(f"expected {expected} in transaction.string, but instead received {ft.transaction.string} in acsm {acsm_id}")
isbn = int(ft.resourceItemInfo.resource.string.split("-").pop())+int(9e12)
identifier_is_isbn = True
identifier_to_isbn = 0
@@ -169,7 +172,7 @@ try:
book.thumbnail_extension = thumbnail_extension
book.language = language
book.format = format
- borrow = Borrow(id=acsm_id, isbn=isbn, purchase=purchase, expiration=expiration, obtained=int(time()), book=book)
+ borrow = Borrow(id=acsm_id, isbn=isbn, purchase=purchase, expiration=expiration, obtained=int(time()), book=book, transaction=transaction)
logger.info(f"found a new {borrow!r}")
session.add(borrow)
session.commit()
@@ -178,31 +181,3 @@ except KeyboardInterrupt:
logger.warning(f"Keyboard interrupt. Exiting. I hope this terminated cleanly. Last requested acsm was discarded.")
logger.info(f"In this session, {valid_acsms} valid acsms were stored, {only_isbn_acsms} acsms had only isbn and no other data available and {failed_acsms} acsms failed to be received with response code 200 and {failed_acsms_not200} acsms failed to be received but did not return 200. Last valid requested acsm was {acsm_id}. Thank you for cooperation.")
-
-"""
-metadata = MetaData()
-books = Table(
- "books",
- metadata,
- Column("title", String, nullable=False, doc="title of the book, dcc:title in acsm"),
- Column("creator", String, nullable=False, doc="author of the book, dc:creator in acsm"),
- Column("publisher", String, nullable=False, doc="publisher of the book, dc:publisher in acsm")
-)
-borrows = Table(
- "borrows",
- metadata,
- Column("id", Integer, primary_key=True, nullable=False, doc="id in transaction element of acsm or in filename of acsm on http"),
- Column("isbn", ForeignKey(books.c.isbn), nullable=False, doc="foreign key that leads to a book"),
- Column("purchase", String, nullable=False, doc="acsm purchase element: iso8601 of purchase of book, including timezone"),
- Column("expiration", String, nullable=False, doc="acsm expiration element: iso8601 of expiration of acsm, including timezone"),
- Column("duration", String, nullable=False, doc="acsm duration element, specifying borrow time of the book")
-)
-metadata.create_all(engine)
-"""
-
-"""
-with sqlalchemy.orm.Session(engine) as session:
- result = session.execute(sqlalchemy.text("CREATE TABLE IF NOT EXISTS borrows (id INT PRIMARY KEY, isbn INT NOT NULL, purchase TEXT NOT NULL, expiration TEXT NOT NULL, title TEXT NOT NULL, creator TEXT NOT NULL, publisher TEXT NOT NULL, duration INT NOT NULL, hmac BLOB NOT NULL) STRICT"))
- session.commit()
-# cur.execute("CREATE TABLE IF NOT EXISTS borrows(id INT PRIMARY KEY, isbn INT NOT NULL, purchase TEXT NOT NULL, expiration TEXT NOT NULL, title TEXT NOT NULL, creator TEXT NOT NULL, publisher TEXT NOT NULL, duration INT NOT NULL, hmac BLOB NOT NULL) STRICT")
-"""