Last active
August 29, 2015 14:17
-
-
Save fmarcia/31ef2f50180891e07beb to your computer and use it in GitHub Desktop.
Search and download AUR packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/node | |
/* ----------------------------------------------------------------------------- | |
* aur.js | |
* Search and download AUR packages | |
* Author: Franck Marcia - https://github.com/fmarcia | |
*/ | |
"use strict"; | |
var https = require("https"); | |
/* ----------------------------------------------------------------------------- | |
* check <query> as 1st argument | |
*/ | |
(function main() { | |
var query = process.argv[2]; | |
if (!query) { | |
return exit(-1, "syntax: aur <package>"); | |
} | |
// next step | |
search(query); | |
}()); | |
/* ----------------------------------------------------------------------------- | |
* search for <query> in aur packages | |
*/ | |
function search(query) { | |
var data = ""; | |
https.get( | |
"https://aur.archlinux.org/rpc.php?type=search&arg=" + query, | |
function (response) { | |
response.setEncoding("utf8"); | |
// accumulate data | |
response.on("data", function (chunk) { data += chunk; }); | |
// next step | |
response.on("end", function () { check(query, data); }); | |
} | |
).on( | |
"error", | |
function (error) { | |
exit(-2, "list error: " + error.message); | |
} | |
); | |
} | |
/* ----------------------------------------------------------------------------- | |
* check response | |
*/ | |
function check(query, data) { | |
var pkgs; | |
try { | |
pkgs = JSON.parse(data); | |
} catch (e) { | |
pkgs = {}; | |
} | |
if (!pkgs.type || !pkgs.results) { | |
return exit(-3, "invalid response"); | |
} | |
if (pkgs.type === "error") { | |
return exit(-4, pkgs.results); | |
} | |
if (!pkgs.results.length) { | |
console.log("no package for " + query); | |
return; | |
} | |
// next | |
sort(pkgs); | |
} | |
/* ----------------------------------------------------------------------------- | |
* sort packages list by NumVotes descending then PackageBase ascending | |
*/ | |
function sort(pkgs) { | |
pkgs.results.sort(function (a, b) { | |
if (a.NumVotes > b.NumVotes) return -1; | |
if (a.NumVotes < b.NumVotes) return 1; | |
if (a.NumVotes === b.NumVotes) { | |
if (a.PackageBase < b.PackageBase) return -1; | |
if (a.PackageBase > b.PackageBase) return 1; | |
return 0; | |
} | |
}); | |
// next step | |
list(pkgs); | |
} | |
/* ----------------------------------------------------------------------------- | |
* print out packages list | |
*/ | |
function list(pkgs) { | |
var length = pkgs.results.length; | |
console.log(length + " package" + (length > 1 ? "s" : "") + " found"); | |
pkgs.results.forEach(function (pkg, index) { | |
console.log( | |
(index + 1) + ". " + | |
pkg.PackageBase + " " + pkg.Version + | |
" (" + pkg.NumVotes + ")" | |
); | |
}); | |
// next step | |
choose(pkgs); | |
} | |
/* ----------------------------------------------------------------------------- | |
* choose a package (or quit) | |
*/ | |
function choose(pkgs) { | |
var inter = require("readline").createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
inter.setPrompt(" > "); | |
inter.prompt(); | |
inter.on("line", function (answer) { | |
answer = parseInt(answer, 10) - 1; | |
if (pkgs.results[answer]) { | |
// next step | |
download(pkgs.results[answer]); | |
} | |
inter.close(); | |
}); | |
} | |
/* ----------------------------------------------------------------------------- | |
* download a package | |
*/ | |
function download(pkg) { | |
var url = "https://aur.archlinux.org" + pkg.URLPath; | |
var file = url.split("/").pop(); | |
var stream = require("fs").createWriteStream(file); | |
https.get( | |
url, | |
function (response) { | |
response.on("data", function (chunk) { stream.write(chunk); }); | |
response.on("end", function () { stream.end(); }); | |
} | |
).on( | |
"error", | |
function (error) { | |
exit(-5, "download error: " + error.message); | |
} | |
); | |
} | |
/* ----------------------------------------------------------------------------- | |
* exit on error | |
*/ | |
function exit(exitCode, message) { | |
console.error(message); | |
process.exit(exitCode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment