<commit_before>/**
 * Get members of Parliament
 */
var request = require('request'); // For HTTP requests
var Q = require('q');             // For promises
var xml2js = require('xml2js');
var config = require(__dirname + '/config.json');
var phpjs = require('phpjs');

function members() {

    /**
     * Get a Member of Parliament by name.
     *
     * If there is no info, attempts to look them up on
     * data.parliament.uk and cache the response before returning.
     */
    this.getMemberByName = function(memberName) {
        
        // Normalize member names (for consistency)
        memberName = phpjs.trim(memberName);
        memberName = memberName.replace(/^Mr /, '');
        memberName = memberName.replace(/^Mrs /, '');

        // Used to normalize names like "<NAME>" to "<NAME>"
        // (which he actually goes by).
        // This shouldn't really be different when on Bills but sometimes it is.
        var aliases = require(__dirname + '/aliases.json');
        if (aliases[memberName])
            memberName = aliases[memberName];
        
        var thisFunc = this;
        var deferred = Q.defer();
        db.members.find({ name: memberName }, function(err, members) {
            if (err) {
                deferred.resolve(null);
            } else if (!members.length || members.length < 1) {
                // If not found then try to look up the member via API
                thisFunc.getMemberDetailsFromParliament(memberName)
                .then(function(member) {
                    if (member.party) {
                        var deferred2 = Q.defer();
                        member.lastUpdated = phpjs.date('Y-m-d');
                        db.members.save(member, function(err, saved) {
                            deferred2.resolve(member);
                        });
                        return deferred2.promise;
                    } else {
                        return member;
                    }
                })  
                .then(function(member) {
                    // If we don't have party details for the member after 
                    // calling getMemberDetailsFromParliament() then try
                    // looking up TheyWorkForYou to see if the member is
                    // from the House of Lords.
                    if (!member.party) {
                        var deferred2 = Q.defer();
                        member = thisFunc.getLordDetailsFromTheyWorkForYou(memberName)
                        .then(function(member) {
                            member.lastUpdated = phpjs.date('Y-m-d');
                            db.members.save(member, function(err, saved) {
                                deferred2.resolve(member);
                            });
                        });
                        return deferred2.promise;
                    } else {
                        return member;
                    }
                })
                .then(function(member) {
                    // Return member object
                    deferred.resolve(member);
                });
            } else {
                // If found in DB, return cached version of member object
                deferred.resolve(members[0]);
            }
        });
        return deferred.promise;
    };
    
    /**
     * Fetch multiple members at once (as an array)<commit_msg>Now uses Parliamentary member ID for member ID objects to prevent duplicates.<commit_after>/**
 * Get members of Parliament
 */
var request = require('request'); // For HTTP requests
var Q = require('q');             // For promises
var crypto = require('crypto');
var xml2js = require('xml2js');
var config = require(__dirname + '/config.json');
var phpjs = require('phpjs');

function members() {

    /**
     * Get a Member of Parliament by name.
     *
     * If there is no info, attempts to look them up on
     * data.parliament.uk and cache the response before returning.
     */
    this.getMemberByName = function(memberName) {
        
        // Normalize member names (for consistency)
        memberName = phpjs.trim(memberName);
        memberName = memberName.replace(/^Mr /, '');
        memberName = memberName.replace(/^Mrs /, '');

        // Used to normalize names like "<NAME>" to "<NAME>"
        // (which he actually goes by).
        // This shouldn't really be different when on Bills but sometimes it is.
        var aliases = require(__dirname + '/aliases.json');
        if (aliases[memberName])
            memberName = aliases[memberName];
        
        var thisFunc = this;
        var deferred = Q.defer();
        db.members.find({ name: memberName }, function(err, members) {
            if (err) {
                deferred.resolve(null);
            } else if (!members.length || members.length < 1) {
                // If not found then try to look up the member via API
                thisFunc.getMemberDetailsFromParliament(memberName)
                .then(function(member) {
                    if (member.party) {
                        var deferred2 = Q.defer();
                        member._id = member.memberId;
                        member.lastUpdated = phpjs.date('Y-m-d');
                        db.members.save(member, function(err, saved) {
                            deferred2.resolve(member);
                        });
                        return deferred2.promise;
                    } else {
                        return member;s
                    }
                })  
                .then(function(member) {
                    // If we don't have party details for the member after 
                    // calling getMemberDetailsFromParliament() then try
                    // looking up TheyWorkForYou to see if the member is
                    // from the House of Lords.
                    if (!member.party) {
                        var deferred2 = Q.defer();
                        member = thisFunc.getLordDetailsFromTheyWorkForYou(memberName)
                        .then(function(member) {
                            member.lastUpdated = phpjs.date('Y-m-d');
                            if (member.memberId) {
                                member._id = member.memberId;
                            } else {
                                member._id = crypto.createHash('sha1').update( memberName ).digest("hex");
                            }
                            db.members.save(member, function(err, saved) {
                                deferred2.resolve(member);
                            });
                        });
                        return deferred2.promise;
                    } else {
                        return member;
                    }
                })
                .then(function(member) {
                    // Return member object
                    deferred.resolve(member);
                });
            } else {
                // If found in DB, return cached version of member object
                deferred.resolve(members[0]);
            }
        });
        return deferred.promise;
    };
    
    /**
     * Fetch multiple members at once (as an array)
{'fraction_non_alphanumeric': 0.09769636414099361, 'fraction_numerical': 0.0029142381348875937, 'mean_word_length': 1.2451713395638628, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '5278769', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1636, 'n_words': 470}