Skip to content

Commit

Permalink
refactor: manual ugoira zip extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
rayriffy committed Oct 1, 2023
1 parent d5cd767 commit 50a8bed
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
70 changes: 51 additions & 19 deletions tools/ugoira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const bookmarksFilePath = path.join(
'bookmarks.json'
)

const queue = new PQueue({ concurrency: 20 })
const queue = new PQueue({ concurrency: 10 })

;(async () => {
let failures: number[] = []
Expand Down Expand Up @@ -52,22 +52,51 @@ const queue = new PQueue({ concurrency: 20 })
ugoiraCacheDirectory,
`${ugoira.id}.webp`
)

if (!fs.existsSync(targetEncodedFile)) {
try {
const metadataPromise = pixiv.ugoira.metadata({
const metadata = await pixiv.ugoira.metadata({
illust_id: ugoira.id,
r18: true,
restrict: 'private',
})
const downloadedGifPathPromise = pixiv.util.downloadZip(
`https://www.pixiv.net/en/artworks/${ugoira.id}`,
'.'

const fileName = path.basename(
metadata.ugoira_metadata.zip_urls.medium
)

const [metadata, downloadedGifPath] = await Promise.all([
metadataPromise,
downloadedGifPathPromise,
])

const fetchedZip = await fetch(
metadata.ugoira_metadata.zip_urls.medium,
{
method: 'GET',
headers: {
Referer: 'https://www.pixiv.net/',
},
}
)
.then(o => o.arrayBuffer())
.then(o => Buffer.from(o))

const targetExtractPath = path.join(__dirname, '.cache', 'ugoira')
const targetZipPath = path.join(targetExtractPath, fileName)
const targetUgoiraDirPath = path.join(
targetExtractPath,
ugoira.id.toString()
)

if (!fs.existsSync(targetExtractPath))
fs.mkdirSync(targetExtractPath, {
recursive: true,
})

await fs.writeFileSync(targetZipPath, fetchedZip)
await promiseSpawn(
'unzip',
['-d', ugoira.id.toString(), fileName],
{
cwd: targetExtractPath,
}
)

const command = 'img2webp'
const inputArgs = metadata.ugoira_metadata.frames
.map(frame => [
Expand All @@ -80,17 +109,20 @@ const queue = new PQueue({ concurrency: 20 })
])
.flat()
const outputArgs = ['-o', targetEncodedFile]

await promiseSpawn(command, [...inputArgs, ...outputArgs], {
cwd: downloadedGifPath,
})

await fs.promises.rm(downloadedGifPath, {
recursive: true,
cwd: targetUgoiraDirPath,
})

await Promise.all([
fs.promises.rm(targetUgoiraDirPath, {
recursive: true,
}),
fs.promises.rm(targetZipPath),
])
} catch (e) {
failures.push(ugoira.id)
// console.log(`fail: ${ugoira.id}`)
console.log(`fail: ${ugoira.id}`)
}
}
})
Expand Down
6 changes: 4 additions & 2 deletions tools/utils/promiseSpawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ export const promiseSpawn = (
options: SpawnOptions = {}
) =>
new Promise<number>((res, rej) => {
// console.log(`${cmd} ${args.join(' ')}`)

const executeProcess = spawn(cmd, args, options)

// executeProcess.stdout.on('data', data => {
// executeProcess.stdout?.on('data', data => {
// console.error(`stdout: ${data}`)
// })

// executeProcess.stderr.on('data', data => {
// executeProcess.stderr?.on('data', data => {
// console.error(`stderr: ${data}`)
// })

Expand Down

0 comments on commit 50a8bed

Please sign in to comment.