Skip to content

Commit

Permalink
Added option to limit number of hits
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrow committed Feb 24, 2016
1 parent 2fa053e commit 34641bd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions bin/getpapers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ program
'download anything)')
.option('-f, --logfile <filename>',
'save log to specified file in output directory as well as printing to terminal')
.option('-k, --limit <int>',
'limit the number of hits and downloads')
.parse(process.argv);

if (!process.argv.slice(2).length) {
Expand Down Expand Up @@ -96,6 +98,7 @@ options.xml = program.xml;
options.pdf = program.pdf;
options.supp = program.supp;
options.all = program.all;
options.hitlimit = parseInt(program.limit);
options.noexecute = program.noexecute;
if (options.noexecute) {
log.info('Running in no-execute mode, so nothing will be downloaded');
Expand Down
27 changes: 20 additions & 7 deletions lib/eupmc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ EuPmc.prototype.search = function(query) {
if (!eupmc.opts.all) {
query += " OPEN_ACCESS:y";
}
var options = { resulttype: 'core' };
eupmc.pagesize = '100'
var options = { resulttype: 'core', pageSize: eupmc.pagesize };
eupmc.queryurl = eupmc.buildQuery(query, options);
eupmc.first = true;
eupmc.hitcount = 0;
eupmc.residualhits = 0;
eupmc.allresults = [];
eupmc.iter = 0;
eupmc.iter = 1; //we always get back the first page

eupmc.pageQuery();

Expand Down Expand Up @@ -68,23 +70,34 @@ EuPmc.prototype.completeCallback = function(data) {
process.exit(0);
}

// set hitlimit
if (eupmc.opts.hitlimit) {
log.info('Limiting to ' + eupmc.opts.hitlimit + ' hits');
}
else { eupmc.opts.hitlimit = eupmc.hitcount; }

// create progress bar
var progmsg = 'Retrieving results [:bar] :percent' +
' (eta :etas)';
var progopts = {
total: eupmc.hitcount,
total: eupmc.opts.hitlimit,
width: 30,
complete: chalk.green('=')
};
eupmc.pageprogress = new ProgressBar(progmsg, progopts);

}

var result = resp.resultList[0].result;
if (eupmc.residualhits) {
var result = resp.resultList[0].result.slice(0,eupmc.residualhits);
}
else { var result = resp.resultList[0].result; }
eupmc.allresults = eupmc.allresults.concat(result);
eupmc.pageprogress.tick(result.length);

if (eupmc.allresults.length < eupmc.hitcount) {

if (eupmc.allresults.length < eupmc.opts.hitlimit) { //we still have more results to get
if (eupmc.opts.hitlimit - eupmc.allresults.length < eupmc.pagesize) {
eupmc.residualhits = eupmc.opts.hitlimit - eupmc.allresults.length;
}
eupmc.iter += 1;
eupmc.pageQuery();
} else {
Expand Down

0 comments on commit 34641bd

Please sign in to comment.