<commit_before>var cip = require('cip-js');
var cache = require('memory-cache');
var Q = require('q');

if(process.env.CIP_USERNAME === undefined || process.env.CIP_PASSWORD === undefined) {
    console.log("Please set CIP_USERNAME and CIP_PASSWORD, exiting!");
    process.exit(1);
}

var CIP_USERNAME = process.env.CIP_USERNAME;
var CIP_PASSWORD = <PASSWORD>.CIP_PASSWORD;

// Cache in 30 s.
var CACHE_TIME = 1000 * 30;

var NatMusConfig = {
    endpoint: "http://cumulus.natmus.dk/CIP/",
    constants: {
        catch_all_alias: "any",
        layout_alias: "web"
    },
    catalog_aliases: {
        "Alle": "ALL",
        "Antiksamlingen": "AS",
        "Bevaringsafdelingen": "BA",
        "<NAME>delalder og Renæssance": "DMR",
        "<NAME>": "DNT",
        "Etnografisk samling": "ES",
        "Frihedsmuseet": "FHM",
        "Den Kgl. Mønt- og Medaljesamling": "KMM",
        "Musikmuseet": "MUM",
        "Etnografisk Samling": "ES",
        "<NAME>": "DO",
        "Frilandsmuseet": "FLM",
        "Cropper": "Cropper"
    }
};

exports.init_session = function init_session() {
    var nm = cache.get('nm');

    var deferred = Q.defer();

    if(nm) {
        deferred.resolve(nm);
    }
    else {
        nm = new cip.CIPClient(NatMusConfig);
	    nm.session_open(
            CIP_USERNAME, CIP_PASSWORD,
            function( ) {
                cache.put('nm', nm, CACHE_TIME);
                deferred.resolve(nm);
            },
            function( ) {
                deferred.reject( new Error("Couldn't open session, check connection, username and password.") );
            }
        );
    }

    return deferred.promise;
};

exports.get_catalogs = function get_catalogs(nm) {
    var catalogs = cache.get('catalogs');

    var deferred = Q.defer();

    if(catalogs) {
        deferred.resolve(catalogs);
    }
    else {
	    nm.get_catalogs(function(catalogs) {
            if(catalogs === null) {
                deferred.reject();
            } else {
                cache.put('catalogs', catalogs, CACHE_TIME);
                deferred.resolve(catalogs);
            }
        });
    }

    return deferred.promise;
};

exports.get_recent_assets = function get_recent_assets(nm, catalog, expr, callback) {
    var search_string = '"Record Modification Date" >= ' + expr;
    nm.criteriasearch({catalog: catalog}, search_string, null, callback);
};

exports.get_asset = function get_asset(nm, catalog, id) {
    var deferred = Q.defer();

    var id_string = 'ID is "' + id + '"';
    var cache_key = 'asset_' + catalog + '_' + id;
    var asset = cache.get(cache_key);

    if(!asset) {
        nm.criteriasearch({catalog: {alias: catalog}}, id_string, null, function(result) {
            if(result === null) {
                deferred.reject( new Error("The CIP returned no result.") );
            } else {
                result.get(1, 0, function(returnvalue) {
                    if(returnvalue !== null) {
                        cache.put(cache_key, returnvalue, CACHE_TIME);
                        deferred.resolve(returnvalue);
                    } else {<commit_msg>Making the get_recent_assets method return a promise instead of using a callback.<commit_after>var cip = require('cip-js');
var cache = require('memory-cache');
var Q = require('q');

if(process.env.CIP_USERNAME === undefined || process.env.CIP_PASSWORD === undefined) {
    console.log("Please set CIP_USERNAME and CIP_PASSWORD, exiting!");
    process.exit(1);
}

var CIP_USERNAME = process.env.CIP_USERNAME;
var CIP_PASSWORD = process.env.CIP_PASSWORD;

// Cache in 30 s.
var CACHE_TIME = 1000 * 30;

var NatMusConfig = {
    endpoint: "http://cumulus.natmus.dk/CIP/",
    constants: {
        catch_all_alias: "any",
        layout_alias: "web"
    },
    catalog_aliases: {
        "Alle": "ALL",
        "Antiksamlingen": "AS",
        "Bevaringsafdelingen": "BA",
        "<NAME> og Renæssance": "DMR",
        "<NAME>": "DNT",
        "Etnografisk samling": "ES",
        "Frihedsmuseet": "FHM",
        "Den Kgl. Mønt- og Medaljesamling": "KMM",
        "Musikmuseet": "MUM",
        "Etnografisk Samling": "ES",
        "<NAME>": "DO",
        "Frilandsmuseet": "FLM",
        "Cropper": "Cropper"
    }
};

exports.init_session = function init_session() {
    var nm = cache.get('nm');

    var deferred = Q.defer();

    if(nm) {
        deferred.resolve(nm);
    }
    else {
        nm = new cip.CIPClient(NatMusConfig);
	    nm.session_open(
            CIP_USERNAME, CIP_PASSWORD,
            function( ) {
                cache.put('nm', nm, CACHE_TIME);
                deferred.resolve(nm);
            },
            function( ) {
                deferred.reject( new Error("Couldn't open session, check connection, username and password.") );
            }
        );
    }

    return deferred.promise;
};

exports.get_catalogs = function get_catalogs(nm) {
    var catalogs = cache.get('catalogs');

    var deferred = Q.defer();

    if(catalogs) {
        deferred.resolve(catalogs);
    }
    else {
	    nm.get_catalogs(function(catalogs) {
            if(catalogs === null) {
                deferred.reject();
            } else {
                cache.put('catalogs', catalogs, CACHE_TIME);
                deferred.resolve(catalogs);
            }
        });
    }

    return deferred.promise;
};

exports.get_recent_assets = function get_recent_assets(nm, catalog, expr) {
    var deferred = Q.defer();

    var search_string = '"Record Modification Date" >= ' + expr;
    nm.criteriasearch({catalog: catalog}, search_string, null, deferred.resolve);
    
    return deferred.promise;
};

exports.get_asset = function get_asset(nm, catalog, id) {
    var deferred = Q.defer();

    var id_string = 'ID is "' + id + '"';
    var cache_key = 'asset_' + catalog + '_' + id;
    var asset = cache.get(cache_key);

    if(!asset) {
        nm.criteriasearch({catalog: {alias: catalog}}, id_string, null, function(result) {
            if(result === null) {
                deferred.reject( new Error("The CIP returned no result.") );
            } else {
                result.get(1, 0, function(returnvalue) {
                    if(returnvalue !== null) {
                        cache.put(cache_key, returnvalue, CACHE_TIME);
                        deferred.resolve(returnvalue);
                    } else {
{'fraction_non_alphanumeric': 0.14072327044025157, 'fraction_numerical': 0.003459119496855346, 'mean_word_length': 2.1773226773226773, 'pattern_counts': {'":': 26, '<': 10, '<?xml version=': 0, '>': 12, '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': '1412000', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2011, 'n_words': 419}