Skip to content

Commit

Permalink
🚭
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Oct 7, 2024
1 parent b53ae5a commit ab5c84b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 95 deletions.
80 changes: 2 additions & 78 deletions src/export-book-pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,12 @@ import fs from 'node:fs'
import fsp from 'node:fs/promises'
import path from 'node:path'

import type { Metadata } from 'playwright/test'
import PDFDocument from 'pdfkit'

import type { ContentChunk } from './types'
import { assert } from './utils'

interface ContentChunk {
index: number
page: number
text: string
screenshot: string
}

interface TocItem {
title: string
page?: number
location?: number
total: number
}

interface PageChunk {
index: number
page: number
total: number
screenshot: string
}

interface Meta {
ACR: string
asin: string
authorList: Array<string>
bookSize: string
bookType: string
cover: string
language: string
positions: {
cover: number
srl: number
toc: number
}
publisher: string
refEmId: string
releaseDate: string
sample: boolean
title: string
version: string
startPosition: number
endPosition: number
}

interface Info {
clippingLimit: number
contentChecksum: any
contentType: string
contentVersion: string
deliveredAsin: string
downloadRestrictionReason: any
expirationDate: any
format: string
formatVersion: string
fragmentMapUrl: any
hasAnnotations: boolean
isOwned: boolean
isSample: boolean
kindleSessionId: string
lastPageReadData: {
deviceName: string
position: number
syncTime: number
}
manifestUrl: any
originType: string
pageNumberUrl: any
requestedAsin: string
srl: number
}

interface Metadata {
info: Info
meta: Meta
toc: TocItem[]
pages: PageChunk[]
}

async function main() {
const asin = process.env.ASIN
assert(asin, 'ASIN is required')
Expand Down
14 changes: 4 additions & 10 deletions src/extract-kindle-book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { input } from '@inquirer/prompts'
import delay from 'delay'
import { chromium, type Locator } from 'playwright'

import type { Info, Meta, Metadata, PageChunk } from './types'
import {
assert,
deromanize,
Expand All @@ -26,13 +27,6 @@ interface TocItem extends PageNav {
locator?: Locator
}

interface PageChunk {
index: number
page: number
total: number
screenshot: string
}

async function main() {
const asin = getEnv('ASIN')
const amazonEmail = getEnv('AMAZON_EMAIL')
Expand Down Expand Up @@ -61,8 +55,8 @@ async function main() {
})
const page = await context.newPage()

let info: any
let meta: any
let info: Info | undefined
let meta: Meta | undefined

page.on('response', async (response) => {
try {
Expand Down Expand Up @@ -348,7 +342,7 @@ async function main() {
} while (true)
} while (true)

const result = { info, meta, toc, pages }
const result: Metadata = { info: info!, meta: meta!, toc, pages }
await fs.writeFile(
path.join(outDir, 'metadata.json'),
JSON.stringify(result, null, 2)
Expand Down
8 changes: 1 addition & 7 deletions src/transcribe-book-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ import { globby } from 'globby'
import { OpenAIClient } from 'openai-fetch'
import pMap from 'p-map'

import type { ContentChunk } from './types'
import { assert, getEnv } from './utils'

type ContentChunk = {
index: number
page: number
text: string
screenshot: string
}

async function main() {
const asin = getEnv('ASIN')
assert(asin, 'ASIN is required')
Expand Down
77 changes: 77 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
export interface ContentChunk {
index: number
page: number
text: string
screenshot: string
}

export interface TocItem {
title: string
page?: number
location?: number
total: number
}

export interface PageChunk {
index: number
page: number
total: number
screenshot: string
}

export interface Meta {
ACR: string
asin: string
authorList: Array<string>
bookSize: string
bookType: string
cover: string
language: string
positions: {
cover: number
srl: number
toc: number
}
publisher: string
refEmId: string
releaseDate: string
sample: boolean
title: string
version: string
startPosition: number
endPosition: number
}

export interface Info {
clippingLimit: number
contentChecksum: any
contentType: string
contentVersion: string
deliveredAsin: string
downloadRestrictionReason: any
expirationDate: any
format: string
formatVersion: string
fragmentMapUrl: any
hasAnnotations: boolean
isOwned: boolean
isSample: boolean
kindleSessionId: string
lastPageReadData: {
deviceName: string
position: number
syncTime: number
}
manifestUrl: any
originType: string
pageNumberUrl: any
requestedAsin: string
srl: number
}

export interface Metadata {
info: Info
meta: Meta
toc: TocItem[]
pages: PageChunk[]
}

0 comments on commit ab5c84b

Please sign in to comment.