Created
December 2, 2020 02:44
-
-
Save nikki93/66bc5ad58ef08fc1826bcf13529d97a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'); | |
const fs = require('fs'); | |
const deckIds = [ | |
'dDhaN6EmR', | |
'GXW_KkKzT', | |
'ae5b8c7e-fd3a-4835-b972-fbf0bed2b81c', | |
'cbWFJghWo', | |
'z3NzziPIo', | |
]; | |
deckIds.forEach((deckId, deckIndex) => { | |
// Ask our graphql API | |
const encoded = JSON.stringify({ | |
query: ` | |
query($deckId: ID!) { | |
deck(deckId: $deckId) { | |
cards { | |
sceneData | |
} | |
} | |
} | |
`, | |
variables: { deckId }, | |
}); | |
request.post( | |
'https://api.castle.xyz/graphql', | |
{ | |
headers: { | |
'Content-Type': 'application/json', | |
Accept: 'application/json', | |
'Content-Length': encoded.length, | |
Connection: 'keep-alive', | |
}, | |
body: encoded, | |
}, | |
(err, res, body) => { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
// Collect by title (clashes are just clobbered) | |
const entries = {}; | |
const decoded = JSON.parse(res.body); | |
decoded.data.deck.cards.forEach((card) => { | |
Object.values(card.sceneData.snapshot.library).forEach((entry) => { | |
entries[entry.title] = entry; | |
}); | |
}); | |
// Save '.png's for entries that have image data | |
Object.values(entries).forEach((entry) => { | |
if (entry.base64Png) { | |
const filename = `d${deckIndex}-${entry.title.replace(/\s/g, '-')}.png`; | |
fs.writeFile(filename, entry.base64Png, 'base64', (err) => { | |
if (err) { | |
console.log(err); | |
} | |
}); | |
} | |
}); | |
} | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment