Calibre 插件开发

发布时间 2023-11-04 22:37:52作者: 图麻骨

需求背景

整理了一批民国图书资源,图书的基本信息都存在数据库里,因为图书都比较早,在豆瓣的api里搜索不到对应的元数据,所以需要开发一个插件,链接到我本地的数据库中,获取对应图书的元数据。

 

官方参考文档

https://manual.calibre-ebook.com/creating_plugins.html#id3

 

插件的基本结构和安装方法

以官方hello world为例:

1、新建Python工程文件夹;

2、创建Python文件“__init__.py”(必须是这个名字);

3、输入代码:

 1 from calibre.customize import FileTypePlugin
 2 
 3 class HelloWorld(FileTypePlugin):
 4 
 5     name                = 'Hello World Plugin' # Name of the plugin
 6     description         = 'Set the publisher to Hello World for all new conversions'
 7     supported_platforms = ['windows', 'osx', 'linux'] # Platforms this plugin will run on
 8     author              = 'Acme Inc.' # The author of this plugin
 9     version             = (1, 0, 0)   # The version number of this plugin
10     file_types          = {'epub', 'mobi'} # The file types that this plugin will be applied to
11     on_postprocess      = True # Run this plugin after conversion is complete
12     minimum_calibre_version = (0, 7, 53)
13 
14     def run(self, path_to_ebook):
15         from calibre.ebooks.metadata.meta import get_metadata, set_metadata
16         with open(path_to_ebook, 'r+b') as file:
17             ext  = os.path.splitext(path_to_ebook)[-1][1:].lower()
18             mi = get_metadata(file, ext)
19             mi.publisher = 'Hello World'
20             set_metadata(file, mi, ext)
21         return path_to_ebook

4、安装插件,在文件目录下打开终端,执行:

calibre-customize -b .

# 输出:Plugin updated: Hello World Plugin (1, 0, 0)

重启应用,插件已经安装完成。