|
| 1 | +import { load as parseHTML } from 'cheerio'; |
| 2 | +import { fetchApi } from '@libs/fetch'; |
| 3 | +import { Plugin } from '@/types/plugin'; |
| 4 | +import { NovelStatus } from '@libs/novelStatus'; |
| 5 | + |
| 6 | +class NovelArrow implements Plugin.PluginBase { |
| 7 | + id = 'novelarrow'; |
| 8 | + name = 'Novel Arrow'; |
| 9 | + icon = 'src/en/novelarrow/icon.png'; |
| 10 | + site = 'https://novelarrow.com/'; |
| 11 | + version = '1.0.0'; |
| 12 | + |
| 13 | + async popularNovels(page: number) { |
| 14 | + const url = `${this.site}novels/latest?page=${page}`; |
| 15 | + const result = await fetchApi(url).then(res => res.text()); |
| 16 | + const $ = parseHTML(result); |
| 17 | + const novels: Plugin.NovelItem[] = []; |
| 18 | + |
| 19 | + $('article').each((i, el) => { |
| 20 | + const title = $(el).find('h2').text().trim(); |
| 21 | + const cover = $(el).find('img').attr('src'); |
| 22 | + const href = $(el).find('a').attr('href'); |
| 23 | + |
| 24 | + if (title && href) { |
| 25 | + novels.push({ |
| 26 | + name: title, |
| 27 | + cover, |
| 28 | + path: href.substring(1), // Result: "novel/slug" |
| 29 | + }); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + return novels; |
| 34 | + } |
| 35 | + |
| 36 | + async parseNovel(novelPath: string) { |
| 37 | + // Ensure no double slashes in the URL |
| 38 | + const url = this.site + novelPath.replace(/^\//, ''); |
| 39 | + const result = await fetchApi(url).then(res => res.text()); |
| 40 | + const $ = parseHTML(result); |
| 41 | + |
| 42 | + const novelId = novelPath.replace('novel/', '').replace(/^\//, ''); |
| 43 | + |
| 44 | + // Collect genres |
| 45 | + let genres = |
| 46 | + $('meta[name="og:novel:genre"]').attr('content') || |
| 47 | + $('meta[property="og:novel:genre"]').attr('content'); |
| 48 | + |
| 49 | + if (!genres) { |
| 50 | + const genreList: string[] = []; |
| 51 | + $('meta[property="article:tag"]').each((i, el) => { |
| 52 | + const tag = $(el).attr('content'); |
| 53 | + if (tag) genreList.push(tag); |
| 54 | + }); |
| 55 | + genres = genreList.join(', '); |
| 56 | + } |
| 57 | + |
| 58 | + // Attempt to get the full summary from the JSON stream if the meta tag is truncated |
| 59 | + let fullSummary = |
| 60 | + $('meta[name="description"]').attr('content') || |
| 61 | + $('meta[property="og:description"]').attr('content'); |
| 62 | + const summaryMatch = result.match(/\\?"description\\?":\\?"(.*?)\\?"/); |
| 63 | + if (summaryMatch && summaryMatch[1].length > (fullSummary?.length || 0)) { |
| 64 | + fullSummary = summaryMatch[1].replace(/\\n/g, '\n').replace(/\\"/g, '"'); |
| 65 | + } |
| 66 | + |
| 67 | + const novel: Plugin.SourceNovel = { |
| 68 | + path: novelPath, |
| 69 | + name: |
| 70 | + $('meta[name="og:novel:novel_name"]').attr('content') || |
| 71 | + $('meta[property="og:novel:novel_name"]').attr('content') || |
| 72 | + $('meta[property="og:title"]').attr('content')?.split(' Novel')[0] || |
| 73 | + $('h1').first().text().trim(), |
| 74 | + cover: |
| 75 | + $('meta[property="og:image"]').attr('content') || |
| 76 | + $('meta[name="og:image"]').attr('content'), |
| 77 | + author: |
| 78 | + $('meta[name="og:novel:author"]').attr('content') || |
| 79 | + $('meta[property="og:novel:author"]').attr('content') || |
| 80 | + $('meta[name="author"]').attr('content') || |
| 81 | + $('meta[property="article:author"]').attr('content'), |
| 82 | + status: |
| 83 | + ($('meta[name="og:novel:status"]').attr('content') || |
| 84 | + $('meta[property="og:novel:status"]').attr('content')) === 'Ongoing' |
| 85 | + ? NovelStatus.Ongoing |
| 86 | + : NovelStatus.Completed, |
| 87 | + summary: fullSummary, |
| 88 | + genres: genres, |
| 89 | + chapters: [], |
| 90 | + }; |
| 91 | + |
| 92 | + const chaptersUrl = `${this.site}api-web/novels/${novelId}/chapters?sort=asc`; |
| 93 | + try { |
| 94 | + const chaptersJson = await fetchApi(chaptersUrl, { |
| 95 | + headers: { |
| 96 | + 'Accept': 'application/json', |
| 97 | + }, |
| 98 | + }).then(res => res.json()); |
| 99 | + |
| 100 | + if (chaptersJson && chaptersJson.items) { |
| 101 | + novel.chapters = chaptersJson.items.map( |
| 102 | + (item: { chapter_name: string; chapter_id: string }) => ({ |
| 103 | + name: item.chapter_name, |
| 104 | + path: `chapter/${novelId}/${item.chapter_id}`, |
| 105 | + releaseTime: null, |
| 106 | + }), |
| 107 | + ); |
| 108 | + } |
| 109 | + } catch (e) { |
| 110 | + const chaptersMap = new Map(); |
| 111 | + // Flexible Regex to handle JSON stream variations |
| 112 | + const combinedRegex = |
| 113 | + /\\?"chapter_id\\?":\\?"([^"]+)\\?",\\?"chapter_name\\?":\\?"([^"]+)\\?"/g; |
| 114 | + let match; |
| 115 | + while ((match = combinedRegex.exec(result)) !== null) { |
| 116 | + const path = match[1]; |
| 117 | + const name = match[2].replace(/\\"/g, '"'); |
| 118 | + const fullPath = `chapter/${novelId}/${path}`; |
| 119 | + if (!chaptersMap.has(fullPath)) { |
| 120 | + chaptersMap.set(fullPath, { |
| 121 | + name, |
| 122 | + path: fullPath, |
| 123 | + releaseTime: null, |
| 124 | + }); |
| 125 | + } |
| 126 | + } |
| 127 | + novel.chapters = Array.from(chaptersMap.values()); |
| 128 | + } |
| 129 | + |
| 130 | + return novel; |
| 131 | + } |
| 132 | + |
| 133 | + async parseChapter(chapterPath: string) { |
| 134 | + const pathParts = chapterPath.replace('chapter/', '').split('/'); |
| 135 | + const novelId = pathParts[0]; |
| 136 | + const chapterId = pathParts[1]; |
| 137 | + |
| 138 | + const url = `${this.site}api-web/novels/${novelId}/chapters/${chapterId}`; |
| 139 | + |
| 140 | + try { |
| 141 | + const json = await fetchApi(url, { |
| 142 | + headers: { |
| 143 | + 'Accept': 'application/json', |
| 144 | + 'x-track-reading-progress': 'false', |
| 145 | + }, |
| 146 | + }).then(res => res.json()); |
| 147 | + |
| 148 | + if ( |
| 149 | + json && |
| 150 | + json.item && |
| 151 | + json.item.chapterInfo && |
| 152 | + json.item.chapterInfo.chapter_content |
| 153 | + ) { |
| 154 | + return json.item.chapterInfo.chapter_content; |
| 155 | + } |
| 156 | + } catch (e) { |
| 157 | + const result = await fetchApi(`${this.site}${chapterPath}`).then(res => |
| 158 | + res.text(), |
| 159 | + ); |
| 160 | + const contentRegex = /\\u003ch4\\u003e(.*)\\u003c\/p\\u003e/; |
| 161 | + const match = result.match(contentRegex); |
| 162 | + |
| 163 | + if (match) { |
| 164 | + let chapterHtml = match[0]; |
| 165 | + chapterHtml = chapterHtml |
| 166 | + .replace(/\\u003c/g, '<') |
| 167 | + .replace(/\\u003e/g, '>') |
| 168 | + .replace(/\\"/g, '"') |
| 169 | + .replace(/\\n/g, '') |
| 170 | + .replace(/\\t/g, '') |
| 171 | + .replace(/\\r/g, '') |
| 172 | + .replace(/\\\\/g, '\\'); |
| 173 | + |
| 174 | + const lastPTagIndex = chapterHtml.lastIndexOf('</p>'); |
| 175 | + if (lastPTagIndex !== -1) { |
| 176 | + chapterHtml = chapterHtml.substring(0, lastPTagIndex + 4); |
| 177 | + } |
| 178 | + return chapterHtml; |
| 179 | + } |
| 180 | + |
| 181 | + const $ = parseHTML(result); |
| 182 | + return $('.site-reading-copy').html() || 'Content not found or premium.'; |
| 183 | + } |
| 184 | + |
| 185 | + return 'Content not found or premium.'; |
| 186 | + } |
| 187 | + |
| 188 | + async searchNovels(searchTerm: string, page: number) { |
| 189 | + const url = `${this.site}novels/search?keyword=${encodeURIComponent(searchTerm)}&page=${page}`; |
| 190 | + const result = await fetchApi(url).then(res => res.text()); |
| 191 | + const $ = parseHTML(result); |
| 192 | + const novels: Plugin.NovelItem[] = []; |
| 193 | + |
| 194 | + $('article').each((i, el) => { |
| 195 | + const title = $(el).find('h2').text().trim(); |
| 196 | + const cover = $(el).find('img').attr('src'); |
| 197 | + const href = $(el).find('a').attr('href'); |
| 198 | + |
| 199 | + if (title && href) { |
| 200 | + novels.push({ |
| 201 | + name: title, |
| 202 | + cover, |
| 203 | + path: href.substring(1), |
| 204 | + }); |
| 205 | + } |
| 206 | + }); |
| 207 | + |
| 208 | + return novels; |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +export default new NovelArrow(); |
0 commit comments