Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
ygopro-cn-database-generator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
ygopro-cn-database-generator
Commits
8ac28b2b
Commit
8ac28b2b
authored
Oct 08, 2020
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
name to code
parent
bd75b87d
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
15 deletions
+75
-15
config.example.yaml
config.example.yaml
+1
-1
src/base.ts
src/base.ts
+18
-0
src/dbreader.ts
src/dbreader.ts
+48
-0
src/fetcher.ts
src/fetcher.ts
+2
-14
test/fetch.ts
test/fetch.ts
+6
-0
No files found.
config.example.yaml
View file @
8ac28b2b
postDepth
:
5
jpDatabasePath
:
./ygopro-database/locales/ja-JP/cards.cdb
cnDatabasePath
:
./ygopro-database/locales/zh-CN/cards.cdb
outputPath
:
./output
/cn.cdb
outputPath
:
./output
src/base.ts
0 → 100644
View file @
8ac28b2b
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
();
}
}
src/dbreader.ts
0 → 100644
View file @
8ac28b2b
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
);
}
}
src/fetcher.ts
View file @
8ac28b2b
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
,
{
...
...
test/fetch.ts
View file @
8ac28b2b
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
();
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment