Skip to content

Instantly share code, notes, and snippets.

@miau
Created March 26, 2011 18:41
Show Gist options
  • Select an option

  • Save miau/888525 to your computer and use it in GitHub Desktop.

Select an option

Save miau/888525 to your computer and use it in GitHub Desktop.
@if(0)==(0) ECHO OFF
CScript.exe //NoLogo //E:JScript "%~f0" %*
pause
GOTO :EOF
@end
var fso = new ActiveXObject('Scripting.FileSystemObject');
if (!fso.FolderExists('dist')) {
fso.CreateFolder('dist');
}
var folder = fso.GetFolder('.');
for (var files = new Enumerator(folder.Files); !files.atEnd(); files.moveNext()) {
var file = files.item().Name;
if (!file.match(/^(?:[0-9a-f]+|oreilly)-([-0-9X]+)e?.pdf$/)) {
continue;
}
var isbn = RegExp.$1.replace(/-/g, '');
var title = get_title(isbn);
var dist_file = 'dist\\' + title.replace(/[\\/:*?"<>|]/g, '') + '.pdf';
if (fso.FileExists(dist_file)) {
WScript.Echo("skipped: " + dist_file + ' (already exists)');
continue;
}
fso.CopyFile(file, dist_file, false);
WScript.Echo("copied: " + file + ' -> ' + dist_file);
}
function get_title(isbn) {
var xhr = new ActiveXObject('Msxml2.XMLHTTP');
xhr.open('GET', 'http://www.oreilly.co.jp/books/' + isbn + '/biblio.json', false);
xhr.send(null);
var json = xhr.responseText;
var data;
try {
data = eval('(' + json + ')');
} catch(e) {
WScript.Echo('Invalid ISBN: ' + isbn);
WScript.Quit();
}
return data.title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment