Created
December 13, 2012 15:05
-
-
Save saranrapjs/4276979 to your computer and use it in GitHub Desktop.
Used to add durations to Guthrie's JACK FM data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var echonest = require('echonest'), | |
myNest = new echonest.Echonest({ | |
api_key: 'XXX' | |
}), | |
sqlite3 = require('sqlite3').verbose(), | |
db = new sqlite3.Database('jack.sql.db'); | |
function getDuration(artist,title,callback) { | |
if (artist.indexOf(",") !== -1 && artist.split(",").length == 2) { // NORMALIZE LAST, FIRST ARTISTS | |
if (artist.indexOf("&") !== -1) { | |
artist = artist.split("&")[0].split(",").reverse().join(' ') + ' & ' + artist.split("&")[1] | |
} else { | |
artist = artist.split(",").reverse().join(" "); | |
} | |
} | |
myNest.song.search({ | |
title:title, | |
artist:artist, | |
song_type:'live:false', | |
bucket:['audio_summary','song_hotttnesss'], | |
sort:'song_hotttnesss-desc', | |
results:2 | |
},function(error,response) { | |
if (error) { | |
} else { | |
if (response && response.songs.length ) { | |
var minDuration; | |
response.songs.forEach(function(v) { // some echo nest results skew to the long duration, doing this out of safety? | |
if (!minDuration || v.audio_summary.duration < minDuration) minDuration = v.audio_summary.duration; | |
}) | |
if (callback) callback(minDuration); | |
} else { | |
console.log('uh oh? '+title,artist) | |
} | |
} | |
}); | |
} | |
//getDuration("The Cranberries","Dreams",function(d) { console.log(d); }) | |
//SELECT * FROM tracks ORDER BY RANDOM() LIMIT 5; | |
//SELECT COUNT(*) FROM tracks; | |
//CREATE TABLE tracks (artist TEXT, title TEXT, plays TEXT, duration TEXT); | |
//SELECT * FROM tracks WHERE duration NOT NULL; | |
//PRAGMA table_info(jackfm_2008); | |
//SELECT artist,title,plays*duration as total,(plays*duration)/(60*60) as hours,plays,duration FROM TRACKS ORDER BY total DESC LIMIT 10; | |
function getUniqueSongs() { | |
var tracks = {}; | |
db.run("DROP TABLE IF EXISTS tracks"); | |
db.run("CREATE TABLE IF NOT EXISTS tracks (artist TEXT, title TEXT, plays NUMERIC, duration NUMERIC, thumb TEXT)"); | |
db.each("SELECT * FROM jackfm_2008 WHERE timeplayed >= Datetime('2008-01-01 00:00:00') AND timeplayed <= Datetime('2008-12-31 11:59:00');", | |
function(err,row) { | |
if (err) return; | |
var key = row.title+row.thumb; | |
if (!tracks[key]) tracks[key] = { artist:row.artist, title:row.title, plays:0, duration:0, thumb:row.thumb }; | |
tracks[key].plays++; | |
}, | |
function(){ | |
var stmt = db.prepare("INSERT INTO tracks VALUES (?,?,?,?,?)"); | |
for (var i in tracks) { | |
var values = []; | |
for (var j in tracks[i]) { | |
values.push(tracks[i][j]); | |
} | |
stmt.run(values); | |
} | |
stmt.finalize(); | |
}); | |
} | |
//getUniqueSongs(); | |
function setOneDuration() { | |
db.serialize(function() { | |
db.get("SELECT * FROM tracks WHERE duration = 0 ORDER BY RANDOM() LIMIT 1;",function(err,row) { | |
if (!row || err || !row.artist || !row.title) { | |
console.log("finished?") | |
process.exit(); | |
} else { | |
getDuration(row.artist,row.title,function(duration) { | |
console.log(row.artist,row.title,duration); | |
var stmt = db.run("UPDATE tracks SET duration = ? WHERE thumb = ?", duration, row.thumb); | |
}); | |
} | |
}); | |
}); | |
} | |
//setOneDuration(); | |
setInterval(setOneDuration,1200); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment