New episode of Simpsons everyday

If you are fun of Simpsons you can get everyday a new episode 😃

// Name: simpsons
// Description: Show next episode of Simpsons
// Author: Daniel Ostapenko
// Twitter: @denieler
import '@johnlindquist/kit'
const simpsonsDB = await db('simpsons', {
season: 7,
episode: 0,
episodesData: []
})
if (!simpsonsDB.episodesData.length) {
const wtf = await npm('wtf_wikipedia')
const doc = await wtf.fetch('List of The Simpsons episodes')
let episodes = doc.section('Episodes')
let episodesText = episodes.text()
const episodesRegexp = /episodes(\d*) = (\d*)/g
let episodesData = episodesText.match(episodesRegexp)
episodesData = episodesData.map(x => {
const [_season, _numberOfEpisodes] = x // episodes1 = 13
.replace('episodes', '') // '1 = 13'
.split(' = ') // ['1', '13']
return {
season: parseInt(_season),
numberOfEpisodes: parseInt(_numberOfEpisodes)
}
})
simpsonsDB.episodesData = episodesData
await simpsonsDB.write()
}
let season = simpsonsDB.season
let episode = simpsonsDB.episode
const seasonEpisodes = simpsonsDB.episodesData.find(x => x.season === season)
if (episode + 1 > seasonEpisodes.numberOfEpisodes) {
const seasonNumbers = simpsonsDB.episodesData
.map(x => x.season)
const maxSeason = seasonNumbers[seasonNumbers.length - 1]
if (season + 1 > maxSeason) {
season = 7
episode = 1
} else {
season = season + 1
episode = 1
}
} else {
episode = episode + 1
}
simpsonsDB.season = season
simpsonsDB.episode = episode
await simpsonsDB.write()
const episodeUrl = `https://www.wcofun.com/the-simpsons-season-${season}-episode-${episode}`
browse(episodeUrl)