"""Добавляет книгу на полку «Полина» по id или названию.""" import datetime import sqlite3 import sys APP = "/opt/books/config/app.db" LIB = "/opt/books/library/metadata.db" SHELF = "Полина" target = sys.argv[1] if len(sys.argv) > 1 else "" if not target: print("укажите название или id книги") raise SystemExit(1) lib = sqlite3.connect(LIB) if target.isdigit(): row = lib.execute("select id,title from books where id=?", (int(target),)).fetchone() else: row = lib.execute("select id,title from books where title like ?", (f"%{target}%",)).fetchone() if not row: print("книга не найдена в библиотеке") raise SystemExit(1) book_id, title = row app = sqlite3.connect(APP) shelf = app.execute("select id from shelf where name=?", (SHELF,)).fetchone() if shelf: shelf_id = shelf[0] else: import uuid now = datetime.datetime.now(datetime.UTC).isoformat() cur = app.execute( "insert into shelf (uuid,name,is_public,user_id,kobo_sync,created,last_modified)" " values (?,?,?,?,?,?,?)", (str(uuid.uuid4()), SHELF, 0, 3, 0, now, now)) shelf_id = cur.lastrowid print("создана полка:", shelf_id) if app.execute("select 1 from book_shelf_link where book_id=? and shelf=?", (book_id, shelf_id)).fetchone(): print("уже на полке:", title) else: app.execute('insert into book_shelf_link (book_id,"order",shelf,date_added)' " values (?,?,?,?)", (book_id, 0, shelf_id, datetime.datetime.now(datetime.UTC).isoformat())) app.commit() print(f"добавлено на полку «{SHELF}»: {title}")