Skip to content

Commit 10c4a43

Browse files
committed
initial
0 parents  commit 10c4a43

File tree

6 files changed

+198
-0
lines changed

6 files changed

+198
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

grid.html

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Attendance</title>
6+
<link rel="stylesheet" href="./pure-min.css">
7+
<link rel="stylesheet" href="./style.css">
8+
</head>
9+
<body>
10+
<table class="pure-table pure-table-striped">
11+
<thead>
12+
<tr>
13+
<th class="showname" >Show Name</th>
14+
{{ #weekns }}
15+
<th class="week-title"> {{ weekstr }} </th>
16+
{{ /weekns }}
17+
</tr>
18+
</thead>
19+
<tbody>
20+
{{ #shows }}
21+
<tr>
22+
<th class="showname"> {{ ShowName }} </th>
23+
{{ #weeks }}
24+
<th class="wk {{ here }}"><a href="{{ link }}">playlist</a></th>
25+
{{ /weeks }}
26+
</tr>
27+
{{ /shows }}
28+
</table>
29+
</body>
30+
</html>

index.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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);

package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "attendance",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node index.js"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"express": "^4.13.4",
14+
"moment": "^2.12.0",
15+
"mustache": "^2.2.1",
16+
"spinitron-spinpapi": "0.0.3"
17+
}
18+
}

0 commit comments

Comments
 (0)