Created
February 25, 2020 04:17
-
-
Save jbass86/50c2bda2d5580a57929087c5e4a0883a to your computer and use it in GitHub Desktop.
dilbert2.js
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 get = require("simple-get"); | |
const cheerio = require("cheerio"); | |
let dilbertData = {}; | |
function pad(num) { | |
if (num < 10) { | |
return "0" + num; | |
} else { | |
return num; | |
} | |
} | |
function getComicImage(date) { | |
return new Promise((resolve, reject) => { | |
const url = `https://dilbert.com/strip/${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}` | |
console.log("url is " + url); | |
get.concat(url, (error, response, data) => { | |
if (error) { | |
console.log("there was error"); | |
console.log(error); | |
return; | |
} | |
let $ = cheerio.load(data.toString()); | |
let thing = $(".img-comic-container img"); | |
let imageURL = "http:" + thing[0].attribs.src; | |
console.log(imageURL); | |
get.concat(imageURL, (error, response, imageData) => { | |
console.log(imageData); | |
resolve(imageData); | |
}); | |
}); | |
}); | |
} | |
const day = 86400000; | |
let today = new Date(); | |
let yesterday = new Date(Date.now() - day); | |
let daybefore = new Date(Date.now() - (day * 2)); | |
getComicImage(today).then((imageData) => { | |
dilbertData["today"] = imageData; | |
}); | |
getComicImage(yesterday).then((imageData) => { | |
dilbertData["yesterday"] = imageData; | |
}); | |
getComicImage(daybefore).then((imageData) => { | |
dilbertData["daybefore"] = imageData; | |
}); | |
const express = require('express') | |
const app = express() | |
const port = 3000 | |
const markup = ` | |
<html> | |
<body> | |
<div> | |
<img src="today"> | |
</div> | |
<div> | |
<img src="yesterday"> | |
</div> | |
<div> | |
<img src="daybefore"> | |
</div> | |
</body> | |
</html> | |
`; | |
app.get('/', (req, res) => res.send(markup)); | |
app.get('/today', (req, res) => res.send(dilbertData["today"])); | |
app.get('/yesterday', (req, res) => res.send(dilbertData["yesterday"])); | |
app.get('/daybefore', (req, res) => res.send(dilbertData["daybefore"])); | |
app.listen(port, () => console.log(`Listening on port ${port}!`)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment