It's my first message, so hello everybody.
I've read this forum for almost two years now, and today I think I may have something interesting to say...
I wrote a little python script to make the iLiad show metadata and covers in contentlister for mobipocket files.
Basically it simply creates one directory for each file, it puts the book into it and it creates a jpg and a manifest file (if the mobi book includes the cover...)
The script invokes "mobi2mobi.exe" to retrieve metadata information from the file, so if I am breaking some licence, please tell me.
Without mobi2mobi the manifest will show defaults for author and title (no filename parsing, sorry).
Usage is simple (
mobi2dirs.py -h for help):
Just specify the path to the folder containing the mobipocket books (default: current) and give a name for the cover file (default ".c.jpg").
You can copy all the folders into the iLiad, and it will show covers, titles, and authors (it may not work in the default Mobipocket directory, so better to use another one).
Here it is:
Code:
import os, sys, shutil
from optparse import OptionParser
#error
class DirException(Exception):
pass
# writes a log file
def logwrite(strings):
logfile = open("log.txt", "a")
logfile.write(strings)
logfile.close()
# put a value between XML tags (no attributes)
def putintags(tag, value):
return("<" + tag + ">" + value + "</" + tag + ">")
def createmanifest(fname, imgname):
manifest = open("manifest.xml", "w")
manifest.write('<?xml version="1.0" encoding="utf-8"?>')
manifest.write("\n")
title = "Titolo"
author = "Autore"
# dirty trick for to get metadata (author & Title)
# Uses mobi2mobi.exe capturing the output
xx=os.popen("mobi2mobi.exe --savethumb " + "\"" + imgname + "\"" + " \"" + fname + "\"", "r")
#print xx
for xline in xx:
if xline.rfind("EXTH item: 100 - Author") != -1:
author = xline[34:len(xline)-1]
#print xline
print author
if xline.rfind("LONGTITLE:") != -1:
title = xline[11:len(xline)-1]
#print xline
print title
xx.close()
#DC Metadata (include title & author)
dcmeta = putintags("Title", title) + putintags("Description", author) + putintags("Date", "2007-01-01T00:00:00")
dcmeta = putintags("dc-metadata", dcmeta)
#y-metadata
ymeta = putintags("image", imgname) + putintags("startpage", fname) + putintags("version", "000") + putintags("ItemSize", "000")
ymeta = putintags("y-metadata", ymeta)
#the long row
longrow = putintags("package", putintags("metadata", dcmeta + ymeta))
manifest.write(longrow + "\n")
manifest.close()
def createdirs(path, img):
a = 0
dirList=os.listdir(path)
for fname in dirList:
if fname.endswith(".prc") or fname.endswith(".mobi"):
a = a + 1
newname = str(a) + ".mobi"
os.rename(fname, newname)
logwrite("ren " + newname + " " + fname + "\n")
os.mkdir(fname)
dst = os.path.join(path, fname, fname)
os.rename(newname, dst)
shutil.copy("mobi2mobi.exe", os.path.join(path, fname, "mobi2mobi.exe"))
os.chdir(os.path.join(path, fname))
print os.getcwd()
createmanifest(fname, img)
os.remove("mobi2mobi.exe")
os.chdir("..");
# Main Function
print "\nMobi2Dir v0.01. Copyright (c) 2008 Nicola Giani"
print "mobi2mobi is part of Mobiperl, by tompe - https://dev.mobileread.com/trac/mobiperl/wiki\n\n"
# parse della linea di comando
parser = OptionParser(usage="mobi2dirs -p <path> -i <image name>")
parser.add_option("-p", "--pathname", dest="pathname", help="Path where mobi files are", metavar="PATH")
parser.add_option("-i", "--imgname", dest="imgname", help="Name of the cover image file (the same for every book)", metavar="IMG")
parser.set_defaults(pathname=os.getcwd())
parser.set_defaults(imgname=".c.jpg")
(options, args) = parser.parse_args()
# controllo degli argomenti e indicazione dei default
p = options.pathname
j = options.imgname
if not os.path.exists(p):
p = os.getcwd()
os.chdir(p)
createdirs(p, j)
It still need some polishing (and some comments, too, I think).
Since it uses mobi2mobi.exe it only works in DOS/Win boxes.
So it needs:
1. Platform independence
2. More metadata (like date, hardcoded for now)
3. More elegant scripting and error management
4. Some options for filename parsing
5. Other ideas?
Feel free to use it, change it, and, of course, improve it.
And if you have comments and suggestions, they are welcome.
PS: I tested the script on two Windows XP machines and two iliads. It works, but I cannot give warranties of any form.
So it's better to backup mobi files before usage.
And remember: don't write my nickname in the .bat file you use to invoke it.
PPS: it's easy to extend it to work with pdf also. I don't know why I didn't, but i will.