Skip to content

Commit

Permalink
getRowAtIdx and /mysong/:name route init
Browse files Browse the repository at this point in the history
  • Loading branch information
kavun committed Mar 18, 2019
1 parent 22e3e69 commit 052f99a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 46 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const tweetRandom = require('./tweetRandom');
const sheetHelper = require('./sheetHelper');
const config = require('./config');

tweetRandom();
sheetHelper.init((rows, tabletop) => {
sheetHelper.tweetRandom();
});
5 changes: 5 additions & 0 deletions local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const app = require('./now.js')

app.listen(3000, function () {
console.log('Express running: http://localhost:3000')
})
33 changes: 28 additions & 5 deletions now.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
const schedule = require('node-schedule');
const tweetRandom = require('./tweetRandom');
const sheetHelper = require('./sheetHelper');
const config = require('./config');
const express = require('express');
const Liquid = require('liquidjs');

const app = express();

const engine = new Liquid({
root: __dirname, // for layouts and partials
extname: '.liquid'
});

// register liquid engine
app.engine('liquid', engine.express());
app.set('views', './views'); // specify the views directory
app.set('view engine', 'liquid'); // set to default

let lastTweet = '';

app.get('/', (req, res) => {
Expand All @@ -15,10 +26,22 @@ app.get('/', (req, res) => {
});

app.get('/tweet', (req, res) => {
tweetRandom((tweet) => {
lastTweet = tweet;
res.write(tweet);
res.end();
sheetHelper.init(() => {
sheetHelper.tweetRandom((tweet) => {
lastTweet = tweet;
res.write(tweet);
res.end();
});
});
});

app.get('/mysong/:name', function (req, res) {
sheetHelper.init(() => {
const row = sheetHelper.getRowAtIdx(12);
res.render('mysong', {
name: req.params.name,
spotifyuri: row.spotifyuri
});
});
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"dotenv": "^6.2.0",
"express": "^4.16.4",
"liquidjs": "^8.0.0",
"node-schedule": "^1.3.2",
"parse-int": "^1.0.3",
"random": "^2.0.13",
Expand Down
63 changes: 63 additions & 0 deletions sheetHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const Tabletop = require('tabletop');
const random = require('random');

const config = require('./config');
const bot = require('./bot');

let rows = null;
let tabletop = null;

function tweetRandom(callback = () => {}) {
const randomInt = random.int(0, rows.length - 1);
const row = getRowAtIdx(randomInt);

if (row == null || row[config.headerName] == null) {
console.log('unreadable row');
return;
}
const value = row[config.headerName];

console.log(`Random at idx:${randomInt} is "${value}"`);

bot.tweet(value, callback);
}

function getRowAtIdx(idx) {
if (rows == null || rows.length < 1) {
console.log('spreadsheet empty');
return;
}

console.log(`Found ${rows.length} rows`);

if (idx < 0 || idx >= rows.length) {
console.log('bad index, out of range');
return null;
}

const row = rows[idx];
if (row == null) {
console.log('unreadable row');
return null;
}

return row;
}

function init(callback = () => {}) {
Tabletop.init({
key: config.googleSheetUrl,
callback: (data, _tabletop) => {
rows = data;
tabletop = _tabletop;
callback(rows, tabletop);
},
simpleSheet: true
});
}

module.exports = {
init: init,
tweetRandom: tweetRandom,
getRowAtIdx: getRowAtIdx,
};
39 changes: 0 additions & 39 deletions tweetRandom.js

This file was deleted.

10 changes: 10 additions & 0 deletions views/mysong.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>My Song</title>
</head>
<body>
<div>{{ name }}</div>
<div>{{ spotifyuri }}</div>
</body>
</html>

0 comments on commit 052f99a

Please sign in to comment.