<commit_before>var sys = require('sys');
var nerve = require('./../deps/nerve/nerve');
var pls = require('./genpls');
var redis = require('./../deps/redis-node/redis');
var view = require('./view');
var utils = require('./../utils');
var _ = require('./../deps/underscore/underscore')._;

// a little different
process.mixin( require('./../utils') )

exports.listArtists = function(req, res, page) {
    page = page || 0;
    utils.newRedis(
        function() {
            this.redis.lrange($artistnames(), page*10 || 0, (page*10 || 0)+10).addCallback(
            function(list) {
                view.output( res, 'artistlist', {
                    'artists':_.map(list || [], function(m) {
                                return JSON.parse(m);
                              })
                    , 'title': 'Artists'
                });
            });
        },

        function() {
            view.output( res, 'error', { message : "Error connecting to database" } );
        }
    );
}

exports.artist = function(req, res, hash) {
    var output = {
        'songs':[]
    }
    utils.newRedis( function() {
        var r = this.redis;
        r.smembers( $artistsongs(hash) ).addCallback(
        function(songs) {
            _.each( songs, function(song, index) {
                r.get( $song(song) ).addCallback( function(s) {
                    output['songs'].push(JSON.parse(s));
                    if( index == songs.length - 1 ) {
                        // getting the artist from the song is cheating, but it works
                        // and is faster and cleaner than another redis query
                        output['title'] = "Artist - " + JSON.parse(s).artist;
                        view.output(res, 'artistsongs', output);
                    }
                });
            });
        });
    });
}

exports.album = function(req, res, hash) {
    var output = {
        'songs':[]
    }
    utils.newRedis( function() {
        var r = this.redis;
        r.smembers( $albumsongs(hash) ).addCallback(
        function(songs) {
            _.each( songs, function(song, index) {
                r.get( $song(song) ).addCallback( function(s) {
                    output['songs'].push(JSON.parse(s));
                    if( index == songs.length - 1 ) {
                        // getting the artist from the song is cheating, but it works
                        // and is faster and cleaner than another redis query
                        output['songs'] = _.sortBy( output['songs'], function( song ) {
                            return song.track;
                        });
                        output['title'] = "Album - " + JSON.parse(s).album;
                        view.output(res, 'albumsongs', output);
                    }
                });
            });
        });
    });
}


exports.listSongs = function(req, res) {
    var resp = "";
    utils.newRedis(function() {
        var r = this.redis;
        r.keys(songk('*')).addCallback( function(songlist) {
            _.each(songlist, function( songkey, i ) {
                r.get(songkey).addCallback( function( songdata ) {
                    view.render('info', JSON.parse(songdata), function(out) {
                        resp += out;

                        if( i == songlist.length-1 ) {
                            res.respond( resp );
                            r.close();
                        }
                    });
                })
            });
            r.close();
        });
    });
}
<commit_msg>Added the handler for /song/hash URLs<commit_after>var sys = require('sys');
var nerve = require('./../deps/nerve/nerve');
var pls = require('./genpls');
var redis = require('./../deps/redis-node/redis');
var view = require('./view');
var utils = require('./../utils');
var _ = require('./../deps/underscore/underscore')._;

// a little different
process.mixin( require('./../utils') )

exports.listArtists = function(req, res, page) {
    page = page || 0;
    utils.newRedis(
        function() {
            this.redis.lrange($artistnames(), page*10 || 0, (page*10 || 0)+10).addCallback(
            function(list) {
                view.output( res, 'artistlist', {
                    'artists':_.map(list || [], function(m) {
                                return JSON.parse(m);
                              })
                    , 'title': 'Artists'
                });
            });
        },

        function() {
            view.output( res, 'error', { message : "Error connecting to database" } );
        }
    );
}

exports.artist = function(req, res, hash) {
    var output = {
        'songs':[]
    }
    utils.newRedis( function() {
        var r = this.redis;
        r.smembers( $artistsongs(hash) ).addCallback(
        function(songs) {
            _.each( songs, function(song, index) {
                r.get( $song(song) ).addCallback( function(s) {
                    output['songs'].push(JSON.parse(s));
                    if( index == songs.length - 1 ) {
                        // getting the artist from the song is cheating, but it works
                        // and is faster and cleaner than another redis query
                        output['title'] = "Artist - " + JSON.parse(s).artist;
                        view.output(res, 'artistsongs', output);
                    }
                });
            });
        });
    });
}

exports.album = function(req, res, hash) {
    var output = {
        'songs':[]
    }
    utils.newRedis( function() {
        var r = this.redis;
        r.smembers( $albumsongs(hash) ).addCallback(
        function(songs) {
            _.each( songs, function(song, index) {
                r.get( $song(song) ).addCallback( function(s) {
                    output['songs'].push(JSON.parse(s));
                    if( index == songs.length - 1 ) {
                        // getting the artist from the song is cheating, but it works
                        // and is faster and cleaner than another redis query
                        output['songs'] = _.sortBy( output['songs'], function( song ) {
                            return song.track;
                        });
                        output['title'] = "Album - " + JSON.parse(s).album;
                        view.output(res, 'albumsongs', output);
                    }
                });
            });
        });
    });
}


exports.listSongs = function(req, res) {
    var resp = "";
    utils.newRedis(function() {
        var r = this.redis;
        r.keys(songk('*')).addCallback( function(songlist) {
            _.each(songlist, function( songkey, i ) {
                r.get(songkey).addCallback( function( songdata ) {
                    view.render('info', JSON.parse(songdata), function(out) {
                        resp += out;

                        if( i == songlist.length-1 ) {
                            res.respond( resp );
                            r.close();
                        }
                    });
                })
            });
            r.close();
        });
    });
}

exports.songInfo = function( req, res, songhash ) {
    utils.newRedis( function() {
        this.redis.get($song(songhash)).addCallback( function(meta) {
                view.render('info', JSON.parse(meta), _(res.respond).bind(res));
        });
    });
};
{'fraction_non_alphanumeric': 0.15002032795771786, 'fraction_numerical': 0.0032524732348556715, 'mean_word_length': 1.3317535545023698, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '307429', 'n_tokens_mistral': 2109, 'n_tokens_neox': 1978, 'n_words': 427}