View Single Post
Old 07-23-2014, 02:32 PM   #15
Ackis
Member
Ackis began at the beginning.
 
Posts: 21
Karma: 10
Join Date: Sep 2013
Location: Canada
Device: Galaxy Tab 2 10.1, iPad 2
Quote:
Originally Posted by eschwartz View Post
Been there, done that. It gets better.

A general recommendation seems to be, use as few commands to get a job done. "for" can do quite a few interesting things on its own.
It took me a while to catch on to basename.

Happy scripting!

... trying to figure out how to parse calibredb list output...
Thanks. When I'm learning something new I tend to be as verbose as possible with the code and even the comments just to make it a bit easier for me to understand.

Quote:
Originally Posted by kovidgoyal View Post
Better, but it wont handle books that have multiple formats, for which there will be multiple entries inside the [] brackets.

And now that I think about it, there is actually a bug in calibredb list. It should not be separating multiple path by commas, as commas can occur in filenames. Now fixed: https://github.com/kovidgoyal/calibr...4e95f15101ec6c
Yay, at least something good came out of my mucking around.

Quote:
Originally Posted by kovidgoyal View Post
Since I was here in any case, I decided to add a --for-machine option to calibredb list that makes it easier to parse the output.

And here's a one liner (in python because I hate sh) that demonstrates how to get a list of all ids and formats for ever id in the library

Code:
calibredb list --for-machine -f formats | python2 -c "import sys; print '\n\n'.join('id:%s formats:%r' % (r.partition(chr(29))[0], r.partition(chr(29))[-1].split(chr(28))) for r in sys.stdin.read().split(chr(30)) if r.strip())"
And the help of --for-machine

Code:
  --for-machine         Generate output that is more suitable for machine
                        parsing. Book entries are separates using the ASCII
                        Record Separator character (30), individual fields in
                        an entry using the Group Separator (29) and individual
                        filenames using the File Separator (28)
Thank you for adding that in. Python is on my to-do list. :P
Ackis is offline   Reply With Quote