|
| 1 | +import { AuthRequiredError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors'; |
| 2 | +import { cli, Strategy } from '@jackwener/opencli/registry'; |
| 3 | +import type { IPage } from '@jackwener/opencli/types'; |
| 4 | +import { htmlToMarkdown, isRecord } from '@jackwener/opencli/utils'; |
| 5 | +const LINUX_DO_DOMAIN = 'linux.do'; |
| 6 | +const LINUX_DO_HOME = 'https://linux.do'; |
| 7 | + |
| 8 | +interface FetchTopicResult { |
| 9 | + ok: boolean; |
| 10 | + status?: number; |
| 11 | + data?: unknown; |
| 12 | + error?: string; |
| 13 | +} |
| 14 | + |
| 15 | +interface LinuxDoTopicPost { |
| 16 | + post_number?: number; |
| 17 | + username?: string; |
| 18 | + raw?: string; |
| 19 | + cooked?: string; |
| 20 | + like_count?: number; |
| 21 | + created_at?: string; |
| 22 | +} |
| 23 | + |
| 24 | +interface LinuxDoTopicPayload { |
| 25 | + title?: string; |
| 26 | + post_stream?: { |
| 27 | + posts?: LinuxDoTopicPost[]; |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +interface TopicContentRow { |
| 32 | + content: string; |
| 33 | +} |
| 34 | + |
| 35 | +function toLocalTime(utcStr: string): string { |
| 36 | + if (!utcStr) return ''; |
| 37 | + const date = new Date(utcStr); |
| 38 | + return Number.isNaN(date.getTime()) ? utcStr : date.toLocaleString(); |
| 39 | +} |
| 40 | + |
| 41 | +function normalizeTopicPayload(payload: unknown): LinuxDoTopicPayload | null { |
| 42 | + if (!isRecord(payload)) return null; |
| 43 | + const postStream = isRecord(payload.post_stream) |
| 44 | + ? { |
| 45 | + posts: Array.isArray(payload.post_stream.posts) |
| 46 | + ? payload.post_stream.posts.filter(isRecord).map((post) => ({ |
| 47 | + post_number: typeof post.post_number === 'number' ? post.post_number : undefined, |
| 48 | + username: typeof post.username === 'string' ? post.username : undefined, |
| 49 | + raw: typeof post.raw === 'string' ? post.raw : undefined, |
| 50 | + cooked: typeof post.cooked === 'string' ? post.cooked : undefined, |
| 51 | + like_count: typeof post.like_count === 'number' ? post.like_count : undefined, |
| 52 | + created_at: typeof post.created_at === 'string' ? post.created_at : undefined, |
| 53 | + })) |
| 54 | + : undefined, |
| 55 | + } |
| 56 | + : undefined; |
| 57 | + |
| 58 | + return { |
| 59 | + title: typeof payload.title === 'string' ? payload.title : undefined, |
| 60 | + post_stream: postStream, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +function buildTopicMarkdownDocument(params: { |
| 65 | + title: string; |
| 66 | + author: string; |
| 67 | + likes?: number; |
| 68 | + createdAt: string; |
| 69 | + url: string; |
| 70 | + body: string; |
| 71 | +}): string { |
| 72 | + const frontMatterLines: string[] = []; |
| 73 | + const entries: [string, string | number | undefined][] = [ |
| 74 | + ['title', params.title || undefined], |
| 75 | + ['author', params.author || undefined], |
| 76 | + ['likes', typeof params.likes === 'number' && Number.isFinite(params.likes) ? params.likes : undefined], |
| 77 | + ['createdAt', params.createdAt || undefined], |
| 78 | + ['url', params.url || undefined], |
| 79 | + ]; |
| 80 | + for (const [key, value] of entries) { |
| 81 | + if (value === undefined) continue; |
| 82 | + if (typeof value === 'number') { |
| 83 | + frontMatterLines.push(`${key}: ${value}`); |
| 84 | + } else { |
| 85 | + // Quote strings that could be misinterpreted by YAML parsers |
| 86 | + const needsQuote = /[#{}[\],&*?|>!%@`'"]/.test(value) || /: /.test(value) || /:$/.test(value) || value.includes('\n'); |
| 87 | + frontMatterLines.push(`${key}: ${needsQuote ? `'${value.replace(/'/g, "''")}'` : value}`); |
| 88 | + } |
| 89 | + } |
| 90 | + const frontMatter = frontMatterLines.join('\n'); |
| 91 | + |
| 92 | + return [ |
| 93 | + frontMatter ? `---\n${frontMatter}\n---` : '', |
| 94 | + params.body.trim(), |
| 95 | + ].filter(Boolean).join('\n\n').trim(); |
| 96 | +} |
| 97 | + |
| 98 | +function extractTopicContent(payload: unknown, id: number): TopicContentRow { |
| 99 | + const topic = normalizeTopicPayload(payload); |
| 100 | + if (!topic) { |
| 101 | + throw new CommandExecutionError('linux.do returned an unexpected topic payload'); |
| 102 | + } |
| 103 | + |
| 104 | + const posts = topic.post_stream?.posts ?? []; |
| 105 | + const mainPost = posts.find((post) => post.post_number === 1); |
| 106 | + if (!mainPost) { |
| 107 | + throw new EmptyResultError('linux-do/topic-content', `Could not find the main post for topic ${id}.`); |
| 108 | + } |
| 109 | + |
| 110 | + const body = typeof mainPost.raw === 'string' && mainPost.raw.trim() |
| 111 | + ? mainPost.raw.trim() |
| 112 | + : htmlToMarkdown(mainPost.cooked ?? ''); |
| 113 | + |
| 114 | + if (!body) { |
| 115 | + throw new EmptyResultError('linux-do/topic-content', `Topic ${id} does not contain a readable main post body.`); |
| 116 | + } |
| 117 | + |
| 118 | + return { |
| 119 | + content: buildTopicMarkdownDocument({ |
| 120 | + title: topic.title?.trim() ?? '', |
| 121 | + author: mainPost.username?.trim() ?? '', |
| 122 | + likes: typeof mainPost.like_count === 'number' ? mainPost.like_count : undefined, |
| 123 | + createdAt: toLocalTime(mainPost.created_at ?? ''), |
| 124 | + url: `${LINUX_DO_HOME}/t/${id}`, |
| 125 | + body, |
| 126 | + }), |
| 127 | + }; |
| 128 | +} |
| 129 | + |
| 130 | +async function fetchTopicPayload(page: IPage, id: number): Promise<unknown> { |
| 131 | + const result = await page.evaluate(`(async () => { |
| 132 | + try { |
| 133 | + const res = await fetch('/t/${id}.json?include_raw=true', { credentials: 'include' }); |
| 134 | + let data = null; |
| 135 | + try { |
| 136 | + data = await res.json(); |
| 137 | + } catch (_error) { |
| 138 | + data = null; |
| 139 | + } |
| 140 | + return { |
| 141 | + ok: res.ok, |
| 142 | + status: res.status, |
| 143 | + data, |
| 144 | + error: data === null ? 'Response is not valid JSON' : '', |
| 145 | + }; |
| 146 | + } catch (error) { |
| 147 | + return { |
| 148 | + ok: false, |
| 149 | + error: error instanceof Error ? error.message : String(error), |
| 150 | + }; |
| 151 | + } |
| 152 | + })()`) as FetchTopicResult | null; |
| 153 | + |
| 154 | + if (!result) { |
| 155 | + throw new CommandExecutionError('linux.do returned an empty browser response'); |
| 156 | + } |
| 157 | + |
| 158 | + if (result.status === 401 || result.status === 403) { |
| 159 | + throw new AuthRequiredError(LINUX_DO_DOMAIN, 'linux.do requires an active signed-in browser session'); |
| 160 | + } |
| 161 | + |
| 162 | + if (result.error === 'Response is not valid JSON') { |
| 163 | + throw new AuthRequiredError(LINUX_DO_DOMAIN, 'linux.do requires an active signed-in browser session'); |
| 164 | + } |
| 165 | + |
| 166 | + if (!result.ok) { |
| 167 | + throw new CommandExecutionError( |
| 168 | + result.error || `linux.do request failed: HTTP ${result.status ?? 'unknown'}`, |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + if (result.error) { |
| 173 | + throw new CommandExecutionError(result.error, 'Please verify your linux.do session is still valid'); |
| 174 | + } |
| 175 | + |
| 176 | + return result.data; |
| 177 | +} |
| 178 | + |
| 179 | +cli({ |
| 180 | + site: 'linux-do', |
| 181 | + name: 'topic-content', |
| 182 | + description: 'Get the main topic body as Markdown', |
| 183 | + domain: LINUX_DO_DOMAIN, |
| 184 | + strategy: Strategy.COOKIE, |
| 185 | + browser: true, |
| 186 | + defaultFormat: 'plain', |
| 187 | + args: [ |
| 188 | + { name: 'id', positional: true, type: 'int', required: true, help: 'Topic ID' }, |
| 189 | + ], |
| 190 | + columns: ['content'], |
| 191 | + func: async (page: IPage, kwargs) => { |
| 192 | + const id = Number(kwargs.id); |
| 193 | + if (!Number.isInteger(id) || id <= 0) { |
| 194 | + throw new CommandExecutionError(`Invalid linux.do topic id: ${String(kwargs.id ?? '')}`); |
| 195 | + } |
| 196 | + |
| 197 | + const payload = await fetchTopicPayload(page, id); |
| 198 | + return [extractTopicContent(payload, id)]; |
| 199 | + }, |
| 200 | +}); |
| 201 | + |
| 202 | +export const __test__ = { |
| 203 | + buildTopicMarkdownDocument, |
| 204 | + extractTopicContent, |
| 205 | + normalizeTopicPayload, |
| 206 | + toLocalTime, |
| 207 | +}; |
0 commit comments