Skip to content

Instantly share code, notes, and snippets.

@JeanOsorio
Forked from abigpotostew/index.js
Created November 8, 2021 20:50
Show Gist options
  • Save JeanOsorio/782ba9ae46488f393b227cd0d6e23cb2 to your computer and use it in GitHub Desktop.
Save JeanOsorio/782ba9ae46488f393b227cd0d6e23cb2 to your computer and use it in GitHub Desktop.
Gradis to GIF script
/**
* Gradis Token to GIF
* Install:
* * npm i puppeteer gifencoder png-js
* Run:
* * node index.js <token id>
* * node index.js 10360308020202100302110304,10350503030202000202140003,10160116030200020302040506
* tested on node v14.17.6
*/
const fs = require('fs');
const puppeteer = require('puppeteer');
const GIFEncoder = require('gifencoder');
const PNG = require('png-js');
const size = 800;
const factor = 1.5;
function decode(png) {
return new Promise(r => {
png.decode(pixels => r(pixels))
});
}
async function gifAddFrame(page, encoder) {
const borderD = (size * factor - size)
const pngBuffer = await page.screenshot({
clip: {
width: size / (factor),
height: size / (factor),
x: borderD/4+30,
y: 0,
}
});
const png = new PNG(pngBuffer);
await decode(png).then(pixels => encoder.addFrame(pixels));
}
const run = async (tokenId)=>{
const browser = await puppeteer.launch({
headless: true, slowMo: 0,
});
const page = await browser.newPage();
await page.setViewport({
width: Math.floor(size ),
height: Math.floor(size ),
deviceScaleFactor: factor
});
await page.goto('https://nft.gradis.art/?tokenid='+tokenId, {
waitUntil: ['domcontentloaded', 'networkidle2']
});
const anim = '#gradis-background'
const sleep = m => new Promise(r => setTimeout(r, m));
await page.click(anim)
await sleep(1500) //wait for gradis to load
// record gif
var encoder = new GIFEncoder(size, size);
encoder.createWriteStream()
.pipe(fs.createWriteStream(tokenId+'.gif'));
// setting gif encoder
encoder.start();
encoder.setRepeat(0);
encoder.setDelay(17);
encoder.setQuality(10); // default
for (let i = 0; i < 30; i++) {
await page.click(anim)
await page.click(anim)
await gifAddFrame(page, encoder);
}
encoder.finish();
await browser.close();
}
(async () => {
const args = process.argv.slice(2)
if(args.length!==1){
console.log("Usage: node index.js <tokenIds>")
process.exit(1)
}
const tokenIds = args[0].split(',')
for (let tokenId of tokenIds) {
await run(tokenId)
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment