Skip to content

Instantly share code, notes, and snippets.

@dustinrouillard
Created July 26, 2020 07:19
Show Gist options
  • Save dustinrouillard/cf1044f3554cc800ff8ea829eb141bad to your computer and use it in GitHub Desktop.
Save dustinrouillard/cf1044f3554cc800ff8ea829eb141bad to your computer and use it in GitHub Desktop.
Cloudflare worker that runs kush.pics
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
});
const default_image = '';
const kushes = [
'https://www.mensjournal.com/wp-content/uploads/mf/weed-marijuana.jpg',
'https://dynaimage.cdn.cnn.com/cnn/c_fill,g_auto,w_1200,h_675,ar_16:9/https%3A%2F%2Fcdn.cnn.com%2Fcnnnext%2Fdam%2Fassets%2F191031084204-marijuana-flower-stock.jpg',
'https://media.npr.org/assets/img/2019/03/19/weed-bud-bc500c4866252c287b190cd5fe679238b2a91982.jpg',
'https://d279m997dpfwgl.cloudfront.net/wp/2018/06/0605_tweed-canada06.jpg',
'https://www.rollingstone.com/wp-content/uploads/2018/06/vermont-legal-weed-guide-4d008b70-ea84-4ebf-b6c2-b6af019fd906.jpg',
'https://cdn.arstechnica.net/wp-content/uploads/2020/07/GettyImages-1137890260-800x533.jpg',
'https://specials-images.forbesimg.com/imageserve/667618280/960x0.jpg?fit=scale',
'https://hightimes.com/wp-content/uploads/2018/07/how-grind-weed-without-grinder-featured.jpg',
'https://www.todaysparent.com/wp-content/uploads/2018/04/should-you-try-weed-to-soothe-your-pregnancy-woes.jpg',
'https://cdn.the-scientist.com/assets/articleNo/65624/aImg/31165/cannabis-s.jpg',
'https://static.independent.co.uk/s3fs-public/thumbnails/image/2015/08/17/13/marijuanagetty.jpg',
'https://hightimes.com/wp-content/uploads/2018/09/colorful-weed-strains-brighten-your-day-featured.jpg',
'https://g.foolcdn.com/editorial/images/514096/cannabis-buds-marijuana-weed-pot-cash-money-legal-canada-us-getty.jpg',
'https://www.citynews1130.com/wp-content/blogs.dir/sites/9/2018/09/10/Ep53_WomenWeed.jpg',
'https://images.agoramedia.com/wte3.0/gcms/Marijuana-and-Breastfeeding-722x406.jpg',
'https://grist.files.wordpress.com/2017/10/highpricecheapweed.jpg',
'https://www.citynews1130.com/wp-content/blogs.dir/sites/9/2018/10/19/Ep81_PotQs.jpg',
'https://www.verywellmind.com/thmb/2sogEr3gNemXNC2pzVyQTxQ1fgQ=/1280x853/filters:no_upscale():max_bytes(150000):strip_icc()/marijuana-454601963-resized-569fd2345f9b58eba4ad583d.jpg',
'https://static.highsnobiety.com/thumbor/MJZPRrNvOxbfAUXpaETKUVabjiE=/fit-in/480x320/smart/static.highsnobiety.com/wp-content/uploads/2016/08/21165846/weed-accounts-main.jpg'
];
async function fetchImage() {
const imageUrl = kushes[Math.floor(Math.random() * kushes.length)];
let imageFile = await fetch(imageUrl);
let type = imageFile.headers.get('content-type');
if (type.includes('text/plain')) {
imageUrl = default_image;
imageFile = await fetch(default_image);
type = imageFile.headers.get('content-type');
}
let { readable, writable } = new TransformStream();
imageFile.body.pipeTo(writable);
return { url: imageUrl, file: readable, type };
}
async function handleRequest(request) {
const { url, file, type } = await fetchImage();
return new Response(file, {
headers: {
'x-original-url': url,
'content-type': type
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment