<commit_before>// Load tracks
var sqlite3 = require('sqlite3').verbose(),
    colors = require('colors'),
    fs = require('fs'),
    lwip = require('lwip'),
    path = require('path'),
    config = require('config'),
    LastfmAPI = require('lastfmapi'),
    http = require('http');

var lastFM = new LastfmAPI({
    'api_key' : config.get('LastFM.key'),
    'secret' : config.get('LastFM.secret')
});

var Library = function(callback) {
    var self = this;

    // Keep track of callbacks
    this.watchers = [];

    this.db = new sqlite3.Database('tracks.db');

    this.initiated = false;
    this.tracks = [];

    // Load in library
    this.db.run("CREATE TABLE IF NOT EXISTS tracks_ratings (\
                    uuid TEXT NOT NULL,\
                    track TEXT NOT NULL,\
                    rating INT,\
                    added DATETIME DEFAULT CURRENT_TIMESTAMP,\
                    PRIMARY KEY (uuid, track)\
                 )");

    this.db.run("CREATE TABLE IF NOT EXISTS tracks (\
                    id TEXT PRIMARY KEY NOT NULL,\
                    track TEXT NOT NULL,\
                    artist TEXT NOT NULL,\
                    image TEXT,\
                    user_id INT,\
                    added DATETIME DEFAULT CURRENT_TIMESTAMP,\
                    last DATETIME,\
                    plays INT,\
                    youtube TEXT NOT NULL\
                 );", function() {

        self.db.all("SELECT * FROM tracks ORDER BY artist, track", function(err, rows) {
            self.tracks = rows;
            self.initiated = true;

            if (callback && typeof(callback) === "function") {
                callback.call(self);
            }

            // global.ui.renderTracks(this.tracks);
        });
    });
}


/**
 * Count number of tracks in the library
 * @return {Number} track id
 */
Library.prototype.countTracks = function() {
    return this.tracks.length;
}

/**
 * Update DB
 * @param {Number} track id
 */
Library.prototype.playingTrack = function(id) {
    var stmt = this.db.prepare("UPDATE tracks SET `last` = datetime(), plays = IFNULL(plays, 0) + 1 WHERE id = ?");
    stmt.run(id);
}

/**
 * Rate track
 * @param {Number} track id
 * @param {Number} uuid
 */
Library.prototype.rateTrack = function(id, uuid, rating) {
    if (rating !== 1 && rating !== 0 && rating !== -1) return;

    var self = this;

    try {
        // Delete any previous ratings
        var stmt = this.db.prepare("DELETE FROM tracks_ratings WHERE `uuid` = ? AND `track` = ?");
        stmt.run(uuid, id, function() {
            // Add new rating
            var stmt = self.db.prepare("INSERT INTO tracks_ratings (`uuid`, `track`, `rating`) VALUES (?, ?, ?)");
            stmt.run(uuid, id, rating);
        });
    } catch(e) {
        console.log(e);
    }
}

/**
 * Get a random track from the library
 * @param {Function} callback [{Number} track id]
 */
Library.prototype.getRandomTrackID = function(callback) {
    // Build weighted tracklisting
    this.db.all("SELECT id, julianday('now') - julianday(IFNULL(last,added)) AS weight FROM tracks ORDER BY weight", function(err, tracks) {
        var trackCount = tracks.length,
            tracksWeighted = [];
<commit_msg>Affect random weight by thumbs up and down<commit_after>// Load tracks
var sqlite3 = require('sqlite3').verbose(),
    colors = require('colors'),
    fs = require('fs'),
    lwip = require('lwip'),
    path = require('path'),
    config = require('config'),
    LastfmAPI = require('lastfmapi'),
    http = require('http');

var lastFM = new LastfmAPI({
    'api_key' : config.get('LastFM.key'),
    'secret' : config.get('LastFM.secret')
});

var Library = function(callback) {
    var self = this;

    // Keep track of callbacks
    this.watchers = [];

    this.db = new sqlite3.Database('tracks.db');

    this.initiated = false;
    this.tracks = [];

    // Load in library
    this.db.run("CREATE TABLE IF NOT EXISTS tracks_ratings (\
                    uuid TEXT NOT NULL,\
                    track TEXT NOT NULL,\
                    rating INT,\
                    added DATETIME DEFAULT CURRENT_TIMESTAMP,\
                    PRIMARY KEY (uuid, track)\
                 )");

    this.db.run("CREATE TABLE IF NOT EXISTS tracks (\
                    id TEXT PRIMARY KEY NOT NULL,\
                    track TEXT NOT NULL,\
                    artist TEXT NOT NULL,\
                    image TEXT,\
                    user_id INT,\
                    added DATETIME DEFAULT CURRENT_TIMESTAMP,\
                    last DATETIME,\
                    plays INT,\
                    youtube TEXT NOT NULL\
                 );", function() {

        self.db.all("SELECT * FROM tracks ORDER BY artist, track", function(err, rows) {
            self.tracks = rows;
            self.initiated = true;

            if (callback && typeof(callback) === "function") {
                callback.call(self);
            }

            // global.ui.renderTracks(this.tracks);
        });
    });
}


/**
 * Count number of tracks in the library
 * @return {Number} track id
 */
Library.prototype.countTracks = function() {
    return this.tracks.length;
}

/**
 * Update DB
 * @param {Number} track id
 */
Library.prototype.playingTrack = function(id) {
    var stmt = this.db.prepare("UPDATE tracks SET `last` = datetime(), plays = IFNULL(plays, 0) + 1 WHERE id = ?");
    stmt.run(id);
}

/**
 * Rate track
 * @param {Number} track id
 * @param {Number} uuid
 */
Library.prototype.rateTrack = function(id, uuid, rating) {
    if (rating !== 1 && rating !== 0 && rating !== -1) return;

    var self = this;

    try {
        // Delete any previous ratings
        var stmt = this.db.prepare("DELETE FROM tracks_ratings WHERE `uuid` = ? AND `track` = ?");
        stmt.run(uuid, id, function() {
            // Add new rating
            var stmt = self.db.prepare("INSERT INTO tracks_ratings (`uuid`, `track`, `rating`) VALUES (?, ?, ?)");
            stmt.run(uuid, id, rating);
        });
    } catch(e) {
        console.log(e);
    }
}

/**
 * Get a random track from the library
 * @param {Function} callback [{Number} track id]
 */
Library.prototype.getRandomTrackID = function(callback) {
    // Build weighted tracklisting
    this.db.all("SELECT
                    id,
                    julianday('now') - julianday(IFNULL(last,added)) - (IFNULL(ratings_down.down, 0)*2) + IFNULL(ratings_down.down, 0) AS weight
                 FROM tracks
                 LEFT JOIN (SELECT track, COUNT(*) AS up, GROUP_CONCAT(uuid) AS up_uuid FROM tracks_ratings WHERE rating > 0 GROUP BY track) ratings_up\
                 ON ratings_up.track = tracks.id\
                 LEFT JOIN (SELECT track, COUNT(*) AS down, GROUP_CONCAT(uuid) AS down_uuid FROM tracks_ratings WHERE rating < 0 GROUP BY track) ratings_down\
                 ON ratings_down.track = tracks.id
                 ORDER BY weight", function(err, tracks) {
        var trackCount = tracks.length,
            tracksWeighted = [];

{'fraction_non_alphanumeric': 0.13100312588803636, 'fraction_numerical': 0.0029838022165387893, 'mean_word_length': 1.928036605657238, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '2703943', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1948, 'n_words': 571}