Skip to content

Instantly share code, notes, and snippets.

@chalkpe
Last active September 6, 2020 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chalkpe/f95748e65d42a4d943c1b181c9c2d348 to your computer and use it in GitHub Desktop.
Save chalkpe/f95748e65d42a4d943c1b181c9c2d348 to your computer and use it in GitHub Desktop.
const fs = require('fs')
const puppeteer = require('puppeteer-core')
const MAKER_ENDPOINT = `https://thtl1999.github.io/elf-knight-maker/`
const DATABASE_ENDPOINT = `https://www.db.yugioh-card.com/yugiohdb/card_search.action`
async function fetchCardsFromText (browser, text) {
const page = await browser.newPage()
await page.goto(MAKER_ENDPOINT, {waitUntil: 'networkidle0'})
await page.click('#reset-button')
await page.type('#user-string', text)
await page.click('#start-button')
await page.waitForSelector('#colored-text span')
return page.evaluate(() => [...document.querySelectorAll('#card-list a')].map(card => ({
name: card.textContent,
id: new URL(card.getAttribute('href')).searchParams.get('cid'),
letter: card.querySelector('span:not([style*="lightgrey"])').textContent
})))
}
function openCardImageStream (browser, card) {
return new Promise(async (resolve, reject) => {
const page = await browser.newPage()
await page.setRequestInterception(true)
await page.on('request', req => req.continue())
await page.on('response', async res => {
const {pathname, searchParams: params} = new URL(res.url())
if (pathname !== '/yugiohdb/get_image.action') return
if (params.get('cid') !== card.id || params.get('type') !== '2') return
resolve(await res.buffer())
await page.close()
})
try { await page.goto(`${DATABASE_ENDPOINT}?ope=2&cid=${card.id}&request_locale=ko`) } catch (err) { }
})
}
async function main (text) {
const browser = await puppeteer.launch({
headless: false,
args: ['--lang=ko-KR'],
executablePath: "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
})
const dir = text.replace(/\s/g, '_')
await fs.promises.mkdir(dir)
const cards = await fetchCardsFromText(browser, text)
await Promise.all(cards.map(async (card, index) => {
const buffer = await openCardImageStream(browser, card)
await fs.promises.writeFile(`${dir}\\${index.toString().padStart(3, '0')}-${card.letter}.png`, buffer)
}))
await browser.close()
}
try { main(`신이슈가르드 학살자 풀금단 해두고 공명 영식 2주차 맞이한 엘프 용기사`) } catch (e) { console.error(e) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment