euterpe-source/packages/preprocessor/src/generate_db.ts

58 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-02-12 15:28:54 +00:00
import { Collection, Ref, RefTo, Song, DB } from "@euterpe.js/music-library"
import { songs } from "./songs_list"
export function generate_db() {
2023-12-27 17:47:54 +00:00
console.log(songs)
// construct db
2024-02-12 15:28:54 +00:00
let db = new DB()
2023-12-27 17:47:54 +00:00
let collections: string[] = new Array()
let new_songs = []
const path_char = songs[0].includes("\\") ? "\\" : "/"
2023-12-27 17:47:54 +00:00
//create collections by folder names
for (let i = 0; i < songs.length; i++) {
const song = songs[i]
const last_i = song.lastIndexOf(path_char)
2024-02-12 15:36:02 +00:00
const collection_name = song.slice(song.slice(0, last_i).lastIndexOf(path_char) + 1, last_i)
2023-12-27 17:47:54 +00:00
/*
const foreforelast_i = song.slice(0, forelast_i - 1)
const foreforeforelast_i = song.slice(0, foreforelast_i - 1).lastIndexOf("\\")
*/
if (!collections.includes(collection_name)) {
console.log(`creating collection ${collection_name}`)
2024-02-12 15:28:54 +00:00
db.add([
new Collection({
name: collection_name,
songs: [],
artists: []
})
])
2023-12-27 17:47:54 +00:00
collections.push(collection_name)
}
2023-07-20 15:28:22 +00:00
2024-02-12 15:28:54 +00:00
let col = db.collections.find((col) => col.name == collection_name)!
2023-12-27 17:47:54 +00:00
let col_id = col.id
new_songs.push({ song: song, collection_id: col_id! })
}
2023-12-27 17:47:54 +00:00
//create songs
for (let i = 0; i < new_songs.length; i++) {
let song = new_songs[i]
const last_i = song.song.lastIndexOf(path_char)
2023-12-27 17:47:54 +00:00
const name = song.song.slice(last_i + 1)
2024-02-12 15:36:02 +00:00
const song_url = song.song.slice(song.song.indexOf(`public${path_char}`) + 7)
2023-12-27 17:47:54 +00:00
const db_song = new Song({
name: name.slice(0, name.lastIndexOf(".")),
artists: [],
2024-02-12 15:36:02 +00:00
url: new URL(`${window.location.href}${song_url}`.replaceAll("\\", "/")),
2023-12-27 17:47:54 +00:00
duration: 0,
remix_artists: [],
in_collection: new Ref(RefTo.Collections, song.collection_id)
})
db.add([db_song])
}
console.log(db)
return db
}