|
|
|
try {
|
|
|
|
const { Pool, Client } = require('pg')
|
|
|
|
const request = require('superagent');
|
|
|
|
const Hex = require('crypto-js/enc-hex');
|
|
|
|
const MD5 = require('crypto-js/md5');
|
|
|
|
const XLSX = require('xlsx')
|
|
|
|
const path = require('path')
|
|
|
|
const fs = require("fs");
|
|
|
|
|
|
|
|
// 连接数据库
|
|
|
|
const pool = new Pool({
|
|
|
|
user: 'postgres',
|
|
|
|
host: '10.8.30.32',
|
|
|
|
database: 'highways4good',
|
|
|
|
password: '123',
|
|
|
|
port: 5432,
|
|
|
|
})
|
|
|
|
|
|
|
|
let appid = '20200917000567738';
|
|
|
|
let key = 'xXm4jsuuD38JIkkhEcK6';
|
|
|
|
const getAnswer = async (query) => {
|
|
|
|
let start = (new Date()).getTime();
|
|
|
|
let salt = start;
|
|
|
|
let str1 = appid + query + salt + key;
|
|
|
|
let sign = Hex.stringify(MD5(str1));
|
|
|
|
console.log(`翻译:${query}`);
|
|
|
|
let answer = await request.get('http://api.fanyi.baidu.com/api/trans/vip/translate').timeout(1000 * 30).query({
|
|
|
|
q: query,
|
|
|
|
appid: appid,
|
|
|
|
salt: salt,
|
|
|
|
from: 'zh',
|
|
|
|
to: 'en',
|
|
|
|
sign: sign
|
|
|
|
});
|
|
|
|
if (answer.body.error_code) {
|
|
|
|
console.warn(answer.body);
|
|
|
|
throw '百度不给力,快快debug'
|
|
|
|
}
|
|
|
|
let rslt = answer.body.trans_result;
|
|
|
|
// let upperCaseRslt = rslt[0].dst.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase()).replace(/ /g, '');
|
|
|
|
// let upperCaseRslt = rslt[0].dst.toUpperCase().replace(/ /g, '_');
|
|
|
|
// let upperCaseRslt = rslt[0].dst.toLowerCase().replace(/ /g, '_');
|
|
|
|
let upperCaseRslt = rslt[0].dst.replace(/\//g, ' ').replace(/'/g, '').replace(/\s{2,}/g, '');
|
|
|
|
console.log(`翻译结果:${upperCaseRslt}`);
|
|
|
|
while (((new Date()).getTime() - start) < (1000 / 8)) {//每s只能调用10次
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return upperCaseRslt
|
|
|
|
}
|
|
|
|
|
|
|
|
const fun = async () => {
|
|
|
|
// note: we don't try/catch this because if connecting throws an exception
|
|
|
|
// we don't need to dispose of the client (it will be undefined)
|
|
|
|
const client = await pool.connect()
|
|
|
|
try {
|
|
|
|
await client.query('BEGIN')
|
|
|
|
|
|
|
|
const fileList = [
|
|
|
|
// {
|
|
|
|
// path: './data/道路/村道第三方.xls',
|
|
|
|
// n: '道路',
|
|
|
|
// tableName: 'road',
|
|
|
|
// defaultKey: ['level'],
|
|
|
|
// defaultValue: ['村'],
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// path: './data/道路/县道第三方.xls',
|
|
|
|
// n: '道路',
|
|
|
|
// tableName: 'road',
|
|
|
|
// defaultKey: ['level'],
|
|
|
|
// defaultValue: ['县'],
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// path: './data/道路/乡道第三方.xls',
|
|
|
|
// n: '道路',
|
|
|
|
// tableName: 'road',
|
|
|
|
// defaultKey: ['level'],
|
|
|
|
// defaultValue: ['乡'],
|
|
|
|
// },
|
|
|
|
]
|
|
|
|
|
|
|
|
for (let f of fileList) {
|
|
|
|
console.log(`读取 ${f.path}`);
|
|
|
|
// 读取数据文件
|
|
|
|
let workbook = XLSX.readFile(path.join(__dirname, f.path))
|
|
|
|
let firstSheetName = workbook.SheetNames[0];
|
|
|
|
let worksheet = workbook.Sheets[firstSheetName];
|
|
|
|
let res = XLSX.utils.sheet_to_json(worksheet);
|
|
|
|
const keyMap = require(`./${f.n}_数据库表对应.json`);
|
|
|
|
// console.log(keyMap);
|
|
|
|
for (let d of res) {
|
|
|
|
let insertStr = `INSERT INTO "${f.tableName}" (`;
|
|
|
|
let insertKeys = (f.defaultKey || []).concat([]);
|
|
|
|
let insertValues = (f.defaultValue || []).concat([]);
|
|
|
|
for (let k in keyMap) {
|
|
|
|
// 没做判重
|
|
|
|
let v = d[k];
|
|
|
|
if (v) {
|
|
|
|
insertKeys.push(keyMap[k]);
|
|
|
|
insertValues.push(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
insertStr += insertKeys.join(',') + ') VALUES (';
|
|
|
|
insertStr += insertKeys.map((k, i) => `$${i + 1}`).join(',') + ')';
|
|
|
|
// console.log(insertStr, insertValues);
|
|
|
|
console.log(`插入 ${f.tableName}:${insertStr}`);
|
|
|
|
await client.query(insertStr, insertValues);
|
|
|
|
// break;
|
|
|
|
}
|
|
|
|
// break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// await client.query('ROLLBACK')
|
|
|
|
await client.query('COMMIT')
|
|
|
|
console.log('执行完毕~')
|
|
|
|
} catch (e) {
|
|
|
|
await client.query('ROLLBACK')
|
|
|
|
console.log('执行错误~')
|
|
|
|
throw e
|
|
|
|
} finally {
|
|
|
|
client.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun()
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
}
|