Featured image of post 【Java】SiYuan 实现 Epub 阅读

【Java】SiYuan 实现 Epub 阅读

这是简介

先短暂的解决一下阅读需求,写一个简单的 Java 推送 SiYuan 程序,实现起来也非常简单。

事先准备好 SiYuan 的 Token

Maven 依赖

	<!--  电子书解析器  -->
        <dependency>
            <groupId>com.positiondev.epublib</groupId>
            <artifactId>epublib-core</artifactId>
            <version>${epublib.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-simple</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- HTML解析 -->
        <dependency>
            <!-- jsoup HTML parser library @ https://jsoup.org/ -->
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>
	<!-- HTML 转 Markdown -->
        <dependency>
            <groupId>com.kotcrab.remark</groupId>
            <artifactId>remark</artifactId>
            <version>1.0.0</version>
        </dependency>

如果启动时候日志依赖冲突,就把 epublib​中的日志模块剔除掉

在自己的随便一个 Spring 项目中添加一个接口

public void post2Siyuan(String epubPath) {
	File file = new File(epubPath);
        InputStream in = null;
        try {
            //从输入流当中读取epub格式文件
            EpubReader reader = new EpubReader();
            in = new FileInputStream(file);
            Book book = reader.readEpub(in);
            //获取到书本的头部信息
            Metadata metadata = book.getMetadata();
            //获取到书本的全部资源
            Resources resources = book.getResources();
            //获取到书本的内容资源(正文)
            List<Resource> contents = book.getContents();
            String bookName = metadata.getFirstTitle();
            String author = metadata.getAuthors().get(0).toString().replace(",","");
            StringBuilder chapterContent = new StringBuilder();
            String markdown;
            //contents.remove(0);
            for (Resource content : contents) {
                chapterContent.append(new String(content.getData(), StandardCharsets.UTF_8));
            }
            markdown = new Remark().convertFragment(chapterContent.toString());
            Map<String, Object> siyuanSendData = new HashMap<>();
            siyuanSendData.put("notebook", "笔记本ID");
            siyuanSendData.put("path", "/"+ bookName + " - " + author);
            siyuanSendData.put("markdown", markdown);
            String siyuanResp = HttpRequest.post(SIYUAN_URL + SIYUAN_API_CREATE_DOC)
                    .contentType("application/json", "UTF-8")
                    .authorization(SIYUAN_TOKEN)
                    .send(JSON.toJSONString(siyuanSendData))
                    .body();
            System.out.println(siyuanResp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //一定要关闭资源
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

大功告成