<commit_before>var request = require('request');
var safeParse = require("safe-json-parse/callback")
var parseXMLString = require('xml2js').parseString;
var PlexTv = require('./PlexTv.js')
var PlexClient = require('./PlexClient.js')
var PlexConnection = require('./PlexConnection.js')
module.exports = function PlexServer(){
    this.name;
    this.product;
    this.productVersion;
    this.platform;
    this.platformVersion;
    this.device;
    this.clientIdentifier;
    this.createdAt;
    this.lastSeenAt;
    this.provides;
    this.owned;
    this.httpsRequired;
    this.ownerId;
    this.home;
    this.accessToken
    this.sourceTitle;
    this.synced;
    this.relay;
    this.publicAddressMatches;
    this.presence;
    this.plexConnections;
    this.chosenConnection;

    //Functions
    this.hitApi = function(command,params,connection,callback){
        console.log('Hitting server ' + this.name + ' via ' + connection.uri)
            if (connection == null){
                if (this.chosenConnection == null){
                    console.log('SERVER: You should find a working connection via #findConnection first!')
                }
                connection = this.chosenConnection
            }
            var query = "";
            for (key in params) {
                query += encodeURIComponent(key)+'='+encodeURIComponent(params[key])+'&';
            }
            var _url = connection.uri + command + '?' + query
            var options = {
                url: _url,
                time: true,
                headers: {
                    'X-Plex-Client-Identifier': 'PlexTogether',
                    'Accept':'application/json',
                    'X-Plex-Token':this.accessToken
                },
                timeout: 8000
            }
            var that = this;
            //console.log('Hitting server ' + this.name + ' with command ' + command)
            //console.log(options)
            request(options, function (error, response, body) {
                if (!error) {
                    safeParse(body, function (err, json){
                        if (err){
                            return callback(null,that)
                        }
                        return callback(json,that)                        
                    })
                } else {
                    return callback(null,that)
                }
            }) 
    }
    this.hitApiTestConnections = function(command,connection,callback){
        //For use with #findConnection
        if (connection == null){
            if (this.chosenConnection == null){
                console.log('You need to specify a connection!')
            }
        }
        var _url = connection.uri + command
        var options = {
            url: _url,
            time: true,
            headers: {
                'X-Plex-Client-Identifier': 'PlexTogether',
                'Accept':'application/json',
                'X-Plex-Token':this.accessToken
            },
            timeout: 5000
        }
        var that = this;
        request(options, function (error, response, body) {
            if (!error) {
                safeParse(body, function (err, json){
                    if (err){
                        return callback(null,connection)
                    }
                    return callback(json,connection)                        
                })
            } else {
                return callback(null,connection)
            }
        }) 
    }
    this.findConnection = function(callback){
        //This function iterates through all available connections and 
        // if any of them return a valid response we'll set that connection
        // as the chosen connection for future use.
        var that = this;
        that.chosenConnection = null;
        for (var i in this.plexConnections){
            var connection = this.plexConnections[i]
            this.hitApi('',{},connection,function(result){
                    if (that.chosenConnection != null){
                        //Looks like we've already found a good connection
                        // lets disregard this connection 
                        return(callback(true))
                    }
                    if (result != null){
                        that.chosenConnection = connection 
                        return(callback(true))                    
                    } 
                    return(callback(false))                
                })  
            }                      
    
    }

    //Functions for dealing with media 
    this.search = function(searchTerm,callback){
        //This function hits the PMS using the /search endpoint and returns what the server returns if valid
        var that = this
        this.hitApi('/search',{'query':searchTerm},this.chosenConnection,function(result){
            validResults = []
            if (result){             
                for (var i in result._children){
                    var res = result._children[i]
                    if (res._elementType == 'Directory' || res._elementType == 'Media' || res._elementType == 'Video'){
                        validResults.push(res)
                    }
                }
                return callback(validResults,that)
            }
            return callback(null,that)
        })
    }

    this.getMediaByRatingKey = function(ratingKey,callback){
        //This function hits the PMS and returns the item at the ratingKey
        this.hitApi('/library/metadata/'+ratingKey,{},this.chosenConnection,function(result,that){
            validResults = []
            console.log('Response back from metadata request')
            if (result != null){
                // Old Server version compatibility
                if (result._children) {
                    for (var i in result._children){
                        var res = result._children[i]
                        if (res._elementType == 'Directory' || res._elementType == 'Media' || res._elementType == 'Video'){
                            return callback(res,that)
                        }
                    }
                } else {
                    // New Server compatibility
                    return callback(result.MediaContainer.Metadata[0],that)
                }
                return callback(null,that)
            } 
            return callback(null,that)
        })
    }
};<commit_msg>Debugging console writes for issue #2<commit_after>var request = require('request');
var safeParse = require("safe-json-parse/callback")
var parseXMLString = require('xml2js').parseString;
var PlexTv = require('./PlexTv.js')
var PlexClient = require('./PlexClient.js')
var PlexConnection = require('./PlexConnection.js')
module.exports = function PlexServer(){
    this.name;
    this.product;
    this.productVersion;
    this.platform;
    this.platformVersion;
    this.device;
    this.clientIdentifier;
    this.createdAt;
    this.lastSeenAt;
    this.provides;
    this.owned;
    this.httpsRequired;
    this.ownerId;
    this.home;
    this.accessToken
    this.sourceTitle;
    this.synced;
    this.relay;
    this.publicAddressMatches;
    this.presence;
    this.plexConnections;
    this.chosenConnection;

    //Functions
    this.hitApi = function(command,params,connection,callback){
        console.log('Hitting server ' + this.name + ' via ' + connection.uri)
            if (connection == null){
                if (this.chosenConnection == null){
                    console.log('SERVER: You should find a working connection via #findConnection first!')
                }
                connection = this.chosenConnection
            }
            var query = "";
            for (key in params) {
                query += encodeURIComponent(key)+'='+encodeURIComponent(params[key])+'&';
            }
            var _url = connection.uri + command + '?' + query
            var options = {
                url: _url,
                time: true,
                headers: {
                    'X-Plex-Client-Identifier': 'PlexTogether',
                    'Accept':'application/json',
                    'X-Plex-Token':this.accessToken
                },
                timeout: 8000
            }
            var that = this;
            //console.log('Hitting server ' + this.name + ' with command ' + command)
            //console.log(options)
            request(options, function (error, response, body) {
                if (!error) {
                    safeParse(body, function (err, json){
                        if (err){
                            return callback(null,that)
                        }
                        return callback(json,that)                        
                    })
                } else {
                    return callback(null,that)
                }
            }) 
    }
    this.hitApiTestConnections = function(command,connection,callback){
        //For use with #findConnection
        if (connection == null){
            if (this.chosenConnection == null){
                console.log('You need to specify a connection!')
            }
        }
        var _url = connection.uri + command
        var options = {
            url: _url,
            time: true,
            headers: {
                'X-Plex-Client-Identifier': 'PlexTogether',
                'Accept':'application/json',
                'X-Plex-Token':this.accessToken
            },
            timeout: 5000
        }
        var that = this;
        request(options, function (error, response, body) {
            if (!error) {
                safeParse(body, function (err, json){
                    if (err){
                        return callback(null,connection)
                    }
                    return callback(json,connection)                        
                })
            } else {
                return callback(null,connection)
            }
        }) 
    }
    this.findConnection = function(callback){
        //This function iterates through all available connections and 
        // if any of them return a valid response we'll set that connection
        // as the chosen connection for future use.
        var that = this;
        that.chosenConnection = null;
        for (var i in this.plexConnections){
            var connection = this.plexConnections[i]
            this.hitApi('',{},connection,function(result){
                    if (that.chosenConnection != null){
                        //Looks like we've already found a good connection
                        // lets disregard this connection 
                        return(callback(true))
                    }
                    if (result != null){
                        that.chosenConnection = connection 
                        return(callback(true))                    
                    } 
                    return(callback(false))                
                })  
            }                      
    
    }

    //Functions for dealing with media 
    this.search = function(searchTerm,callback){
        //This function hits the PMS using the /search endpoint and returns what the server returns if valid
        var that = this
        this.hitApi('/search',{'query':searchTerm},this.chosenConnection,function(result){
            validResults = []
            if (result){             
                for (var i in result._children){
                    var res = result._children[i]
                    if (res._elementType == 'Directory' || res._elementType == 'Media' || res._elementType == 'Video'){
                        validResults.push(res)
                    }
                }
                return callback(validResults,that)
            }
            return callback(null,that)
        })
    }

    this.getMediaByRatingKey = function(ratingKey,callback){
        //This function hits the PMS and returns the item at the ratingKey
        this.hitApi('/library/metadata/'+ratingKey,{},this.chosenConnection,function(result,that){
            validResults = []
            console.log('Response back from metadata request')
            console.log(result)
            if (result != null){                
                if (result._children) {
                    // Old Server version compatibility
                    for (var i in result._children){
                        var res = result._children[i]
                        if (res._elementType == 'Directory' || res._elementType == 'Media' || res._elementType == 'Video'){
                            return callback(res,that)
                        }
                    }
                } else {
                    // New Server compatibility
                    return callback(result.MediaContainer.Metadata[0],that)
                }
                console.log('Didnt find a compatible PMS Metadata object. Result from the server is below')
                console.log(result)
                return callback(null,that)
            }                 
            console.log('Didnt find a compatible PMS Metadata object because result == null. Result from the server is below')
            console.log(result)
            return callback(null,that)
        })
    }
};
{'fraction_non_alphanumeric': 0.09680090327436959, 'fraction_numerical': 0.001580730146782085, 'mean_word_length': 1.3598579040852576, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1041816', 'n_tokens_mistral': 3238, 'n_tokens_neox': 3055, 'n_words': 751}