mono/packages/search/commands/googlemaps.js
2025-03-11 11:28:14 +01:00

114 lines
5.0 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = void 0;
const path = require("path");
const __1 = require("../");
const _cli_1 = require("../_cli");
const report_map_1 = require("../lib/report_map");
const objects_1 = require("@plastichub/core/objects");
const write_1 = require("@plastichub/fs/write");
const read_1 = require("@plastichub/fs/read");
const exists_1 = require("@plastichub/fs/exists");
const pMap = require("p-map");
const options_1 = require("./options");
const geo_1 = require("../lib/geo");
const googlemaps_1 = require("../lib/googlemaps");
let options = (yargs) => (0, googlemaps_1.yargsOptions)(yargs);
const register = (cli) => {
return cli.command('googlemaps <verb>', 'Search GoogleMaps', options, (argv) => __awaiter(void 0, void 0, void 0, function* () {
(0, _cli_1.defaults)();
if (argv.help) {
return;
}
let opts = null;
try {
opts = (0, googlemaps_1.zodSchema)().parse(argv);
}
catch (e) {
__1.logger.error('Invalid options', e.message, e.issues);
return;
}
// opts = parse(argv)
return yield (0, googlemaps_1.googleMaps)(opts);
if (!opts) {
__1.logger.error('Invalid options', opts);
return;
}
try {
if (opts.searchFrom && opts.geocode_key) {
const coords = yield (0, geo_1.geocode_forward)(opts.searchFrom, opts.geocode_key);
if (coords) {
opts.searchCoord = (0, googlemaps_1.locationString)(coords, opts.zoom);
}
else {
__1.logger.error('Error geocoding', opts.searchFrom);
}
}
}
catch (error) {
__1.logger.error('Error geocoding', error, error.stack);
}
let ret = [];
const search = (query, category, opts) => __awaiter(void 0, void 0, void 0, function* () {
opts = (0, objects_1.clone)(opts);
opts.dst = (0, options_1.resolvePath)(argv.dst, query, category, opts);
if (opts.cache !== false && (0, exists_1.sync)(opts.dst + '.json')) {
__1.logger.debug('Skipping', opts.dst + '.json');
return (0, read_1.sync)(opts.dst.replace('.xlsx', 'json')) || [];
}
try {
const sr = yield (0, googlemaps_1.searchGoogleMap)(query, opts.api_key, Object.assign({}, opts));
if (sr && sr.length && opts.dst) {
__1.logger.debug('Writing', opts.dst);
(0, write_1.sync)(opts.dst + '.json', sr);
(0, report_map_1.writeReport)(sr, opts.dst, opts);
const parts = path.parse(opts.dst);
(0, write_1.sync)(path.join(parts.dir, parts.name + '_options.json'), (0, __1.cleanOptions)(opts));
// writeReport(sr, opts.dst.replace('.xlsx', '.md'), opts)
}
ret = [...ret, ...sr];
return ret;
}
catch (error) {
__1.logger.error('Error searching GoogleMaps : ' + error.message, error, error.stack);
}
});
const results = yield pMap(Object.keys(opts.source), (k) => __awaiter(void 0, void 0, void 0, function* () {
return yield pMap(opts.source[k], (t) => __awaiter(void 0, void 0, void 0, function* () {
return search(t, k, opts);
}), {
concurrency: 1
});
}), {
concurrency: 1
});
if (opts.dst) {
opts.dst = (0, options_1.resolvePath)(argv.dst, 'all', 'all', opts);
if ((0, exists_1.sync)(opts.dst + '.json')) {
const last = ((0, read_1.sync)(opts.dst + '.json', 'json') || []);
ret = [...last, ...ret];
}
(0, write_1.sync)(opts.dst + '.json', ret);
(0, report_map_1.writeReport)(ret, opts.dst, opts);
}
if (opts.index) {
let index = (0, read_1.sync)(opts.index, 'json') || {};
ret.forEach((r) => {
index[r.title] = r;
});
(0, write_1.sync)(opts.index, index);
}
return false;
}));
};
exports.register = register;
//# sourceMappingURL=googlemaps.js.map