-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
211 lines (194 loc) · 7.75 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import * as fs from 'fs'
import * as path from 'path'
import * as PDF from 'pdfkit'
import * as conf from './configuration'
import * as SQL from 'sqlite3'
import { exit } from 'process'
const songs: Song[] = []
const songMap: Map<string, Song> = new Map()
interface Score {score: number, player: string, difficulty: number, date: number}
class Song {
dir = ''
folder = ''
filename = ''
title = ''
artist = ''
language = ''
genre = ''
year = ''
creator = ''
mp3 = ''
cover = ''
video = ''
bpm = ''
gap = ''
medley = false
duo = false
scores: Score[] = []
constructor(dir: string, folder: string, filename: string) {
this.dir = dir
this.folder = folder
this.filename = filename
this.readfile()
}
static read(dir: string, folder = '') {
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file)
if (fs.lstatSync(fullPath).isDirectory()) this.read(fullPath, file)
else if (file.endsWith('.txt')) Song.addSong(dir, folder, file)
})
}
static addSong(dir: string, folder: string, filename: string) {
const song = new Song(dir, folder, filename)
if (songMap.has(song.index)) { // Check duo
const otherSong = songMap.get(song.index) as Song
if ((song.filename.endsWith('[MULTI].txt') && !otherSong.filename.endsWith('[MULTI].txt')) ||
(!song.filename.endsWith('[MULTI].txt') && otherSong.filename.endsWith('[MULTI].txt')))
otherSong.duo = true
return
}
songs.push(song)
songMap.set(song.index, song)
}
static sort(songs: Song[], sortBy: string) {
songs.sort((a, b) => {
switch (sortBy) {
case 'a': return a.artist.localeCompare(b.artist)
case 't': return a.title.localeCompare(b.title)
case 'l': return a.language.localeCompare(b.language)
case 'g': return a.genre.localeCompare(b.genre)
case 'y': return a.year.localeCompare(b.year)
case 'c': return a.creator.localeCompare(b.creator)
default: return 0
}
})
}
private get index(): string { return this.artist + '.' + this.title}
private readfile() {
const fullpath = path.join(this.dir, this.filename)
const contents = fs.readFileSync(fullpath, 'latin1')
const lines = contents.split(/\r?\n/)
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
if (line.indexOf('#TITLE:') !== -1) this.title = line.substring(line.indexOf('#TITLE:') + 7).trim()
if (line.indexOf('#ARTIST:') !== -1) this.artist = line.substring(line.indexOf('#ARTIST:') + 8).trim()
if (line.indexOf('#LANGUAGE:') !== -1) this.language = line.substring(line.indexOf('#LANGUAGE:') + 10).trim()
if (line.indexOf('#GENRE:') !== -1) this.genre = line.substring(line.indexOf('#GENRE:') + 7).trim()
if (line.indexOf('#YEAR:') !== -1) this.year = line.substring(line.indexOf('#YEAR:') + 6).trim()
if (line.indexOf('#CREATOR:') !== -1) this.creator = line.substring(line.indexOf('#CREATOR:') + 9).trim()
if (line.indexOf('#MP3:') !== -1) this.mp3 = line.substring(line.indexOf('#MP3:') + 5).trim()
if (line.indexOf('#COVER:') !== -1) this.cover = line.substring(line.indexOf('#COVER:') + 7).trim()
if (line.indexOf('#VIDEO:') !== -1) this.video = line.substring(line.indexOf('#VIDEO:') + 7).trim()
if (line.indexOf('#BPM:') !== -1) this.bpm = line.substring(line.indexOf('#BPM:') + 5).trim()
if (line.indexOf('#GAP:') !== -1) this.gap = line.substring(line.indexOf('#GAP:') + 5).trim()
if (line.indexOf('#MEDLEYSTARTBEAT:') !== -1) this.medley = true
if (line.startsWith(':')) break
}
}
addInfo(pdf: PDFKit.PDFDocument, format: string) {
const items = format.split('.')
items.forEach((item, index) => {
const last = index === items.length - 1
this.addItem(pdf, item, 'a', this.artist, last)
this.addItem(pdf, item, 't', this.title, last)
this.addItem(pdf, item, 'l', this.language, last)
this.addItem(pdf, item, 'g', this.genre, last)
this.addItem(pdf, item, 'y', this.year, last)
this.addItem(pdf, item, 'c', this.creator, last)
this.addItem(pdf, item, 'v', this.video !== '', last)
this.addItem(pdf, item, 'd', this.duo, last)
this.addItem(pdf, item, 'm', this.medley, last)
this.addItem(pdf, item, 'h', this.highScore, last)
this.addItem(pdf, item, 'x', item.substring(1), last)
})
}
private addItem(pdf: PDFKit.PDFDocument, item: string, ch: string, s: string | boolean, last: boolean) {
if (!item.startsWith(ch)) return
pdf.font(item.endsWith('b') ? 'Courier-Bold' : 'Courier')
if (typeof s === 'string') this.addText(pdf, s, last)
if (typeof s === 'boolean') this.addText(pdf, s ? ch : ' ', last)
}
private addText(pdf: PDFKit.PDFDocument, text: string, last: boolean) {
if (text.length > conf.tooLong) pdf.fontSize(conf.fontSizeSmall)
pdf.text(text, {continued: !last}).fontSize(conf.fontSize)
}
private get highScore(): string {
if (this.scores.length === 0) return ' '
this.scores.sort((a, b) => b.score - a.score)
const highScore = this.scores[0]
return `${highScore.player}(${highScore.score})`
}
}
interface DbScore {
Artist: string
Title: string
Difficulty: number
Player: string
Score: number
Date: number
}
class Db {
static read() {
const db = new SQL.Database(conf.db)
try {
db.all('select Artist, Title, Player, Score, Date from us_songs s, us_scores r where s.ID = r.SongID', (error: Error, rows: DbScore[]) => {
if (!rows) {
console.log('Error accessing the database, please check the path: ' + conf.db)
exit(1)
}
rows.forEach(row => {
const artist = row.Artist
const title = row.Title
// The entries in Ultrastar.db have a null a the end, it must be removed
const key = artist.substring(0, artist.length - 1) + '.' + title.substring(0, title.length - 1)
const song = songMap.get(key)
if (song) song.scores.push({score: row.Score, player: row.Player.substring(0, row.Player.length - 1), difficulty: row.Difficulty, date: row.Date})
})
db.close()
Job.execute()
})
} catch (error) {
console.log('Error accessing the database, please check the path: ' + conf.db)
exit(1)
}
}
}
class Job {
static init() {
try {
Song.read(conf.path)
if (songs.length === 0) {
console.log('No songs found, please check the directory: ' + conf.path)
return
}
} catch (error) {
console.log('Error reading the songs, please check the directory: ' + conf.path)
return
}
conf.options.split('.').forEach(option => {
if (option.startsWith('s')) Song.sort(songs, option.substring(option.length - 1))
})
if (conf.checkDb) Db.read()
else this.execute()
}
private static list(predicate: (song: Song) => boolean) {
const pdf = new PDF({margin: conf.margin, size: conf.size, layout: conf.layout})
pdf.pipe(fs.createWriteStream(conf.output))
pdf.fontSize(conf.fontSize)
songs.filter(predicate).forEach(song => song.addInfo(pdf, conf.format))
pdf.end()
}
static execute() {
switch (conf.job) {
case 'printList': return this.list(song => true)
case 'noVideos': return this.list(song => !song.video)
case 'noMedley': return this.list(song => !song.medley)
case 'noYear': return this.list(song => song.year.length !== 4)
case 'withDuo': return this.list(song => song.duo)
case 'withScore': return this.list(song => song.scores.length !== 0)
case 'noScore': return this.list(song => song.scores.length === 0)
default: console.log(`Please provide a valid job: 'printList' 'noVideos' 'noMedley' 'noYear' 'with Duo' 'withScore' 'noScore'`)
}
}
}
Job.init()