|
| 1 | +/** |
| 2 | + * 文件编码: UTF-8(如不是UTF8编码可能会导致乱码或未知错误) |
| 3 | + * 禁止使用import、require导入模块 |
| 4 | + * 若使用import * from *、import()、require()导入模块, 无法通过插件校验 |
| 5 | + * import fs from 'fs'; |
| 6 | + * import('fs').then().catch(); |
| 7 | + * require('fs'); |
| 8 | + */ |
| 9 | +plugin.exports = class Plugin implements BookSource { |
| 10 | + /** |
| 11 | + * 静态属性 ID 自动生成 |
| 12 | + * 该值需符合正则表达式: [A-Za-z0-9_-] |
| 13 | + */ |
| 14 | + public static readonly ID: string = "RipYNVxhVR7aTH22P56gJ"; |
| 15 | + /** |
| 16 | + * 静态属性 TYPE 必填 |
| 17 | + * 插件类型 |
| 18 | + * 值类型: |
| 19 | + * plugin.type.BOOK_SOURCE - 表示该插件为书源类 |
| 20 | + * plugin.type.BOOK_STORE - 表示该插件为书城类 |
| 21 | + */ |
| 22 | + public static readonly TYPE: number = plugin.type.BOOK_SOURCE; |
| 23 | + /** |
| 24 | + * 静态属性 GROUP 必填 |
| 25 | + * 插件分组 |
| 26 | + */ |
| 27 | + public static readonly GROUP: string = "👻MaxOS"; |
| 28 | + /** |
| 29 | + * 静态属性 NAME 必填 |
| 30 | + * 插件名称 |
| 31 | + */ |
| 32 | + public static readonly NAME: string = "蚂蚁阅读"; |
| 33 | + /** |
| 34 | + * 静态属性 VERSION 必填 |
| 35 | + * 插件版本 用于显示 |
| 36 | + */ |
| 37 | + public static readonly VERSION: string = "1.0.0"; |
| 38 | + /** |
| 39 | + * 静态属性 VERSION_CODE 必填 |
| 40 | + * 插件版本代码 用于比较本地插件与静态属性PLUGIN_FILE_URL所指插件的版本号 |
| 41 | + */ |
| 42 | + public static readonly VERSION_CODE: number = 0; |
| 43 | + /** |
| 44 | + * 静态属性 PLUGIN_FILE_URL 必填 |
| 45 | + * 插件http、https链接, 如: http://example.com/plugin-template.js |
| 46 | + */ |
| 47 | + public static readonly PLUGIN_FILE_URL: string = ""; |
| 48 | + /** |
| 49 | + * 静态属性 BASE_URL 必填 |
| 50 | + * 插件请求目标链接 |
| 51 | + */ |
| 52 | + public static readonly BASE_URL: string = "http://www.mayitxt.org"; |
| 53 | + /** |
| 54 | + * 静态属性 REQUIRE 可选 |
| 55 | + * 要求用户填写的值 |
| 56 | + */ |
| 57 | + public static readonly REQUIRE: Record<string, string> = {}; |
| 58 | + private request: ReadCatRequest; |
| 59 | + private store: Store; |
| 60 | + private cheerio: CheerioModule.load; |
| 61 | + private nanoid: () => string; |
| 62 | + constructor(options: PluginConstructorOptions) { |
| 63 | + const { request, store, cheerio, nanoid } = options; |
| 64 | + /** |
| 65 | + * request |
| 66 | + * function get(url, config) |
| 67 | + * url: string 请求链接 |
| 68 | + * config(可选): { |
| 69 | + * params(可选): { [key: string]: number | string | boolean } | URLSearchParams, 请求参数 |
| 70 | + * headers(可选): { [key: string]: string }, 请求头 |
| 71 | + * proxy(可选): boolean 是否开启代理, |
| 72 | + * charset(可选): string 字符集, 默认为自动获取, 当出现乱码时请指定字符集 |
| 73 | + * urlencode(可选): string URL编码, 默认UTF8 |
| 74 | + * maxRedirects(可选): number 最大重定向数, 为0时则禁止重定向 |
| 75 | + * responseType(可选): 'arraybuffer' | 'text' | 'json' 响应体类型, 默认text |
| 76 | + * signal(可选): AbortSignal 中止信号 |
| 77 | + * } |
| 78 | + * return: Promise<{ body, code, headers }> |
| 79 | + * function post(url, config) |
| 80 | + * url: string 请求链接 |
| 81 | + * config(可选): { |
| 82 | + * params(可选): { [key: string]: number | string | boolean }, | URLSearchParams, 请求参数 |
| 83 | + * headers(可选): { [key: string]: string }, 请求头 |
| 84 | + * proxy(可选): boolean 是否开启代理 |
| 85 | + * charset(可选): string 字符集, 默认为自动获取, 当出现乱码时请指定字符集 |
| 86 | + * urlencode(可选): string URL编码, 默认UTF8 |
| 87 | + * maxRedirects(可选): number 最大重定向数, 为0时则禁止重定向 |
| 88 | + * responseType(可选): 'arraybuffer' | 'text' | 'json' 响应体类型, 默认text |
| 89 | + * signal(可选): AbortSignal 中止信号 |
| 90 | + * } |
| 91 | + * return: Promise<{ body, code, headers }> |
| 92 | + * |
| 93 | + * body: 响应体 |
| 94 | + * code: 响应码 |
| 95 | + * headers: 响应头 |
| 96 | + */ |
| 97 | + this.request = request; |
| 98 | + /** |
| 99 | + * 每个插件都自带仓库(最大存储4MB), 您可向该仓库设置、获取、删除值 |
| 100 | + * store |
| 101 | + * function setStoreValue(key, value) |
| 102 | + * key: string, |
| 103 | + * value: any (JavaScript基本数据类型), 该值经过v8.serialize处理 |
| 104 | + * return Promise<void> |
| 105 | + * function getStoreValue(key) |
| 106 | + * key: string |
| 107 | + * return Promise<any> (JavaScript基本数据类型) |
| 108 | + * function removeStoreValue(key) |
| 109 | + * key: string |
| 110 | + * return Promise<void> |
| 111 | + */ |
| 112 | + this.store = store; |
| 113 | + /** |
| 114 | + * function cheerio(html: string) |
| 115 | + * 该值是模块cheerio中的load方法, 用法 const $ = cheerio(HTMLString) |
| 116 | + * 文档: https://cheerio.nodejs.cn/docs/basics/loading#load |
| 117 | + */ |
| 118 | + this.cheerio = cheerio; |
| 119 | + /** |
| 120 | + * function nanoid |
| 121 | + * 获取21位随机字符串 |
| 122 | + */ |
| 123 | + this.nanoid = nanoid; |
| 124 | + } |
| 125 | +// http://www.mayitxt.org/modules/article/search.php?q=%E5%AE%8C%E7%BE%8E%E4%B8%96%E7%95%8C&searchtype=all&s=17333194950446968473 |
| 126 | +// http://www.mayitxt.org/modules/article/search.php?q=%E5%AE%8C%E7%BE%8E%E4%B8%96%E7%95%8C&searchtype=all&s=17333194950446968473 |
| 127 | + async search(searchkey: string): Promise<SearchEntity[]> { |
| 128 | + const { body } = await this.request.get( |
| 129 | + `${Plugin.BASE_URL}/modules/article/search.php?q=${searchkey}&searchtype=all&s=17333194950446968473` |
| 130 | + ); |
| 131 | + const $ = this.cheerio(body); |
| 132 | + const trs = $("tbody tr:nth-child(n+2)"); |
| 133 | + console.log(trs.length); |
| 134 | + const results: SearchEntity[] = []; |
| 135 | + for (const tr of trs) { |
| 136 | + console.log(tr); |
| 137 | + const td = $(tr).find("td"); |
| 138 | + const regex = /\/(\d+)_(\d+)\//; |
| 139 | + const match = $(td.get(2)).children("a").attr("href").match(regex); |
| 140 | + const fid = match[1]; |
| 141 | + const bid = match[2]; |
| 142 | + console.log(fid, bid); |
| 143 | + results.push({ |
| 144 | + bookname: $(td.get(2)).children("a").text(), |
| 145 | + author: $(td.get(5)).text(), |
| 146 | + coverImageUrl: `${Plugin.BASE_URL}/files/article/image/${fid}/${bid}/${bid}s.jpg`, |
| 147 | + detailPageUrl: $(td.get(0)).children("a").attr("href"), |
| 148 | + latestChapterTitle: $(td.get(3)).text(), |
| 149 | + }); |
| 150 | + } |
| 151 | + return results; |
| 152 | + } |
| 153 | + |
| 154 | + async getDetail(detailPageUrl: string): Promise<DetailEntity> { |
| 155 | + const { body } = await this.request.get(detailPageUrl); |
| 156 | + const $ = this.cheerio(body); |
| 157 | + const bookname = $("#info > h1").text(); |
| 158 | + const author = $("#info > p:nth-child(2) > a").text(); |
| 159 | + const latestChapterTitle = $("#info > p:nth-child(5) > a").text(); |
| 160 | + const coverImageUrl = $("#fmimg > img").attr("src"); |
| 161 | + const intro = $("#intro").text(); |
| 162 | + // console.log(detailPageUrl); |
| 163 | + const items = $("#list > dl > dd"); |
| 164 | + const chapterList: Chapter[] = []; |
| 165 | + for (const item of items) { |
| 166 | + const a = $(item).children("a"); |
| 167 | + chapterList.push({ |
| 168 | + title: a.text(), |
| 169 | + url: Plugin.BASE_URL + a.attr("href"), |
| 170 | + }); |
| 171 | + } |
| 172 | + return { |
| 173 | + bookname, |
| 174 | + author, |
| 175 | + latestChapterTitle, |
| 176 | + coverImageUrl, |
| 177 | + intro, |
| 178 | + chapterList, |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + async getTextContent(chapter: Chapter): Promise<string[]> { |
| 183 | + const { body } = await this.request.get(chapter.url); |
| 184 | + const $ = this.cheerio(body); |
| 185 | + $("#content div").remove("div"); |
| 186 | + return $("#content") |
| 187 | + .html() |
| 188 | + .split("<br>") |
| 189 | + .filter((t) => t.trim()); |
| 190 | + } |
| 191 | +}; |
0 commit comments