Use when the user asks about a Calibre ebook library or book collection: find books, search metadata or full text in EPUB/AZW3 files, or locate book paths via Calibre databases.
98
98%
Does it follow best practices?
Impact
99%
1.80xAverage score across 4 eval scenarios
Passed
No known issues
#!/usr/bin/env python3
import argparse
import json
import os
import sqlite3
import sys
def emit_error(message, code, return_code=2):
print(json.dumps({"error": message, "error_code": code}))
return return_code
def fetch_metadata(db_path, limit=200):
if not os.path.exists(db_path):
return emit_error(f"DB not found: {db_path}", "DB_NOT_FOUND", 2)
try:
conn = sqlite3.connect(db_path)
cur = conn.cursor()
cur.execute("SELECT COUNT(*) FROM books")
book_count = int(cur.fetchone()[0])
cur.execute("SELECT COUNT(*) FROM authors")
author_count = int(cur.fetchone()[0])
cur.execute("SELECT COUNT(*) FROM data")
format_row_count = int(cur.fetchone()[0])
cur.execute(
"""
SELECT format, COUNT(*)
FROM data
GROUP BY format
ORDER BY COUNT(*) DESC, format
"""
)
format_counts = {fmt: count for fmt, count in cur.fetchall()}
cur.execute(
"""
SELECT b.id, b.title, group_concat(a.name, ', ') as authors
FROM books b
LEFT JOIN books_authors_link bal ON bal.book = b.id
LEFT JOIN authors a ON a.id = bal.author
GROUP BY b.id
ORDER BY b.title
LIMIT ?
""",
(limit,),
)
sample_books = []
for row in cur.fetchall():
sample_books.append({
"id": int(row[0]),
"title": row[1],
"authors": row[2],
})
print(
json.dumps(
{
"book_count": book_count,
"author_count": author_count,
"format_row_count": format_row_count,
"format_counts": format_counts,
"sample_books": sample_books,
},
indent=2,
ensure_ascii=False,
)
)
return 0
except Exception as e:
return emit_error(str(e), "INSPECT_ERROR", 3)
if __name__ == '__main__':
p = argparse.ArgumentParser(description='Calibre metadata inspector (read-only).')
p.add_argument('--db-path', required=True, help='Path to metadata.db')
p.add_argument('--limit', type=int, default=200, help='Max number of books to fetch')
args = p.parse_args()
rc = fetch_metadata(args.db_path, args.limit)
sys.exit(rc)evals
scenario-1
scenario-2
scenario-3
scenario-4
references
sample-library
Arthur C. Clarke
Warp Drive Studies (993)
Bertrand Russell
The Problems of Philosophy (4)
The Problems of Philosophy (997)
Jules Verne
Leviathan Under the Pacific (998)
Twenty Thousand Leagues under the Sea (1)
Twenty Thousand Leagues under the Sea (999)
Karl Marx
The Communist Manifesto (6)
The Communist Manifesto (995)
The Prince of the Workers (994)
scripts