Commit 8ac28b2b authored by nanahira's avatar nanahira

name to code

parent bd75b87d
postDepth: 5
jpDatabasePath: ./ygopro-database/locales/ja-JP/cards.cdb
cnDatabasePath: ./ygopro-database/locales/zh-CN/cards.cdb
outputPath: ./output/cn.cdb
outputPath: ./output
import { Config, loadConfig } from "./config";
import bunyan from "bunyan";
import _ from "underscore";
export default abstract class Base {
config: Config;
log: bunyan;
constructor(loggerOptions: bunyan.LoggerOptions) {
this.log = bunyan.createLogger(loggerOptions);
}
protected async loadConfig() {
this.config = await loadConfig();
}
async init() {
this.log.debug("Reading config...");
await this.loadConfig();
}
}
import { open, Database } from "sqlite";
import sqlite3 from "sqlite3";
import _ from "underscore";
import Base from "./base";
import { promises as fs } from "fs";
export class DBReader extends Base {
jpdb: Database;
cndb: Database;
outputdb: Database;
private async openDatabase(path: string) {
return await open({
filename: path,
driver: sqlite3.Database
});
}
async init() {
await super.init();
this.log.debug(`Opening databases...`);
this.cndb = await this.openDatabase(this.config.cnDatabasePath);
this.jpdb = await this.openDatabase(this.config.jpDatabasePath);
}
private async openOutputDatabase() {
try {
await fs.access(this.config.outputPath);
} catch (e) {
await fs.mkdir(this.config.outputPath, { recursive: true });
}
this.outputdb = await this.openDatabase(this.config.outputPath);
}
async getCodeFromJapaneseName(name: string): Promise<number[]> {
this.log.debug(`Reading JP database for code of name ${name}.`);
const output = await this.jpdb.get('SELECT id FROM texts WHERE name = ?', name);
if (!output) {
this.log.debug(`Code of ${name} not found.`);
return [];
}
const code: number = output.id;
this.log.debug(`Reading CN database for more codes of id ${code}.`);
const moreCodes: number[] = (await this.cndb.all('SELECT id FROM datas WHERE id >= ? AND id <= ?', [code, code + 10])).map(m => m.id);
this.log.debug(`${name} => ${moreCodes.join(",")}`);
return moreCodes;
}
async getAllCodesFromJapaneseNames(names: string[]): Promise<number[]> {
const codes = _.flatten(await Promise.all(names.map(s => this.getCodeFromJapaneseName(s))), true);
return _.uniq(codes);
}
}
import axios from "axios";
import { Config, loadConfig } from "./config";
import bunyan from "bunyan";
import _ from "underscore";
import Base from "./base";
export class CNFetcher {
config: Config;
log: bunyan;
constructor(loggerOptions: bunyan.LoggerOptions) {
this.log = bunyan.createLogger(loggerOptions);
}
async init() {
this.log.debug("Initializing...");
this.log.debug("Reading config...");
this.config = await loadConfig();
this.log.debug("Initialized.");
}
export class CNFetcher extends Base {
private async fetchPage(url: string): Promise<string> {
this.log.debug(`Downloading content from ${url} .`);
const { data } = await axios.get(url, {
......
import { DBReader } from "../src/dbreader";
import { CNFetcher } from "../src/fetcher";
import _ from "underscore";
async function main() {
const fetcher = new CNFetcher({ name: "Test fetch", level: "debug" });
await fetcher.init();
const dbreader = new DBReader({ name: "Test database", level: "debug" });
await dbreader.init();
const strings = await fetcher.fetch();
console.log(strings);
const codes = _.flatten(await Promise.all(strings.map(s => dbreader.getCodeFromJapaneseName(s))), true);
console.log(codes);
process.exit();
}
main();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment