You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.5 KiB
115 lines
2.5 KiB
/* |
|
* main page |
|
*/ |
|
|
|
var sqlite3 = require('sqlite3'); |
|
var path = require('path'); |
|
var db; // make it global |
|
|
|
/** |
|
* db name |
|
*/ |
|
function dbName() { |
|
return path.join(__dirname, '../../db/lyrics.sq3'); |
|
} |
|
|
|
/** |
|
* turn a phrase into a list of acronyms |
|
*/ |
|
function acronymize(phrase) { |
|
var letters = []; |
|
phrase.split(' ').forEach(function (elem, idx, array) { |
|
letters.push(elem[0].toUpperCase()); |
|
}); |
|
|
|
return letters.join(''); |
|
} |
|
|
|
/*********************************************************************** |
|
* Main page |
|
**********************************************************************/ |
|
|
|
/** |
|
* Pick a song |
|
*/ |
|
function getSong(res) { |
|
var name = dbName(); |
|
var db = null; |
|
var song = null; |
|
var artist = null; |
|
var lyrics = null; |
|
|
|
function pickSong() { |
|
console.log('Picking song...'); |
|
db.get('SELECT * FROM songs WHERE done_in IS NULL ORDER BY random() LIMIT 1', function (err, selectSong) { |
|
if (err) { |
|
throw err; |
|
} |
|
|
|
song = selectSong; |
|
console.log(song); |
|
pickArtist(); |
|
}); |
|
} |
|
|
|
function pickArtist() { |
|
console.log('Retrieving artist for song...'); |
|
db.get('SELECT * FROM artists WHERE id = ' + song.artist, function (err, selectArtist) { |
|
if (err) { |
|
throw err; |
|
} |
|
|
|
artist = selectArtist; |
|
console.log(artist); |
|
pickLyrics(); |
|
}); |
|
} |
|
|
|
function pickLyrics() { |
|
console.log('Retrieving lyrics for song... '); |
|
db.get('SELECT * FROM lyrics WHERE song = ' + song.id, function (err, selectLyrics) { |
|
if (err) { |
|
throw err; |
|
} |
|
|
|
lyrics = selectLyrics; |
|
console.log(lyrics); |
|
answer(); |
|
}); |
|
} |
|
|
|
function answer() { |
|
var params = { |
|
title: 'Round', |
|
lyrics: lyrics.lyrics, |
|
song_title: song.title, |
|
song_artist: artist.name, |
|
acronym_title: acronymize(song.title), |
|
acronym_artist: acronymize(artist.name), |
|
id: song.id |
|
}; |
|
res.render('index', params); |
|
} |
|
|
|
console.log('Opening the database ' + name + '...'); |
|
db = new sqlite3.Database(name, sqlite3.OPEN_READWRITE, function (error) { |
|
if (error) { |
|
throw error; |
|
} |
|
|
|
pickSong(); |
|
}); |
|
} |
|
|
|
exports.index = function (req, res) { |
|
getSong(res); |
|
}; |
|
|
|
|
|
/*********************************************************************** |
|
* Support for the small box with the "others" picks |
|
**********************************************************************/ |
|
exports.others = function (req, res) { |
|
console.log('Artist: ' + req.body.artist); |
|
console.log('Song: ' + req.body.song); |
|
};
|
|
|