![]() |
#1 |
Enthusiast
![]() Posts: 41
Karma: 10
Join Date: Jun 2010
Location: Portland, OR
Device: M1 Max Mac Studio, iPad Pro 2018 12.9, iPhone 7
|
Help with non-standard characters in directory names
I have been working on a python program that retrieves information from my Calibre database and acts as a bookmarking tool for syncing my reading location across multiple devices I use to read multiple formats of ebooks, but that don't sync very reliably.
As part of this, I have accessed cover files within the Calibre library file structure. I am using macOS. In python, I am using the pillow library (from PIL import Image) to read the cover image, convert to a png thumbnail for my use. The problem is that characters like single and double quotes, commas, non-ascii characters, and so on mean that I cannot get to some of the directories Calibre creates. A good example is the recent book "Ring Shout" by P. Djèlí Clark--the non-standard characters in the author name mean that the PIL library calls using the path I create don't work. I have tediously fixed all the current problems by manipulating the author names and book titles in Calibre to remove or change the offending characters--not a good solution. Anyone know how can I manage this in the Python code? |
![]() |
![]() |
![]() |
#2 | |
Bibliophagist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 47,741
Karma: 172313956
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, Lenovo M8 FHD, Paperwhite 4, Tolino epos
|
Quote:
|
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,577
Karma: 28548962
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
If you want to manipulate the calibre library from a script use calibredb https://manual.calibre-ebook.com/gen...calibredb.html
If you want to use python you can run your python script with calibre-debug and then use calibre's python apis instead of calibredb. See the manual for documentation of the database apis. |
![]() |
![]() |
![]() |
#4 |
Enthusiast
![]() Posts: 41
Karma: 10
Join Date: Jun 2010
Location: Portland, OR
Device: M1 Max Mac Studio, iPad Pro 2018 12.9, iPhone 7
|
Thanks for the replies.
I do use calibredb, called from python. I use 'list -f all --for-machine' as options to get the entire calibre database as json. Then I create the path to the cover file for a book by concatenating the library path on my Mac with the 'authors', 'title', 'id' fields taken directly from the json data. But when I do so, the path created has characters in it that result in a "file not found" error when I use the path to call Image.open(): Code:
im = Image.open(calibrecoverpath) I manipulate (both read and write) the calibre database using calibredb from within my program in many other ways. In this case, I just want to grab the cover file so I can display it with my program when I show reading stats on a book. I don't see a way to directly get the path to the cover file from calibredb, so I use the individual fields (author, title) to create the path. What should I do instead? |
![]() |
![]() |
![]() |
#5 |
Enthusiast
![]() Posts: 41
Karma: 10
Join Date: Jun 2010
Location: Portland, OR
Device: M1 Max Mac Studio, iPad Pro 2018 12.9, iPhone 7
|
By the way, I will look into calibre-debug . . .
|
![]() |
![]() |
Advert | |
|
![]() |
#6 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 7,140
Karma: 92500001
Join Date: Nov 2011
Location: Charlottesville, VA
Device: Kindles
|
Quote:
The "cover" field has the correct full path to the cover file. |
|
![]() |
![]() |
![]() |
#7 |
Enthusiast
![]() Posts: 28
Karma: 10
Join Date: Jan 2019
Device: none
|
As has been pointed out, calibre sanitizes the title/author when creating directories as not all filesystems support all characters. Since the json returned by calibredb doesn't not provide the actual path, the method I use in my python scripts is walk the entire library and identify the books by id. See the attached python function I use.
|
![]() |
![]() |
![]() |
#8 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,577
Karma: 28548962
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
And the formats field has the paths to individual book files.
|
![]() |
![]() |
![]() |
#9 |
Enthusiast
![]() Posts: 28
Karma: 10
Join Date: Jan 2019
Device: none
|
Code:
$ calibredb list -f formats,cover id formats cover 1 [PDF] 1 2 [PDF] 1 3 [EPUB] 1 $ calibredb list -f formats,cover --for-machine [ { "cover": 1, "formats": [ "PDF" ], }, |
![]() |
![]() |
![]() |
#10 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,577
Karma: 28548962
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Presumably you are on an old calibre version.
Code:
calibredb list -f formats,cover --for-machine -s id:2017 * [ { "cover": "/home/kovid/test library/calibre/The Hindu (2017)/cover.jpg", "formats": [ "/home/kovid/test library/calibre/The Hindu (2017)/The Hindu - calibre.mobi" ], "id": 2017 } ]% |
![]() |
![]() |
![]() |
#11 |
Enthusiast
![]() Posts: 28
Karma: 10
Join Date: Jan 2019
Device: none
|
Yes, I use the content server because I want to be able to use calibre while a script is running in the background.
|
![]() |
![]() |
![]() |
#12 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,577
Karma: 28548962
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
The content server does not respond with paths, as they are both meningless over a network and an information leak about the layout of files on the server.
|
![]() |
![]() |
![]() |
#13 |
Enthusiast
![]() Posts: 41
Karma: 10
Join Date: Jun 2010
Location: Portland, OR
Device: M1 Max Mac Studio, iPad Pro 2018 12.9, iPhone 7
|
Thanks, all, for the help with this. I, too, was using the content server, so the 'cover' field did not contain the path. By turning off the content server I can get exactly what I want.
|
![]() |
![]() |
![]() |
Thread Tools | Search this Thread |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Initials etc in author names, is there a standard for using period? | LadyKate | Library Management | 5 | 01-18-2014 03:05 AM |
DR1000 Unusual directory names | andre8x8 | iRex | 8 | 10-04-2011 09:28 AM |
Author sort for foreign or non-standard names. | Archon | Library Management | 7 | 02-12-2011 01:55 PM |
Non-standard characters | jbenny | Workshop | 6 | 11-18-2007 04:56 PM |