View Single Post
Old 04-23-2015, 03:27 AM   #11
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,471
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by BetterRed View Post
My utility is written in C (because that's what I know best), but the same thing could be done in a script in two shakes of a lambs tail - but I loathe scripting languages.
You could do it in python as a stand-alone program using the calibre API, writing the columns directly and avoiding the import.

Here is a skeleton program that does the job.
Code:
def init_cache(library_path):
	from calibre.db.backend import DB
	from calibre.db.cache import Cache
	backend = DB(library_path)
	cache = Cache(backend)
	cache.init()
	return cache

# UNDEFINED_DATE is the smallest date that can appear in a calibre library
from calibre.utils.date import UNDEFINED_DATE
	
cache = init_cache(library_path = sys.argv[1])

formats_dict = {}
formats_modtimes_dict = {}

# Loop through all the books in the library
for id_ in cache.all_book_ids():
	latest_format = None
	latest_modtime = UNDEFINED_DATE
	
	# Loop through all the formats for the book, finding the newest one.
	formats = cache.field_for('formats', id_)
	for format in formats:
		format_data = cache.format_metadata(id_, format)
		if format_data['mtime'] > latest_modtime:
			# Format is newer
			latest_modtime = format_data['mtime']
			latest_format = format
	# If we found a format, save it in the result dict so we can 
	# write the column later
	if latest_format is not None:
		formats_dict[id_] = latest_format
		formats_modtimes_dict[id_] = latest_modtime
		
# Print the results for test.
#for id_ in formats_dict:
#	print formats_dict[id_], formats_modtimes_dict[id_]

# Write the results to the db
cache.set_field('#your_text_col_name', formats_dict)
cache.set_field('#your_date_col_name', formats_modtimes_dict)
If you name it modtimes.py then you would run it as
Code:
calibre-debug -e modtimes.py /path/to/library
chaley is offline   Reply With Quote