-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f6c49a
commit abe6911
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import axios from "axios"; | ||
import fs from "fs"; | ||
import moment from "moment"; | ||
import path from "path"; | ||
import { | ||
BABELSTONE_PUA_JSON_NAME, | ||
BABELSTONE_PUA_JSON_URL, | ||
BABELSTONE_SUBDIR_NAME, | ||
DATA_CACHE_DIR_NAME, | ||
} from "./constants"; | ||
|
||
const BABELSTONE_SUBDIR = path.join( | ||
__dirname, | ||
DATA_CACHE_DIR_NAME, | ||
BABELSTONE_SUBDIR_NAME | ||
); | ||
|
||
const BABELSTONE_PUA_FILE = path.join( | ||
BABELSTONE_SUBDIR, | ||
BABELSTONE_PUA_JSON_NAME | ||
); | ||
|
||
type PUAEntry = { | ||
cp: string; | ||
char: string; | ||
ids: string; | ||
note: string; | ||
src: string; | ||
src_refs: string; | ||
enc_stat: string; | ||
}; | ||
|
||
type PUA = { | ||
data: PUAEntry[]; | ||
ts: string; | ||
}; | ||
|
||
const isValidBabelPUAFile = (obj: any) => { | ||
return "data" in obj && "ts" in obj && moment(obj.ts).isValid(); | ||
}; | ||
|
||
const fetchBabelData = async () => { | ||
if (!fs.existsSync(BABELSTONE_SUBDIR)) { | ||
fs.mkdirSync(BABELSTONE_SUBDIR); | ||
} | ||
|
||
try { | ||
if (!fs.existsSync(BABELSTONE_PUA_FILE)) throw new Error(); | ||
|
||
const data = JSON.parse(fs.readFileSync(BABELSTONE_PUA_FILE).toString()); | ||
|
||
if (!isValidBabelPUAFile(data)) throw new Error(); | ||
} catch { | ||
console.error("Invalid Babelstone Han PUA file"); | ||
fs.writeFileSync( | ||
BABELSTONE_PUA_FILE, | ||
JSON.stringify({ | ||
data: [], | ||
ts: moment(0).toISOString(), | ||
}) | ||
); | ||
} | ||
|
||
const data = JSON.parse( | ||
fs.readFileSync(BABELSTONE_PUA_FILE).toString() | ||
) as PUA; | ||
|
||
console.log("Connecting …"); | ||
|
||
const remoteData = (await axios.get<PUA>(BABELSTONE_PUA_JSON_URL)).data; | ||
|
||
if (moment(remoteData.ts) > moment(data.ts)) { | ||
fs.writeFileSync(BABELSTONE_PUA_FILE, JSON.stringify(remoteData)); | ||
} | ||
}; | ||
|
||
export const getPUAData = async (): Promise<PUA> => { | ||
await fetchBabelData(); | ||
const buf = await fs.promises.readFile(BABELSTONE_PUA_FILE); | ||
return JSON.parse(buf.toString()); | ||
}; |