|
| 1 | +var spinitron = require('spinitron-spinpapi'); |
| 2 | +var moment = require('moment'); |
| 3 | +var express = require('express'); |
| 4 | +var mustache = require('mustache'); |
| 5 | +var fs = require('fs'); |
| 6 | + |
| 7 | +spinitron = new spinitron({ |
| 8 | + station: 'ksdt', |
| 9 | + userid: process.env['SPIN_USER'], |
| 10 | + secret: process.env['SPIN_SECRET'] |
| 11 | +}); |
| 12 | + |
| 13 | +var app = express(); |
| 14 | + |
| 15 | +app.use(express.static('static')); |
| 16 | + |
| 17 | + |
| 18 | +var cachedRender; |
| 19 | +var cacheTime; |
| 20 | + |
| 21 | +app.get('/', function (req, res) { |
| 22 | + |
| 23 | + if (cachedRender && moment(cacheTime).add(60, 'minutes').isAfter(moment())) { |
| 24 | + console.log("cached"); |
| 25 | + res.send(cachedRender); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + var weeks = []; |
| 30 | + |
| 31 | + var beginningOfThisWeek = moment().startOf('week'); |
| 32 | + |
| 33 | + weeks.push({ start: moment(beginningOfThisWeek)} ); |
| 34 | + |
| 35 | + for (var i = 0; i < 12; i++) { |
| 36 | + weeks.push({ start: moment(beginningOfThisWeek.subtract(1, 'week')) }); |
| 37 | + } |
| 38 | + |
| 39 | + weeks.forEach(function (week, i) { |
| 40 | + weeks[i].weekstr = |
| 41 | + "Week of " + week.start.format('MMM D'); |
| 42 | + }); |
| 43 | + |
| 44 | + var grid = fs.readFileSync('./grid.html', 'utf-8'); |
| 45 | + |
| 46 | + spinitron.getRegularShowsInfo({}, function(err, resp) { |
| 47 | + |
| 48 | + var shows = resp.results; |
| 49 | + |
| 50 | + function getPlaylistsForShow(show) { |
| 51 | + return new Promise(function (resolve, reject) { |
| 52 | + spinitron.getPlaylistsInfo( { ShowID: show['ShowID'], Num : 99 }, function (err, resp) { |
| 53 | + if (err) reject(err); |
| 54 | + else resolve(resp.results ? resp.results : []); |
| 55 | + }); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + var playlistsPromises = []; |
| 60 | + |
| 61 | + shows.forEach(function (show) { |
| 62 | + show.weeks = new Array(weeks.length); |
| 63 | + for (var i = 0; i < weeks.length; i++) |
| 64 | + show.weeks[i] = {}; |
| 65 | + playlistsPromises.push(new Promise(function (resolve, reject) { |
| 66 | + getPlaylistsForShow(show).then(function(playlists) { |
| 67 | + show.playlists = playlists && playlists.length ? playlists : []; |
| 68 | + }).then(resolve).catch(reject); |
| 69 | + })); |
| 70 | + }); |
| 71 | + |
| 72 | + Promise.all(playlistsPromises).then(function() { |
| 73 | + shows.forEach(function (show) { |
| 74 | + show.weeks.forEach(function (week, i) { |
| 75 | + show.playlists.forEach(function (playlist) { |
| 76 | + if (moment(playlist['PlaylistDate']).isBetween(weeks[i].start, moment(weeks[i].start).add(1, 'week'))) { |
| 77 | + week.here = 'here'; |
| 78 | + week.link = 'https://spinitron.com/radio/playlist.php?station=ksdt&playlist='+playlist['PlaylistID']; |
| 79 | + } |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + }).then(function() { |
| 84 | + cachedRender = mustache.render(grid, { weekns: weeks, shows: shows }); |
| 85 | + cacheTime = moment(); |
| 86 | + res.send(cachedRender); |
| 87 | + }).catch(function(err) { |
| 88 | + console.log(err); |
| 89 | + res.send('sorry, there was an error.'); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | + |
| 95 | +app.listen(process.env.PORT || 3000); |
0 commit comments