From ec377fbc5c7d81fd015912bb57317a8bea1f0f7e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 18 Mar 2023 19:01:14 +0800 Subject: [PATCH] feat: support changing ASIN and CDE type --- src/main.java | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main.java b/src/main.java index cffbba3..92a3374 100644 --- a/src/main.java +++ b/src/main.java @@ -1,8 +1,6 @@ import mobimeta.*; import java.io.File; -import java.util.Arrays; -import java.util.Iterator; import java.util.List; class Main { @@ -110,9 +108,37 @@ class Main { } } -// private static void setASIN(MobiMeta meta, String asin) { -// -// } + private static void setASIN(MobiMeta meta, String asin) { + String encoding = meta.getCharacterEncoding(); + List exthList = meta.getEXTHRecords(); + boolean flag = false; + for (EXTHRecord rec : exthList) { + int recType = rec.getRecordType(); + if (recType == 113 || recType == 504) { + rec.setData(asin, encoding); + flag = true; + } + } + if (!flag) { // without `ASIN` field + exthList.add(new EXTHRecord(113, asin, encoding)); + exthList.add(new EXTHRecord(504, asin, encoding)); // `504` priority on kindle + } + } + + private static void setCDE(MobiMeta meta, String cde) { + String encoding = meta.getCharacterEncoding(); + List exthList = meta.getEXTHRecords(); + boolean flag = false; + for (EXTHRecord rec : exthList) { + if (rec.getRecordType() == 501) { + rec.setData(cde, encoding); + flag = true; + } + } + if (!flag) { // without `CDE type` field + exthList.add(new EXTHRecord(501, cde, encoding)); + } + } public static void main(String[] args) { @@ -126,6 +152,8 @@ class Main { // setBookName(meta, "生若栩栩"); // setAuthor(meta, "大叙"); // setISBN(meta, "666"); +// setASIN(meta, "23333"); +// setCDE(meta, "PDOC"); // saveFile(meta, new File("XXRS_new.azw3")); }