Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 07-08-2021, 11:35 AM   #1
Terry Brown
Enthusiast
Terry Brown began at the beginning.
 
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?
Terry Brown is offline   Reply With Quote
Old 07-08-2021, 12:09 PM   #2
DNSB
Bibliophagist
DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.
 
DNSB's Avatar
 
Posts: 47,741
Karma: 172313956
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, Lenovo M8 FHD, Paperwhite 4, Tolino epos
Quote:
Originally Posted by Terry Brown View Post
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?
My suspicion is that calibre is already sanitizing (Latinizing??) the non-standard characters to create the filenames. You may have to duplicate this process when creating the file path.
DNSB is offline   Reply With Quote
Advert
Old 07-08-2021, 12:23 PM   #3
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
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.
kovidgoyal is offline   Reply With Quote
Old 07-08-2021, 05:11 PM   #4
Terry Brown
Enthusiast
Terry Brown began at the beginning.
 
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)
That's why I'm confused. The files on disk are accessible to me from Finder or the command line, so the names must be sanitized, but the data from calibredb that I get as json is apparently not.

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?
Terry Brown is offline   Reply With Quote
Old 07-08-2021, 05:12 PM   #5
Terry Brown
Enthusiast
Terry Brown began at the beginning.
 
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 . . .
Terry Brown is offline   Reply With Quote
Advert
Old 07-08-2021, 06:16 PM   #6
jhowell
Grand Sorcerer
jhowell ought to be getting tired of karma fortunes by now.jhowell ought to be getting tired of karma fortunes by now.jhowell ought to be getting tired of karma fortunes by now.jhowell ought to be getting tired of karma fortunes by now.jhowell ought to be getting tired of karma fortunes by now.jhowell ought to be getting tired of karma fortunes by now.jhowell ought to be getting tired of karma fortunes by now.jhowell ought to be getting tired of karma fortunes by now.jhowell ought to be getting tired of karma fortunes by now.jhowell ought to be getting tired of karma fortunes by now.jhowell ought to be getting tired of karma fortunes by now.
 
jhowell's Avatar
 
Posts: 7,140
Karma: 92500001
Join Date: Nov 2011
Location: Charlottesville, VA
Device: Kindles
Quote:
Originally Posted by Terry Brown View Post
...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.
As others have mentioned the path for the book may not exactly match the author and title fields because they may contain disallowed characters or need to be truncated.

The "cover" field has the correct full path to the cover file.
jhowell is offline   Reply With Quote
Old 07-08-2021, 08:46 PM   #7
qwertyuio
Enthusiast
qwertyuio began at the beginning.
 
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.
Attached Files
File Type: py traverse_calibre_library.py (486 Bytes, 86 views)
qwertyuio is offline   Reply With Quote
Old 07-08-2021, 10:42 PM   #8
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
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.
kovidgoyal is offline   Reply With Quote
Old 07-08-2021, 11:10 PM   #9
qwertyuio
Enthusiast
qwertyuio began at the beginning.
 
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"
    ],
  },
Is there an option I'm missing?
qwertyuio is offline   Reply With Quote
Old 07-08-2021, 11:18 PM   #10
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
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
  }
]%
And note that this wont work when connecting via the calibre content server.
kovidgoyal is offline   Reply With Quote
Old 07-08-2021, 11:24 PM   #11
qwertyuio
Enthusiast
qwertyuio began at the beginning.
 
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.
qwertyuio is offline   Reply With Quote
Old 07-08-2021, 11:29 PM   #12
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
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.
kovidgoyal is offline   Reply With Quote
Old 07-10-2021, 10:21 AM   #13
Terry Brown
Enthusiast
Terry Brown began at the beginning.
 
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.
Terry Brown is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
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


All times are GMT -4. The time now is 02:20 PM.


MobileRead.com is a privately owned, operated and funded community.