<commit_before>/* global document */

var querystring = require('querystring');
var cheerio = require('cheerio');
var extend = require('util-extend');
var phantom = require('phantom');
var url = require('url');

var options = {
    url: 'http://guides.ucsf.edu/srch.php'
};

exports.setOptions = function (newOptions) {
    options = extend(options, newOptions);
};

exports.search = function (query, callback) {
    'use strict';

    if (! query || ! query.searchTerm) {
        callback(null, {data: []});
        return;
    }

    var myUrl = options.url + '?' + querystring.stringify({q: query.searchTerm});
    if (options.urlParameters) {
        myUrl += '&' + querystring.stringify(options.urlParameters);
    }

    var err, rv;

    // LibGuides API results do not match search page results.
    // Search page uses JavaScript to show results.
    // So, let's use a headless browser...

    phantom.create(function (ph) {
        ph.createPage(function (page) {
            page.open(myUrl, function (status) {
                if (status === 'success') {
                    page.evaluate(
                        function () {
                            // $lab:coverage:off$
                            var el = document.getElementById('s-lg-srch-list');
                            return el && el.innerHTML;
                            // $lab:coverage:on$
                        },
                        function (rawHtml) {
                            var result = [];

                            if (rawHtml) {
                                var $ = cheerio.load(rawHtml);

                                var rawResults = $('.s-lg-srch-listing-t a');
                    
                                rawResults.each(function () {
                                    result.push({
                                        name: $(this).text(),
                                        url: url.resolve(myUrl, $(this).attr('href'))
                                    });
                                });                        
                            }
                            
                            rv = {data: result, url: myUrl};
                            callback(null, rv);
                            ph.exit();
                        }
                    );
                } else {<commit_msg>exit phantom as quickly as possible<commit_after>/* global document */

var querystring = require('querystring');
var cheerio = require('cheerio');
var extend = require('util-extend');
var phantom = require('phantom');
var url = require('url');

var options = {
    url: 'http://guides.ucsf.edu/srch.php'
};

exports.setOptions = function (newOptions) {
    options = extend(options, newOptions);
};

exports.search = function (query, callback) {
    'use strict';

    if (! query || ! query.searchTerm) {
        callback(null, {data: []});
        return;
    }

    var myUrl = options.url + '?' + querystring.stringify({q: query.searchTerm});
    if (options.urlParameters) {
        myUrl += '&' + querystring.stringify(options.urlParameters);
    }

    var err, rv;

    // LibGuides API results do not match search page results.
    // Search page uses JavaScript to show results.
    // So, let's use a headless browser...

    phantom.create(function (ph) {
        ph.createPage(function (page) {
            page.open(myUrl, function (status) {
                if (status === 'success') {
                    page.evaluate(
                        function () {
                            // $lab:coverage:off$
                            var el = document.getElementById('s-lg-srch-list');
                            return el && el.innerHTML;
                            // $lab:coverage:on$
                        },
                        function (rawHtml) {
                            ph.exit();

                            var result = [];

                            if (rawHtml) {
                                var $ = cheerio.load(rawHtml);

                                var rawResults = $('.s-lg-srch-listing-t a');
                    
                                rawResults.each(function () {
                                    result.push({
                                        name: $(this).text(),
                                        url: url.resolve(myUrl, $(this).attr('href'))
                                    });
                                });                        
                            }
                            
                            rv = {data: result, url: myUrl};
                            callback(null, rv);
                        }
                    );
                } else {
{'fraction_non_alphanumeric': 0.1180952380952381, 'fraction_numerical': 0.0, 'mean_word_length': 1.0556763810352328, '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': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '840283', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1089, 'n_words': 249}