63 lines
2.0 KiB
JavaScript
63 lines
2.0 KiB
JavaScript
const nodeHtmlToImage = require('node-html-to-image');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
async function generateQuote(config, outputPath = 'quote.png') {
|
|
const htmlTemplate = fs.readFileSync(path.join(__dirname, 'quote.html'), 'utf8');
|
|
|
|
const configScript = `
|
|
<script>
|
|
window.tweetConfig = ${JSON.stringify(config)};
|
|
</script>
|
|
`;
|
|
|
|
const modifiedHtml = htmlTemplate.replace('</head>', `${configScript}</head>`);
|
|
|
|
await nodeHtmlToImage({
|
|
output: outputPath,
|
|
html: modifiedHtml,
|
|
puppeteerArgs: {
|
|
args: ['--no-sandbox'],
|
|
defaultViewport: {
|
|
width: 3240,
|
|
height: 3240
|
|
}
|
|
},
|
|
beforeScreenshot: async (page) => {
|
|
await new Promise(resolve => setTimeout(resolve, 500));
|
|
}
|
|
});
|
|
|
|
console.log(`Quote generated: ${outputPath}`);
|
|
}
|
|
|
|
|
|
// Example usage
|
|
const exampleTweet = {
|
|
displayName: "Geoff Marshall",
|
|
username: "@geofftech",
|
|
avatarUrl: "https://pbs.twimg.com/profile_images/1295490618140110848/Fu4chISB_x96.jpg",
|
|
text: "Does anyone else find it immensely satisfying when you turn a pocket inside out and get rid of the crumbs and fluff stuck in the bottom.",
|
|
imageUrl: null,
|
|
timestamp: "10:04 AM · Jul 11, 2017",
|
|
viewsCount: "128K"
|
|
};
|
|
|
|
const exampleTweetWithImage = {
|
|
displayName: "miss katie",
|
|
username: "@katiopolis",
|
|
avatarUrl: "https://pbs.twimg.com/profile_images/2004569144893554688/KaYjqylC_x96.jpg",
|
|
text: "omg i brought these watches home because none of them worked but i thought id hang onto them and my dad fixed and polished them all for me 😭 i love him so much",
|
|
imageUrl: "https://pbs.twimg.com/media/G9Wtk1xWcAA-WMZ?format=jpg&name=large",
|
|
timestamp: "5:58 PM · Dec 29, 2025",
|
|
viewsCount: "1.1M"
|
|
};
|
|
|
|
// Generate both examples
|
|
(async () => {
|
|
await generateQuote(exampleTweet, 'quote-no-image.png');
|
|
await generateQuote(exampleTweetWithImage, 'quote-with-image.png');
|
|
})();
|
|
|
|
module.exports = { generateQuote };
|