Created
July 12, 2019 20:52
-
-
Save bencooper222/0098f4251108115932eea92abc7475f9 to your computer and use it in GitHub Desktop.
This pulls comic starting from a user-defined day and getting a user-defined number of days after that. See early comments for licensing information.
This file contains 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
// MIT License Copyright (c) 2019 Benjamin Cooper | |
// I take no responsibility for your use of this code. | |
// you'll have to install these modules with npm/yarn | |
const download = require('image-downloader'); | |
const fetch = require('node-fetch'); | |
const cheerio = require('cheerio'); | |
Date.prototype.addDays = function(days) { | |
var date = new Date(this.valueOf()); | |
date.setDate(date.getDate() + days); | |
return date; | |
}; | |
const getImageUrl = async pageUrl => { | |
const $ = cheerio.load(await (await fetch(pageUrl)).text()); | |
return $('img.img-comic')[0].attribs.src; | |
}; | |
const getImages = async (start, numDays) => { | |
const url = 'https://dilbert.com/strip/'; | |
const date = new Date(start); | |
for (let i = 0; i < numDays; i++) { | |
const datePath = `${date.getFullYear()}-${numberToDouble(date.getMonth() + 1)}-${numberToDouble( | |
date.getDate(), | |
)}`; | |
console.log(datePath); | |
const imageUrl = `https://${(await getImageUrl(`${url}${datePath}`)).slice(2)}.gif`; | |
download | |
.image({ | |
url: imageUrl, | |
dest: `./images/${datePath}.gif`, | |
}) | |
.then(({ filename, image }) => { | |
console.log('File saved to', filename); | |
}) | |
.catch(err => { | |
console.error(err); | |
}); | |
date.setDate(date.getDate() + 1); | |
} | |
}; | |
const numberToDouble = num => { | |
return num < 10 ? '0' + num : num; | |
}; | |
getImages('2017-09-19', 30); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment