#!/usr/bin/env node const { program } = require('commander'); const request = require("sync-request"); const cheerio = require('cheerio'); const cliSelect = require('cli-select'); const chalk = require('chalk'); const open = require('open'); program .name('qrzlookup') .description('CLI to lookup someone on QRZ.com (a site for ham radio operators)') .version(require("./package.json").version) .option('-u, --username ', "Your QRZ Username") .option('-p, --password ', "Your QRZ Password") .option('-t, --text', "Disables the action selector (best used in scripts)") .parse(); const opts = program.opts(process.argv); var use_env = false if (process.env.QRZ_USERNAME && process.env.QRZ_PASSWORD) use_env = true if (!opts.username && !use_env) { console.error("Please provide a username using the -u flag(or set the QRZ_USERNAME variable."); process.exit(-1); } if (!opts.password && !use_env) { console.error("Please provide a password using the -p flag (or set the QRZ_PASSWORD variable.") process.exit(-1) } var call = process.argv.find(a=>!a.includes("-")&& !a.includes("/")) if (!call) { console.error("Please provide a callsign (i.e qrzlookup XX1XXX)") process.exit(-1) } var username = process.env.QRZ_USERNAME || opts.username var password = process.env.QRZ_PASSWORD || opts.password var xml = request("GET", `https://xmldata.qrz.com/xml/current/?callsign=${call};username=${username};password=${password}`).getBody("utf8") const $ = cheerio.load(xml) if ($("Error").length > 0) { console.error($("Error")[0].children[0].data) process.exit(-1) } var args = [] call = $("call")[0].children[0].data console.log("Callsign: %s", $("call")[0].children[0].data) console.log("Name: %s", $("name_fmt")[0].children[0].data) if ($("nickname").length > 0) console.log("Nickname: %s", $("nickname")[0].children[0].data) if ($("email").length > 0){ console.log("Email: %s", $("email")[0].children[0].data) args.push(`Email`) } if ($("p_call").length > 0) console.log(`Previously: ${$("p_call")[0].children[0].data}`) console.log("Address:") console.log($("addr1")[0].children[0].data) if ($("addr2").length > 0) process.stdout.write($("addr2")[0].children[0].data+" ") if ($("state").length > 0) process.stdout.write($("state")[0].children[0].data) if ($("zip").length > 0) console.log(" "+$("zip")[0].children[0].data) if ($("county").length > 0) console.log(`${$("county")[0].children[0].data} county`) console.log($("country")[0].children[0].data) args.push("Look at bio") if ($("image").length > 0) args.push("View picture") args.push("QTH on Google Maps") if (!opts.text){ console.log("\nNow, what would you like to do?") cliSelect({ values: args, valueRenderer: (value, selected) => { if (selected) { return chalk.underline(value); } return value; }, }).then(async selected=>{ if (selected.includes("Email")) await open($("email")[0].children[0].data) if (selected.includes("picture")) await open($("image")[0].children[0].data) if (selected.includes("QTH")) await open(`https://maps.google.com/maps?q=${$("addr1")[0].children[0].data}, ${$("zip")[0].children[0].data}, ${$("country")[0].children[0].data}`) if (selected.includes("QTH")) await open(`https://qrz.com/db/${call}`) }).catch(e=>{}) }