|
| 1 | +import { readFile, readdir, unlink, writeFile } from "fs/promises" |
| 2 | +import { join } from "path" |
| 3 | + |
| 4 | +import type { Browser } from "playwright" |
| 5 | + |
| 6 | +import { ARCHIVE_DIR, HOOK_SCRIPT, SCRIPT, ZIP_SCRIPT } from "./constants.js" |
| 7 | + |
| 8 | +export async function getGeminiIdsFromReadme(readmePath: string): Promise<Set<string>> { |
| 9 | + const readme = await readFile(readmePath, "utf8") |
| 10 | + const matches = readme.matchAll(/https:\/\/(?:gemini\.google\.com|g\.co\/gemini)\/share\/(?<id>[^\/\s]+)/g) |
| 11 | + |
| 12 | + const ids = new Set<string>() |
| 13 | + for (const match of matches) ids.add(match.groups!.id!) |
| 14 | + return ids |
| 15 | +} |
| 16 | + |
| 17 | +export async function getArchivedConversations(archiveDir: string): Promise<Map<string, string>> { |
| 18 | + const files = await readdir(archiveDir) |
| 19 | + const map = new Map<string, string>() |
| 20 | + |
| 21 | + for (const file of files) { |
| 22 | + const match = file.match(/^(?<id>[^-]+) - .+\.html$/) |
| 23 | + if (match) map.set(match.groups?.["id"] as string, file) |
| 24 | + } |
| 25 | + return map |
| 26 | +} |
| 27 | + |
| 28 | +export async function deleteFile(filePath: string) { |
| 29 | + try { |
| 30 | + await unlink(filePath) |
| 31 | + return true |
| 32 | + } catch { |
| 33 | + return false |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +export async function archiveConversation(browser: Browser, id: string) { |
| 38 | + const url = `https://gemini.google.com/share/${id}` |
| 39 | + let page |
| 40 | + |
| 41 | + try { |
| 42 | + page = await browser.newPage({ bypassCSP: true }) |
| 43 | + // https://github.com/gildas-lormeau/single-file-cli/blob/v2.0.75/lib/cdp-client.js#L235-L243 |
| 44 | + await page.addInitScript({ content: HOOK_SCRIPT }) |
| 45 | + await page.addInitScript({ content: SCRIPT }) |
| 46 | + |
| 47 | + await page.goto(url) |
| 48 | + await page.waitForSelector("message-content", { timeout: 20000 }) |
| 49 | + await page.waitForTimeout(3000) |
| 50 | + |
| 51 | + // Click all visible elements with text starting with "Show" |
| 52 | + const showButtons = await page.getByText("Show").all() |
| 53 | + for (const btn of showButtons) { |
| 54 | + if (await btn.isVisible()) await btn.click() |
| 55 | + } |
| 56 | + |
| 57 | + const title = // @ts-expect-error |
| 58 | + (await page.evaluate(() => document.querySelector("h1 > strong").textContent, "")).substring(0, 100) |
| 59 | + |
| 60 | + // https://github.com/gildas-lormeau/single-file-cli/blob/v2.0.75/single-file-cli-api.js#L258 |
| 61 | + // https://github.com/gildas-lormeau/single-file-cli/blob/v2.0.75/lib/cdp-client.js#L332 |
| 62 | + // https://github.com/gildas-lormeau/single-file-core/blob/212a657/single-file.js#L125 |
| 63 | + // @ts-expect-error |
| 64 | + const pageData = await page.evaluate(async options => await singlefile.getPageData(options), { |
| 65 | + zipScript: ZIP_SCRIPT |
| 66 | + }) |
| 67 | + |
| 68 | + const fileContent = pageData.content |
| 69 | + .replaceAll(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>\s*/gi, "") |
| 70 | + .replaceAll(/@font-face\s*{\s*[^}]+font-family:\s*codicon[^}]+}/gi, "") |
| 71 | + .replaceAll(/@font-face\s*\{[^}]*\}/g, (fontFaceRule: string) => { |
| 72 | + const fontFamilyMatch = fontFaceRule.match(/font-family:\s*(?<quote>['"]?)(?<fontFamily>[^'"]+)\k<quote>;/) |
| 73 | + |
| 74 | + if (fontFamilyMatch && fontFamilyMatch.groups?.fontFamily) { |
| 75 | + const fontFamily = fontFamilyMatch.groups?.fontFamily.trim() |
| 76 | + if (fontFamily === "Google Symbols") return fontFaceRule |
| 77 | + if (pageData.content.includes(`class="katex"`) && fontFamily.startsWith("KaTeX")) return fontFaceRule |
| 78 | + } |
| 79 | + |
| 80 | + return "" |
| 81 | + }) |
| 82 | + |
| 83 | + // Remove illegal filename chars |
| 84 | + const sanitizedTitle = title.replace(/[\\/:*?"<>|\n]/g, "") |
| 85 | + const filepath = join(ARCHIVE_DIR, `${id} - ${sanitizedTitle}.html`) |
| 86 | + await writeFile(filepath, fileContent) |
| 87 | + |
| 88 | + await page.close() |
| 89 | + } catch (err) { |
| 90 | + if (page) await page.close() |
| 91 | + |
| 92 | + console.error(`Failed to archive ${id}: ${(err as Error).message}`) |
| 93 | + throw err |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export function buildCommitMessage(added: string[], deleted: string[]): string { |
| 98 | + let msg = "chore: Automatic conversation archive\n" |
| 99 | + |
| 100 | + if (added.length > 0) msg += `\nAdded conversations: ${added.join(", ")}` |
| 101 | + if (deleted.length > 0) msg += `\nDeleted conversations: ${deleted.join(", ")}` |
| 102 | + |
| 103 | + return msg |
| 104 | +} |
0 commit comments