1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
const headers = { "Content-Type": "text/html; charset=utf-8", 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"', 'Sec-Fetch-Site': 'same-site', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-User': '?1', 'Sec-Fetch-Dest': 'document', 'Referer': 'https://m.douban.com/', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', }
async function douban(QuickAdd){ const user_input = await QuickAdd.quickAddApi.inputPrompt( "请输入书籍背后的13位ISBN码/10位IMDb/网页地址:" ); let simpleInfo={}; if(user_input.substring(0,4)!='http') { if(user_input.length!=13 && user_input.length!=10 && user_input.substring(0,4)!='http'){ new Notice("输入错误"); throw new Error("输入错误"); } simpleInfo =await getBookUrl(user_input); if(!simpleInfo){ new Notice("无法识别此ISBN码"); throw new Error("无法识别此ISBN码"); } } else { simpleInfo.title='数据'; simpleInfo.url = user_input; }
let url = simpleInfo.url; new Notice("准备获取《"+simpleInfo.title+"》的内容信息",1000) let bookInfo = await getDetailInfo(url) if(!bookInfo){ new Notice("获取内容失败"); throw new Error("获取内容失败"); } new Notice("笔记已生成!",500); const date = window.moment().format("gggg-MM-DD") bookInfo.today = date;
QuickAdd.variables = { ...bookInfo }; }
async function getBookUrl(isbn){ url = "https://m.douban.com/search/?query="+isbn; let searchUrl = new URL(url); const res = await request({ url: searchUrl.href, method: "GET", cache: "no-cache", headers: { "Content-Type": "text/html; charset=utf-8", 'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36' } });
if(!res){ return null; }
let p = new DOMParser(); let doc = p.parseFromString(res, "text/html"); let title = doc.querySelector("div.subject-info span").textContent; let detailUrl = String(doc.querySelector("ul li a").href).replace("app://obsidian.md","https://m.douban.com"); if (!detailUrl){ return null; } let simpleInfo={}; simpleInfo.title=title; simpleInfo.url = detailUrl; return simpleInfo; }
async function getDetailInfo(url){ let bookUrl = new URL(url); const res = await request({ url: bookUrl.href, method: "GET", cache: "no-cache", headers: headers }); let p = new DOMParser(); let doc = p.parseFromString(res, "text/html"); let $ = s => doc.querySelector(s); let $2 = z => doc.querySelectorAll(z);
let bookInfo = {}; let name = $("meta[property='og:title']")?.content; let title = "\""+name+"\""; let author = "\""+$("meta[property='book:author']")?.content.replace(/[\[\]\(\)()]/g,"")+"\""; let isbn = $("meta[property='book:isbn']")?.content; let cover = $("meta[property='og:image']")?.content; let text = $("#info")?.textContent.replace("\n",""); let transAuthor = text.match(/(?<=译者:\s*)\S+\s?\S+/g)?text.match(/(?<=译者:\s*)\S+\s?\S+/g)[0].trim():""; let originalName = text.match(/(?<=原作名:\s*)[\S ]+/g)?("\""+text.match(/(?<=原作名:\s*)[\S ]+/g)[0].trim()+"\""):""; let pages = text.match(/(?<=页数:\s*)[\S ]+/g)?text.match(/(?<=页数:\s*)[\S ]+/g)[0].trim():""; let publisher = text.match(/(?<=出版社:\s*)\S+\s?\S+/g)?text.match(/(?<=出版社:\s*)\S+\s?\S+/g)[0].trim():"";
let rating = $("div#interest_sectl div div strong")?.textContent.replace(/\s/g,"");
let intro = ""; let authorIntro = ""; var temp1 = $("h2"); if(temp1.innerText.includes("内容简介")){ var temp2 = temp1.nextElementSibling.querySelectorAll("div.intro") var temp3 = temp2[temp2.length-1].querySelectorAll("p"); for(var i=0;i<temp3.length;i++){ intro = intro+temp3[i].textContent+"\n"; } try{ temp2 = $2("h2")[1].nextElementSibling.querySelectorAll("div.intro"); temp3 = temp2[temp2.length-1].querySelectorAll("p"); for(var i=0;i<temp3.length;i++){ authorIntro = authorIntro+temp3[i].textContent+"\n"; } }catch(e){ new Notice("没有简介"); } }else if(temp1.innerText.includes("作者简介")){ var temp2 = temp1.nextElementSibling.querySelectorAll("div.intro") var temp3 = temp2[temp2.length-1].querySelectorAll("p"); for(var i=0;i<temp3.length;i++){ authorIntro = authorIntro+temp3[i].textContent+"\n"; } }
var temp = $2("script"); let tags = temp[temp.length-3].textContent.match(/(?<=:)[\u4e00-\u9fa5·]+/g); if (tags === null) { tags = []; } else { tags.push("book"); }
bookInfo.name = name; bookInfo.title=title; bookInfo.author=author; bookInfo.transAuthor=transAuthor; bookInfo.coverUrl=cover; bookInfo.originalName=originalName; bookInfo.pages=pages; bookInfo.publisher=publisher; bookInfo.intro=intro; bookInfo.isbn=isbn; bookInfo.rating=rating; bookInfo.authorIntro =authorIntro; bookInfo.tags=tags; bookInfo.link = url;
for(var i in bookInfo){ if(bookInfo[i]==""){ bookInfo[i]="Not Found."; } }
return bookInfo; } module.exports = douban
|