dsh-lamplit 0.0.0-stage → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/pdf.js ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * PDF:两条路,跟桌面阅读器一致。
3
+ *
4
+ * - 有文字层 → 抽文本,进沉浸排版(能划词、能调字号、能搜索)
5
+ * - 没有文字层(扫描件 / 纯图片)→ **不硬抽**,标记成 `pdf-scan`,
6
+ * 阅读器用页面渲染(浏览器内置 PDF 阅读器)显示原始页面。
7
+ *
8
+ * `lib/pdf-text.js` 只负责「判断有没有文字层 + 抽出来」,本文件负责决策。
9
+ */
10
+ import { extractPdfText } from './pdf-text.js'
11
+ import { parsePlainText } from './txt.js'
12
+
13
+ /** PDF 里常出现 CJK 兼容表意字(U+2F00–U+2FDF / U+F900–U+FAFF),统一成常用字。 */
14
+ function normalizeIdeographs(text) {
15
+ return String(text).replace(/[\u2e80-\u2fdf\uf900-\ufaff]/g, (char) => char.normalize('NFKC'))
16
+ }
17
+
18
+ /**
19
+ * @param {Uint8Array} buffer
20
+ * @param {{fallbackTitle?: string}} options
21
+ */
22
+ export function analyzePdf(buffer, options = {}) {
23
+ let extracted
24
+ try {
25
+ extracted = extractPdfText(buffer)
26
+ } catch (error) {
27
+ if (String(error?.message ?? '').includes('not a pdf')) {
28
+ const failure = new Error('这不是一个 PDF 文件。')
29
+ failure.code = 'FORMAT'
30
+ throw failure
31
+ }
32
+ throw error
33
+ }
34
+
35
+ const pageCount = Number(extracted?.pageCount) || (Array.isArray(extracted?.pages) ? extracted.pages.length : 0) || 1
36
+ const text = normalizeIdeographs(String(extracted?.text ?? ''))
37
+ const perPage = text.trim().length / Math.max(pageCount, 1)
38
+ const hasTextLayer = typeof extracted?.hasTextLayer === 'boolean' ? extracted.hasTextLayer : perPage >= 25
39
+
40
+ if (!hasTextLayer) {
41
+ return {
42
+ mode: 'scan',
43
+ pageCount,
44
+ textDensity: Number(extracted?.textDensity ?? Number(perPage.toFixed(1))),
45
+ warnings: extracted?.warnings ?? ['no-text-layer'],
46
+ chapters: [],
47
+ title: options.fallbackTitle ?? '',
48
+ author: '',
49
+ }
50
+ }
51
+
52
+ const parsed = parsePlainText(text, { fallbackTitle: options.fallbackTitle, maxChars: 9000 })
53
+ return {
54
+ mode: 'text',
55
+ pageCount,
56
+ textDensity: Number(extracted?.textDensity ?? Number(perPage.toFixed(1))),
57
+ warnings: extracted?.warnings ?? [],
58
+ chapters: parsed.chapters,
59
+ title: parsed.title,
60
+ author: '',
61
+ }
62
+ }
@@ -0,0 +1,208 @@
1
+ /**
2
+ * 番茄小说来源。
3
+ *
4
+ * 现实情况(2026-11 实测):
5
+ * - 目录接口公开可用:`/api/reader/directory/detail?bookId=` 返回分卷章节列表。
6
+ * - 书页 HTML 公开可读:`/page/<bookId>` 里有书名。
7
+ * - 正文接口 `/api/reader/full` 需要 App 侧签名;网页版正文把常用字换成了
8
+ * U+E000 私用区字形(字体反爬),直接抓 HTML 只能拿到掺了空洞的文本。
9
+ *
10
+ * 所以这里:目录、书名、章节元数据照常同步;正文尽力抓,抓到的是混淆文本时
11
+ * **如实报错**(code: OBFUSCATED),由界面引导「去番茄读」或「粘贴正文导入」,
12
+ * 不把乱码当正文塞给读者。
13
+ */
14
+ import { ProviderError, extractBookId, request, requestJson } from '../http.js'
15
+
16
+ const ORIGIN = 'https://fanqienovel.com'
17
+ const TIMEOUT_MS = 20000
18
+ const PUA_SOURCE = /[\ue000-\uf8ff]/g
19
+
20
+ function countPrivateUse(text) {
21
+ return (String(text).match(PUA_SOURCE) ?? []).length
22
+ }
23
+
24
+ function stripTags(html) {
25
+ return String(html ?? '')
26
+ .replace(/<br\s*\/?>/gi, '\n')
27
+ .replace(/<\/p>/gi, '\n')
28
+ .replace(/<[^>]+>/g, '')
29
+ .replace(/&nbsp;/g, ' ')
30
+ .replace(/&lt;/g, '<')
31
+ .replace(/&gt;/g, '>')
32
+ .replace(/&quot;/g, '"')
33
+ .replace(/&#39;/g, "'")
34
+ .replace(/&amp;/g, '&')
35
+ }
36
+
37
+ function paragraphsOf(text) {
38
+ return String(text ?? '')
39
+ .split('\n')
40
+ .map((line) => line.trim())
41
+ .filter((line) => line !== '')
42
+ }
43
+
44
+ export function createFanqieProvider({ readRecord }) {
45
+ async function cookie() {
46
+ const record = await readRecord()
47
+ return String(record?.cookie ?? '').trim()
48
+ }
49
+
50
+ async function directory(bookId) {
51
+ let payload
52
+ try {
53
+ payload = await requestJson(`${ORIGIN}/api/reader/directory/detail?bookId=${encodeURIComponent(bookId)}`, {
54
+ cookie: await cookie(),
55
+ referer: `${ORIGIN}/page/${bookId}`,
56
+ timeoutMs: TIMEOUT_MS,
57
+ })
58
+ } catch (error) {
59
+ throw error instanceof ProviderError ? error : new ProviderError('NET', String(error?.message ?? error))
60
+ }
61
+ const data = payload.json?.data
62
+ if (data === null || typeof data !== 'object') {
63
+ throw new ProviderError('UPSTREAM', '番茄没有返回这本书的目录(书 id 可能不对)。')
64
+ }
65
+ const volumes = Array.isArray(data.chapterListWithVolume) ? data.chapterListWithVolume : []
66
+ const chapters = []
67
+ for (const volume of volumes) {
68
+ for (const chapter of Array.isArray(volume) ? volume : []) {
69
+ if (chapter === null || typeof chapter !== 'object' || chapter.itemId === undefined) continue
70
+ chapters.push({
71
+ chapterId: String(chapter.itemId),
72
+ title: String(chapter.title ?? '').trim() || `第 ${chapters.length + 1} 章`,
73
+ locked: chapter.needPay === 1 || chapter.isChapterLock === true,
74
+ })
75
+ }
76
+ }
77
+ if (chapters.length === 0 && Array.isArray(data.allItemIds)) {
78
+ for (const [index, itemId] of data.allItemIds.entries()) {
79
+ chapters.push({ chapterId: String(itemId), title: `第 ${index + 1} 章`, locked: false })
80
+ }
81
+ }
82
+ if (chapters.length === 0) throw new ProviderError('UPSTREAM', '这本书的目录是空的。')
83
+ return chapters
84
+ }
85
+
86
+ async function pageMeta(bookId) {
87
+ try {
88
+ const response = await request(`${ORIGIN}/page/${bookId}`, {
89
+ cookie: await cookie(),
90
+ timeoutMs: TIMEOUT_MS,
91
+ accept: 'text/html,application/xhtml+xml',
92
+ })
93
+ const html = await response.text()
94
+ const title = /<h1[^>]*>([^<]{1,80})<\/h1>/.exec(html)?.[1]?.trim() ?? ''
95
+ const cover =
96
+ /<meta[^>]+property="og:image"[^>]+content="([^"]+)"/.exec(html)?.[1] ??
97
+ /"thumb_url"\s*:\s*"([^"]+)"/.exec(html)?.[1] ??
98
+ ''
99
+ const author =
100
+ /"author"\s*:\s*"([^"]{1,40})"/.exec(html)?.[1]?.trim() ??
101
+ /作者[::]\s*<[^>]*>([^<]{1,40})/.exec(html)?.[1]?.trim() ??
102
+ ''
103
+ return { title, author, cover }
104
+ } catch {
105
+ return { title: '', author: '' }
106
+ }
107
+ }
108
+
109
+ return {
110
+ id: 'fanqie',
111
+ label: '番茄小说',
112
+
113
+ async login({ cookie: raw }) {
114
+ const value = String(raw ?? '').trim()
115
+ if (value === '') throw new ProviderError('BADREQ', 'Cookie 是空的。')
116
+ // 用一次目录请求确认网络与登录头都被接受。
117
+ try {
118
+ await directory('7143038691944959011')
119
+ } catch (error) {
120
+ if (error?.code === 'AUTH') throw error
121
+ throw new ProviderError('AUTH', '这段 Cookie 没被番茄接受,或当前网络访问不了番茄。')
122
+ }
123
+ return { account: '番茄账号', shelfCount: 0 }
124
+ },
125
+
126
+ /** 番茄没有可用书架接口:这里返回空数组,书籍通过「链接/ID 添加」。 */
127
+ async shelf() {
128
+ return []
129
+ },
130
+
131
+ /** @param {{bookId: string, title?: string}} input */
132
+ async toc({ bookId }) {
133
+ const chapters = await directory(bookId)
134
+ const meta = await pageMeta(bookId)
135
+ return {
136
+ title: meta.title || `番茄小说 ${bookId}`,
137
+ author: meta.author,
138
+ cover: meta.cover ?? '',
139
+ chapters: chapters.map((chapter) => ({
140
+ title: chapter.locked ? `${chapter.title}(付费)` : chapter.title,
141
+ chapterId: chapter.chapterId,
142
+ locked: chapter.locked,
143
+ })),
144
+ }
145
+ },
146
+
147
+ /** @param {{book: object, chapter: object}} input */
148
+ async chapter({ book, chapter }) {
149
+ const itemId = String(chapter.chapterId ?? '')
150
+ if (itemId === '') throw new ProviderError('BADREQ', '这一章没有 itemId。')
151
+ const sessionCookie = await cookie()
152
+
153
+ // 1) 先试接口(登录态正常时偶尔能给纯文本)
154
+ let text = ''
155
+ try {
156
+ const payload = await requestJson(`${ORIGIN}/api/reader/full?itemId=${encodeURIComponent(itemId)}`, {
157
+ cookie: sessionCookie,
158
+ referer: `${ORIGIN}/reader/${itemId}`,
159
+ timeoutMs: TIMEOUT_MS,
160
+ })
161
+ const content = payload.json?.data?.content
162
+ if (typeof content === 'string' && content.trim() !== '') text = stripTags(content)
163
+ } catch {
164
+ // 接口不可用是常态,继续走 SSR。
165
+ }
166
+
167
+ // 2) 退到服务端渲染的阅读页
168
+ if (text.trim() === '') {
169
+ const response = await request(`${ORIGIN}/reader/${itemId}`, {
170
+ cookie: sessionCookie,
171
+ referer: `${ORIGIN}/page/${book.sourceId}`,
172
+ timeoutMs: TIMEOUT_MS,
173
+ accept: 'text/html,application/xhtml+xml',
174
+ })
175
+ const html = await response.text()
176
+ const match = /<div class="muye-reader-content[^"]*">([\s\S]*?)<\/div>/.exec(html)
177
+ if (match) text = stripTags(match[1])
178
+ }
179
+
180
+ if (text.trim() === '') {
181
+ throw new ProviderError('UPSTREAM', '番茄这一章没有返回可读正文(可能需要登录、付费,或被风控拦截)。')
182
+ }
183
+
184
+ const paragraphs = paragraphsOf(text)
185
+ const joined = paragraphs.join('')
186
+ const puaCount = countPrivateUse(joined)
187
+ if (puaCount / Math.max(joined.length, 1) > 0.02) {
188
+ throw new ProviderError(
189
+ 'OBFUSCATED',
190
+ '这一章的正文被番茄换成了私用区字形(字体反爬),抓下来只能是乱码。请在番茄 App/网页里读这一章,或用「粘贴正文」把它导进灯下。',
191
+ { ratio: Number((puaCount / Math.max(joined.length, 1)).toFixed(3)) },
192
+ )
193
+ }
194
+
195
+ return {
196
+ title: chapter.title,
197
+ blocks: paragraphs.map((line) => ({ t: 'p', x: line })),
198
+ }
199
+ },
200
+
201
+ /** 从链接或纯 id 解析书;界面「添加番茄书籍」用。 */
202
+ resolve(input) {
203
+ const bookId = extractBookId(input)
204
+ if (bookId === '') throw new ProviderError('BADREQ', '没认出番茄的书 id,请粘贴 `fanqienovel.com/page/<id>` 链接。')
205
+ return bookId
206
+ },
207
+ }
208
+ }
@@ -0,0 +1,144 @@
1
+ /**
2
+ * 微信读书来源:用你自己账号的 Cookie,读你自己的书架、目录与正文。
3
+ *
4
+ * 网络层复用 vendored 的 dsh-weread 实现(MIT,见 lib/vendor/weread/NOTICE)。
5
+ * 这里只做三件事:凭据校验、书架/目录归一化、章节正文落成 blocks。
6
+ */
7
+ import { WereadReaderError, WereadWebReader } from '../vendor/weread/reader.js'
8
+ import { WereadWebApi } from '../vendor/weread/web.js'
9
+ import { ProviderError, parseCookie } from '../http.js'
10
+
11
+ const TIMEOUT_MS = 25000
12
+
13
+ function makeClients(cookie) {
14
+ const getCookie = () => cookie
15
+ const web = new WereadWebApi({ getCookie, getTimeoutMs: () => TIMEOUT_MS })
16
+ const reader = new WereadWebReader({ getCookie, getTimeoutMs: () => TIMEOUT_MS })
17
+ return { web, reader }
18
+ }
19
+
20
+ function translateError(error) {
21
+ if (error instanceof ProviderError) return error
22
+ const code = error?.code ?? ''
23
+ const message = error?.message ?? String(error)
24
+ if (code === 'weread/session-expired' || code === 'weread/no-session' || code === 'weread/bad-cookie') {
25
+ return new ProviderError('AUTH', '微信读书登录已失效,请重新复制 Cookie。', { code })
26
+ }
27
+ if (code.startsWith?.('weread/')) return new ProviderError('UPSTREAM', message, { code })
28
+ return new ProviderError('NET', message, { code })
29
+ }
30
+
31
+ function paragraphsToBlocks(paragraphs) {
32
+ const blocks = []
33
+ for (const paragraph of Array.isArray(paragraphs) ? paragraphs : []) {
34
+ const text = String(paragraph ?? '').trim()
35
+ if (text === '') continue
36
+ blocks.push({ t: 'p', x: text })
37
+ }
38
+ return blocks
39
+ }
40
+
41
+ export function createWereadProvider({ readRecord }) {
42
+ async function session() {
43
+ const record = await readRecord()
44
+ const cookie = String(record?.cookie ?? '').trim()
45
+ if (cookie === '') throw new ProviderError('AUTH', '还没有连接微信读书:在来源卡片里粘贴浏览器 Cookie。')
46
+ return { cookie, ...makeClients(cookie) }
47
+ }
48
+
49
+ return {
50
+ id: 'weread',
51
+ label: '微信读书',
52
+
53
+ /** @param {{cookie: string}} input */
54
+ async login({ cookie }) {
55
+ const raw = String(cookie ?? '').trim()
56
+ if (raw === '') throw new ProviderError('BADREQ', 'Cookie 是空的。')
57
+ const pairs = parseCookie(raw)
58
+ if (!pairs.has('wr_skey') && !pairs.has('wr_vid')) {
59
+ throw new ProviderError('AUTH', '这段 Cookie 里没有 wr_vid / wr_skey,像是复制错了位置。')
60
+ }
61
+ const clients = makeClients(raw)
62
+ let books = []
63
+ try {
64
+ const payload = await clients.web.shelf()
65
+ books = Array.isArray(payload?.books) ? payload.books : []
66
+ } catch (error) {
67
+ throw translateError(error)
68
+ }
69
+ const vid = pairs.get('wr_vid') ?? ''
70
+ return {
71
+ account: vid === '' ? '已连接' : `账号 ${vid.slice(0, 6)}${vid.length > 6 ? '…' : ''}`,
72
+ shelfCount: books.length,
73
+ }
74
+ },
75
+
76
+ async shelf() {
77
+ const { web } = await session()
78
+ let payload
79
+ try {
80
+ payload = await web.shelf()
81
+ } catch (error) {
82
+ throw translateError(error)
83
+ }
84
+ const books = Array.isArray(payload?.books) ? payload.books : []
85
+ return books.map((raw) => {
86
+ const book = WereadWebApi.normalizeBook(raw)
87
+ return {
88
+ sourceId: String(book.bookId),
89
+ title: String(book.title ?? ''),
90
+ author: String(book.author ?? ''),
91
+ cover: typeof book.cover === 'string' ? book.cover : '',
92
+ }
93
+ })
94
+ },
95
+
96
+ /** @param {{bookId: string}} input */
97
+ async toc({ bookId }) {
98
+ const { reader, web } = await session()
99
+ let chapters = []
100
+ try {
101
+ chapters = await reader.webCatalog(String(bookId))
102
+ } catch (error) {
103
+ throw translateError(error)
104
+ }
105
+ let title = ''
106
+ let author = ''
107
+ try {
108
+ const info = await web.bookInfo(String(bookId))
109
+ title = String(info?.title ?? '')
110
+ author = String(info?.author ?? '')
111
+ } catch {
112
+ // 书名不是必需的:拿不到就用 id。
113
+ }
114
+ return {
115
+ title: title || `微信读书 ${bookId}`,
116
+ author,
117
+ chapters: chapters.map((chapter, index) => ({
118
+ title: chapter.title || `第 ${index + 1} 章`,
119
+ chapterId: String(chapter.chapterUid),
120
+ })),
121
+ }
122
+ },
123
+
124
+ /** @param {{book: object, chapter: {chapterId: string, title: string}}} input */
125
+ async chapter({ book, chapter }) {
126
+ const { reader } = await session()
127
+ let payload
128
+ try {
129
+ payload = await reader.fetchChapter({
130
+ bookId: String(book.sourceId),
131
+ chapterUid: Number(chapter.chapterId),
132
+ title: chapter.title,
133
+ })
134
+ } catch (error) {
135
+ throw translateError(error)
136
+ }
137
+ const blocks = paragraphsToBlocks(payload?.paragraphs)
138
+ if (blocks.length === 0) {
139
+ throw new ProviderError('UPSTREAM', '微信读书这一章没有返回正文(可能需要付费或该章为空)。')
140
+ }
141
+ return { title: payload?.title || chapter.title, blocks }
142
+ },
143
+ }
144
+ }