{"id":262,"date":"2021-12-18T15:24:33","date_gmt":"2021-12-18T07:24:33","guid":{"rendered":"https:\/\/fugary.com\/?p=262"},"modified":"2021-12-18T16:14:45","modified_gmt":"2021-12-18T08:14:45","slug":"calibre%e6%8f%92%e4%bb%b6%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=262","title":{"rendered":"Calibre\u63d2\u4ef6\u5f00\u53d1"},"content":{"rendered":"<h3>Calibre\u63d2\u4ef6\u5f00\u53d1<\/h3>\n<p>\u200b   \u6700\u8fd1\u5728\u4f7f\u7528calibre-web\u7ba1\u7406\u7535\u5b50\u4e66\uff0c\u4e0d\u8fc7\u5f88\u591a\u65f6\u5019\u8fd8\u662f\u9700\u8981\u7528\u5230Calibre\u684c\u9762\u7248\u8f6f\u4ef6\uff0c\u6279\u91cf\u7ba1\u7406\uff0c\u7f16\u8f91\u7535\u5b50\u4e66\u7b49\u529f\u80fd\uff0c\u5728calibre-web\u4e0a\u5df2\u7ecf\u4f7f\u7528calibre-web-douban-api\u641c\u7d20\u8c46\u74e3\u5143\u6570\u636e\uff0c\u4f46\u662f\u684c\u9762\u7248Calibre\u8f6f\u4ef6\u7f3a\u6ca1\u6709\u529e\u6cd5\u4f7f\u7528\uff0c\u4e0d\u8fc7calibre\u53ef\u4ee5\u4f7f\u7528\u63d2\u4ef6\uff0c\u800c\u4e14\u662f\u4f7f\u7528python\u5f00\u53d1\uff0c\u56e0\u6b64\u53ef\u4ee5\u628acalibre-web-douban-api\u6539\u9020\u4e00\u4e0b\u5305\u88c5\u6210calibre\u63d2\u4ef6\uff0c\u7b80\u5355\u5143\u6570\u636e\u63d2\u4ef6\u8fd8\u662f\u6bd4\u8f83\u5bb9\u6613\u7684<\/p>\n<h3>\u53c2\u8003\u6587\u6863<\/h3>\n<p><a href=\"https:\/\/manual.calibre-ebook.com\/creating_plugins.html#pluginstutorial\">https:\/\/manual.calibre-ebook.com\/creating_plugins.html#pluginstutorial<\/a><\/p>\n<p>\u9996\u5148\u672c\u5730\u5df2\u7ecf\u5b89\u88c5\u4e86calibre\u8f6f\u4ef6<\/p>\n<h3>\u57fa\u672c\u63d2\u4ef6\u5f00\u53d1<\/h3>\n<h4>\u5f00\u53d1HelloWorld\u63d2\u4ef6<\/h4>\n<p>\u65b0\u5efaPython\u5de5\u7a0b\u6587\u4ef6\u5939<\/p>\n<p>\u65b0\u5efa<code>__init__.py<\/code>\uff0c\u540d\u5b57\u5fc5\u987b\u662f\u8fd9\u4e2a\u3002<\/p>\n<p>\u53c2\u8003HelloWorld\uff0c\u8fd9\u4e2a\u63d2\u4ef6\u662f\u6587\u4ef6\u7c7b\u578b\u63d2\u4ef6\uff0c\u7ee7\u627fFileTypePlugin\uff1a<\/p>\n<pre><code class=\"language-python\">from calibre.customize import FileTypePlugin\n\nclass HelloWorld(FileTypePlugin):\n\n    name                = &#039;Hello World Plugin&#039; # Name of the plugin\n    description         = &#039;Set the publisher to Hello World for all new conversions&#039;\n    supported_platforms = [&#039;windows&#039;, &#039;osx&#039;, &#039;linux&#039;] # Platforms this plugin will run on\n    author              = &#039;Acme Inc.&#039; # The author of this plugin\n    version             = (1, 0, 0)   # The version number of this plugin\n    file_types          = set([&#039;epub&#039;, &#039;mobi&#039;]) # The file types that this plugin will be applied to\n    on_postprocess      = True # Run this plugin after conversion is complete\n    minimum_calibre_version = (0, 7, 53)\n\n    def run(self, path_to_ebook):\n        from calibre.ebooks.metadata.meta import get_metadata, set_metadata\n        with open(path_to_ebook, &#039;r+b&#039;) as file:\n            ext  = os.path.splitext(path_to_ebook)[-1][1:].lower()\n            mi = get_metadata(file, ext)\n            mi.publisher = &#039;Hello World&#039;\n            set_metadata(file, mi, ext)\n        return path_to_ebook<\/code><\/pre>\n<h4>\u547d\u4ee4\u5b89\u88c5\u63d2\u4ef6<\/h4>\n<p>\u5728\u521a\u624d\u5de5\u7a0b\u76ee\u5f55\u4e0b\u9762\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\uff1a<\/p>\n<pre><code class=\"language-sh\">calibre-customize -b .\n# \u8f93\u51fa\uff1aPlugin updated: Hello World Plugin (1, 0, 0)<\/code><\/pre>\n<p>\u7136\u540e\u5230calibre\u8f6f\u4ef6\u4e2d\u67e5\u770b\uff1a<\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/fugary\/blogpic1\/-\/raw\/master\/images\/202112152047524.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/fugary\/blogpic1\/-\/raw\/master\/images\/202112152047524.png\" alt=\"image-20211215204715335\" \/><\/div><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u63d2\u4ef6\u5df2\u7ecf\u5b89\u88c5\u5230calibre\u4e2d\u4e86\u3002<\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/fugary\/blogpic1\/-\/raw\/master\/images\/202112152100207.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/fugary\/blogpic1\/-\/raw\/master\/images\/202112152100207.png\" alt=\"image-20211215210055084\" \/><\/div><\/p>\n<p>\u8fd9\u4e2a\u63d2\u4ef6\u662f\u5728\u8f6c\u6362\u683c\u5f0f\u7684\u65f6\u5019\u4f7f\u7528\u5230\u3002<\/p>\n<h3>\u5f00\u53d1\u5143\u6570\u636e\u63d2\u4ef6<\/h3>\n<p>\u73b0\u5728\u53ef\u4ee5\u5f00\u59cb\u5f00\u53d1\u5143\u6570\u636e\u63d2\u4ef6\u4e86\u3002<\/p>\n<p>\u53ef\u4ee5\u5148\u4e0b\u8f7dcalibre\u7684\u6e90\u7801\uff0c\u7136\u540e\u53c2\u8003\u91cc\u9762\u7684\u5176\u4ed6\u5143\u6570\u636e\u63d2\u4ef6\u5f00\u53d1\uff1a<\/p>\n<p>calibre\u7684\u5f00\u6e90\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/kovidgoyal\/calibre\">https:\/\/github.com\/kovidgoyal\/calibre<\/a><\/p>\n<p>\u4e5f\u53ef\u4ee5\u53c2\u8003\u5176\u4ed6\u4eba\u5f00\u53d1\u7684\u8c46\u74e3\u63d2\u4ef6\uff1a<a href=\"https:\/\/github.com\/jnozsc\/calibre-douban\">https:\/\/github.com\/jnozsc\/calibre-douban<\/a><\/p>\n<h4>\u6539\u9020\u6210\u5143\u6570\u636e<\/h4>\n<p>\u73b0\u5728\u53ef\u4ee5\u6539\u9020HelloWorld\u63d2\u4ef6\uff0c\u5143\u6570\u636e\u63d2\u4ef6\u9700\u8981\u7ee7\u627f\u3002<\/p>\n<p><code>calibre.ebooks.metadata.sources.base.Source<\/code>\uff0c\u4e3b\u8981\u9700\u8981\u5b9e\u73b0<code>identity<\/code>\u548c<code>download_cover<\/code>\u65b9\u6cd5<\/p>\n<p>\u7b80\u5355\u5f00\u53d1\u51fa\u57fa\u672c\u7ed3\u6784\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\nfrom calibre.ebooks.metadata.book.base import Metadata\nfrom calibre.ebooks.metadata.sources.base import Source, Option\nclass NewDouban(Source):\n    name = &#039;New Test Books&#039;  # Name of the plugin\n    description = &#039;Downloads metadata and covers from Douban Books web site.&#039;\n    supported_platforms = [&#039;windows&#039;, &#039;osx&#039;, &#039;linux&#039;]  # Platforms this plugin will run on\n    author = &#039;Gary Fu&#039;  # The author of this plugin\n    version = (1, 0, 0)  # The version number of this plugin\n    minimum_calibre_version = (5, 0, 0)\n    capabilities = frozenset([&#039;identify&#039;, &#039;cover&#039;])\n    touched_fields = frozenset([\n        &#039;title&#039;, &#039;authors&#039;, &#039;tags&#039;, &#039;pubdate&#039;, &#039;comments&#039;, &#039;publisher&#039;,\n        &#039;identifier:isbn&#039;, &#039;rating&#039;, &#039;identifier:new_douban&#039;\n    ])  # language currently disabled\n    options = (\n    )\n    def identify(\n            self,\n            log,\n            result_queue,\n            abort,\n            title=None,\n            authors=None,  # {{{\n            identifiers={},\n            timeout=30):\n        result_queue.put(get_test_book())\ndef get_test_book():\n    book = Metadata(&#039;\u6df1\u5165\u7406\u89e3\u8ba1\u7b97\u673a\u7cfb\u7edf\uff08\u539f\u4e66\u7b2c3\u7248\uff09&#039;, [&#039;\u9f9a\u5955\u5229&#039;, &#039;\u8d3a\u83b2&#039;])\n    book.identifiers = {&#039;new_douban&#039;: &#039;26912767&#039;}\n    book.pubdate = datetime.date(2016, 11, 1)\n    book.publisher = &#039;\u673a\u68b0\u5de5\u4e1a\u51fa\u7248\u793e&#039;\n    return book\n\nif __name__ == &quot;__main__&quot;:\n    # To run these test use: calibre-debug -e .\/__init__.py\n    from calibre.ebooks.metadata.sources.test import (\n        test_identify_plugin, title_test, authors_test\n    )\n    test_identify_plugin(\n        NewDouban.name, [\n            ({\n                 &#039;identifiers&#039;: {\n                     &#039;isbn&#039;: &#039;9787111544937&#039;\n                 },\n                 &#039;title&#039;: &#039;\u6df1\u5165\u7406\u89e3\u8ba1\u7b97\u673a\u7cfb\u7edf\uff08\u539f\u4e66\u7b2c3\u7248\uff09&#039;\n             }, [title_test(&#039;\u6df1\u5165\u7406\u89e3\u8ba1\u7b97\u673a\u7cfb\u7edf\uff08\u539f\u4e66\u7b2c3\u7248\uff09&#039;, exact=True),\n                 authors_test([&#039;\u9f9a\u5955\u5229&#039;, &#039;\u8d3a\u83b2&#039;])])\n        ]\n    )\n<\/code><\/pre>\n<p>\u63d2\u4ef6\u672c\u5730\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"language-sh\"># \u63d2\u4ef6\u5b9a\u4e49\u5b89\u88c5\ncalibre-customize -b .\n# \u5982\u679c\u6709\u6d4b\u8bd5\u4ee3\u7801\uff0c\u8fd9\u4e2a\u662f\u53ef\u4ee5\u8c03\u7528\u6d4b\u8bd5\ncalibre-debug -e __init__.py<\/code><\/pre>\n<h4>\u5b8c\u5584\u63d2\u4ef6\u5177\u4f53\u4ee3\u7801<\/h4>\n<p>\u53c2\u8003calibre-web-douban-api\u7684\u4ee3\u7801\uff0c\u6539\u9020\u6210calibre\u63d2\u4ef6\uff0c\u5177\u4f53\u6e90\u4ee3\u7801\u89c1GitHub\u5f00\u6e90\uff1a<\/p>\n<p>\u5f00\u6e90\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/fugary\/calibre-douban\">https:\/\/github.com\/fugary\/calibre-douban<\/a><\/p>\n<h3>\u6253\u5305\u53d1\u5e03<\/h3>\n<p>calibre\u63d2\u4ef6\u5c31\u662f\u4e00\u4e2azip\u5305\uff0c\u53ef\u4ee5\u76f4\u63a5\u628a<code>__init__.py<\/code>\u6253\u5305\u6210zip\u5373\u53ef\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u5de5\u5177<code>build.py<\/code><\/p>\n<pre><code class=\"language-python\">import os\nimport shutil\nimport zipfile\ndef zip_dir(input_path, output_file):\n    output_zip = zipfile.ZipFile(output_file, &quot;w&quot;, zipfile.ZIP_DEFLATED)\n    for path, dir_names, file_names in os.walk(input_path):\n        # \u539f\u8def\u5f84\u4fee\u590d: .\/src\/test -&gt; \/test\n        parsed_path = path.replace(input_path, &#039;&#039;)\n        for filename in file_names:\n            full_path = os.path.join(path, filename)\n            print(&#039;zip adding file %s&#039; % full_path)\n            # \u6587\u4ef6\u8def\u5f84\uff0c\u538b\u7f29\u8def\u5f84\n            output_zip.write(full_path, os.path.join(parsed_path, filename))\n    output_zip.close()\nif __name__ == &quot;__main__&quot;:\n    input_path = &quot;src&quot;\n    out_path = &quot;out&quot;\n    output_file = out_path + &quot;\/NewDouban.zip&quot;\n    if os.path.exists(out_path):\n        print(&#039;clean path %s&#039; % out_path)\n        shutil.rmtree(out_path)\n    os.mkdir(out_path)\n    zip_dir(input_path, output_file)\n<\/code><\/pre>\n<p>\u53d1\u5e03\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/fugary\/calibre-douban\/releases\">https:\/\/github.com\/fugary\/calibre-douban\/releases<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calibre\u63d2\u4ef6\u5f00\u53d1 \u200b \u6700\u8fd1\u5728\u4f7f\u7528calibre-web\u7ba1\u7406\u7535\u5b50\u4e66\uff0c\u4e0d\u8fc7\u5f88\u591a\u65f6\u5019\u8fd8\u662f\u9700\u8981\u7528\u5230Calibre [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[43,46],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/262"}],"collection":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=262"}],"version-history":[{"count":2,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/262\/revisions"}],"predecessor-version":[{"id":267,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/262\/revisions\/267"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}