Starting to work with cities

This commit is contained in:
leca 2024-08-09 01:56:19 +03:00
parent 21731dc0ae
commit 2b33fec932
7 changed files with 54042 additions and 2 deletions

2
.gitignore vendored
View File

@ -139,4 +139,4 @@ reg.js
encryption_bot_sled
bot.json
bot_data
postgres
postgres

View File

@ -40,3 +40,6 @@ Assuming, that the database is running locally and on a port 5432, change the va
### Update:
``git pull``
### Thanks
Thanks to https://simplemaps.com/resources/free-country-cities for the free version of a database with cities!

6109
cities.sql Normal file

File diff suppressed because it is too large Load Diff

42
csvtosql.js Normal file
View File

@ -0,0 +1,42 @@
import pg from "pg";
import fs from "fs";
import readline from 'readline';
const { Client } = pg;
const getClient = async () => {
const client = new Client({
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
database: process.env.POSTGRES_DB
});
await client.connect();
await client.query(fs.readFileSync('./scheme.psql').toString());
return client;
};
const db = await getClient();
const file = fs.readFileSync("./output.csv").toString();
const inFileStream = fs.createReadStream("./output.csv");
const rl = readline.createInterface({
input: inFileStream,
output: null,
crlfDelay: Infinity
})
rl.on('line', async (line) => {
let splitted = line.split(",")
await db.query(`INSERT INTO cities (name, lat, lng, country) VALUES ($1, $2, $3, $4);`, [splitted[0], splitted[1], splitted[2], splitted[3]])
});
rl.on('close', () => {
console.log("done");
})

47868
output.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS users (
language VARCHAR(8) DEFAULT 'en',
country VARCHAR(64) DEFAULT NULL,
city VARCHAR(64) DEFAULT NULL,
range DOUBLE PRECISION DEFAULT 20.0,
current_action VARCHAR(16) DEFAULT NULL,
currently_viewing VARCHAR(64) --link to "room_id"
);
@ -37,3 +38,10 @@ CREATE TABLE IF NOT EXISTS messages (
);
CREATE UNIQUE INDEX IF NOT EXISTS unique_messages ON messages(sender, recipient);
CREATE TABLE IF NOT EXISTS cities (
name VARCHAR(32),
lat REAL,
lng REAL,
country VARCHAR(32)
);

View File

@ -1,6 +1,7 @@
import pg from 'pg';
import fs from 'fs';
import { convertMsgType } from './utils.js';
import { convertMsgType, logError } from './utils.js';
import { exec } from 'child_process';
const { Client } = pg;
@ -16,6 +17,15 @@ const getClient = async () => {
await client.query(fs.readFileSync('./scheme.psql').toString());
if ((await client.query("SELECT * FROM cities")).rowCount < 6079) {
await client.query("DELETE FROM cities");
//Not sure if pg has support for such kind of things, sooooooooo
exec(`psql -h ${process.env.POSTGRES_HOST} -p ${process.env.POSTGRES_PORT} -d ${process.env.POSTGRES_DB} -U ${process.env.POSTGRES_USER} -f ./cities.sql`, (error) => {
if (error) logError(error);
})
}
return client;
};