From e6d655261d35575a66bbc1327b744ba8b70020bf Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 5 Sep 2013 10:02:56 -0300 Subject: [PATCH] removed a bunch of duplicated songs --- db/db.py | 5 +- lyrics.yaml | 130 +------------------------------- lyricsnode/routes/round.js | 144 ++++++++++++++++++++++++++---------- lyricsnode/views/index.html | 1 + 4 files changed, 110 insertions(+), 170 deletions(-) diff --git a/db/db.py b/db/db.py index 381ac84..c217657 100644 --- a/db/db.py +++ b/db/db.py @@ -36,8 +36,9 @@ print '\tSongs...' cursor.execute('''CREATE TABLE songs ( id INTEGER PRIMARY KEY, title TEXT, - artist INTEGER CONSTRAINT aritst_song REFERENCES artists (id), - done_in TEXT + artist INTEGER CONSTRAINT artist_song REFERENCES artists (id), + done_in TEXT, + CONSTRAINT no_repeats UNIQUE (artist, title) ON CONFLICT ABORT )''') print '\tLyrics...' diff --git a/lyrics.yaml b/lyrics.yaml index 0fcf981..40b1027 100644 --- a/lyrics.yaml +++ b/lyrics.yaml @@ -209,22 +209,6 @@ Nothing ever lasts forever - -- - artist: Technotronic - song: Pump Up the Jam - done: true - lyrics: > - Yo Come On Move This - - Shake That Body - - Shake That Body - - People dont you know, dont you know - - its about time - - artist: Cazuza song: Exagerado @@ -1122,13 +1106,6 @@ If you feel like letting go (hold on) When you think you've had too much of this life, well hang on - -- - artist: cazuza - song: exagerado - done: true - lyrics: > - - artist: survivor song: eye of the tiger @@ -1317,13 +1294,6 @@ song: in the end done: true lyrics: > - -- - artist: culture club - song: karma chameleon - done: true - lyrics: > - - artist: bon jovi song: livin' on a prayer @@ -1832,18 +1802,11 @@ Leave the real one far behind And we can dance -- - artist: natalie imbruglia - song: torn - done: true - lyrics: > - - artist: spin doctors song: two princes done: true lyrics: > - - artist: chico buarque song: vai passar @@ -6081,11 +6044,6 @@ song: divida done: true lyrics: > -- - artist: billy idol - song: eyes without a face - done: true - lyrics: > - artist: leona lewis song: bleeding love @@ -7246,7 +7204,7 @@ - artist: natalie imbruglia song: torn - done: false + done: true lyrics: > I thought I saw a man brought to life @@ -7331,92 +7289,6 @@ Of the cross I bear that you gave to me , , -- - artist: bon jovi - song: livin' on a prayer - done: false - lyrics: > - Once upon a time - - Not so long ago - - - - Tommy used to work on the docks - - Union's been on strike - - Hes down on his luck...it's tough, so tough - - Gina works the diner all day - - Working for her man, she brings home her pay - - For love - for love - - She says: We've got to hold on to what we've got - - 'Cause it doesn't make a difference - - If we make it or not - - We've got each other and that's a lot - - For love - we'll give it a shot - - - - Oh, we're half way there - - Whoah, - - Take my hand, we'll make it, I swear - - Whoah, -- - artist: europe - song: the final countdown - done: false - lyrics: > - We're leaving together - - But still it's farewell - - And maybe we'll come back, - - To earth, who can tell? - - - - I guess there is no one to blame - - We're leaving ground (leaving ground) - - Will things ever be the same again? - - It's . - - - - . - - - - Oh, We're heading for Venus (Venus) - - And still we stand tall - - Cause maybe they've seen us - - And welcome us all, yeah - - With so many light years to go - - And things to be found (to be found) - - I'm sure that we'll all miss her so - - It's . - artist: gotye song: somebody that i used to know diff --git a/lyricsnode/routes/round.js b/lyricsnode/routes/round.js index a8ffe14..97b2133 100644 --- a/lyricsnode/routes/round.js +++ b/lyricsnode/routes/round.js @@ -4,18 +4,18 @@ var sqlite3 = require('sqlite3'); var path = require('path'); -var db; // make it global +var db = null; // make it global -/** - * db name - */ +/********************************************************************** + * Helper functions + *********************************************************************/ + +/** db name */ function dbName() { return path.join(__dirname, '../../db/lyrics.sq3'); } -/** - * turn a phrase into a list of acronyms - */ +/** turn a phrase into a list of acronyms */ function acronymize(phrase) { var letters = []; phrase.split(' ').forEach(function (elem, idx, array) { @@ -25,31 +25,44 @@ function acronymize(phrase) { return letters.join(''); } -/*********************************************************************** - * Main page - **********************************************************************/ - -/** - * Pick a song +/********************************************************************** + * Open the database + *********************************************************************/ +console.log('Opening the database ' + dbName() + '...'); +db = new sqlite3.Database(dbName(), sqlite3.OPEN_READWRITE, function (error) { + if (error) { + db = null; // keep it null, to point that it is broken + } +}); + +/** small note: the db structure: + * artists: + * id int primary key + * name text unique + * + * songs: + * id int primary key + * title text + * artist int + * done_in text + * + * lyrics: + * id int primary key + * song int + * lyrics text */ -function getSong(res) { - var name = dbName(); - var db = null; + +/********************************************************************** + * Select a song and display it + *********************************************************************/ +exports.index = function (req, res) { 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(); - }); + if (!db) { + res.render('db-error'); + return; } function pickArtist() { @@ -91,25 +104,78 @@ function getSong(res) { res.render('index', params); } - console.log('Opening the database ' + name + '...'); - db = new sqlite3.Database(name, sqlite3.OPEN_READWRITE, function (error) { - if (error) { - throw error; + 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; } - pickSong(); + song = selectSong; + console.log(song); + pickArtist(); }); } -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); + var posted_artist = req.body.artist.toLowerCase(); + var posted_song = req.body.song.toLowerCase(); + + console.log('Artist: ' + posted_artist); + console.log('Song: ' + posted_song); + + var result = {status: '', message: ''} + + function insertSong(artist) { + var now = new Date(); + console.log('Adding song as played in ' + now.toJSON() + '...'); + + var stmt = db.run('INSERT INTO songs (id, title, artist, done_in) VALUES (NULL, ?, ?, ?)', [ + posted_song, + artist, + now.toJSON() + ], function (error) { + if (error) { + // expected: artist AND song already picked + console.log('Insert failed, possible duplicate'); + result.status = 'ERROR'; + result.message = 'Song already picked'; + } else { + console.log('Insert succeded'); + // things went alright + result.status = 'OK' + } + + console.log(JSON.stringify(result)); + res.writeHead(200, {'Content-Type': 'application/json'}); + res.write(JSON.stringify(result)); + res.end(); + } + ); + } + + var stmt = db.run('INSERT INTO artists (id, name) VALUES (NULL, ?)', [ + posted_artist + ], + function (error) { + if (error) { + // expected error: artist already exists + console.log('Artist exists, trying to find id...'); + db.get('SELECT id FROM artists WHERE name = ?', [posted_artist], function (err, artist) { + if (err) { + throw err; + } + + console.log('Artist id = ' + artist.id); + insertSong(artist.id); + }); + } else { + console.log('New artist id = ' + this.lastID); + insertSong(this.lastID); + } + } + ); }; diff --git a/lyricsnode/views/index.html b/lyricsnode/views/index.html index bb90e37..e3425f9 100644 --- a/lyricsnode/views/index.html +++ b/lyricsnode/views/index.html @@ -110,6 +110,7 @@ $('#others').on('submit', function() { url: '/round/others/', type: 'POST', data: $(this).serialize(), + dataType: 'json', success: function (response) { console.log(response); }