mirror of
https://git.stupid.fish/teidesu/scripts.git
synced 2025-07-28 02:32:11 +10:00
chore: update public repo
This commit is contained in:
parent
8c04afc6d2
commit
3057b2a78c
1 changed files with 60 additions and 0 deletions
60
scripts/media/lastfm-csv-to-sqlite.ts
Normal file
60
scripts/media/lastfm-csv-to-sqlite.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import { createReadStream } from 'node:fs'
|
||||
import { nodeReadableToFuman } from '@fuman/node'
|
||||
import Database from 'better-sqlite3'
|
||||
import { question } from 'zx'
|
||||
import { CsvReader } from '../../utils/csv.ts'
|
||||
|
||||
const csvPath = process.argv[2] ?? await question('path to csv > ')
|
||||
|
||||
// convert csv generated by https://mainstream.ghan.nl/export.html to an sqlite database
|
||||
|
||||
const db = new Database('assets/lastfm-import.db')
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS scrobbles (
|
||||
date_uts TEXT,
|
||||
artist_mbid TEXT,
|
||||
artist_name TEXT,
|
||||
album_mbid TEXT,
|
||||
album_name TEXT,
|
||||
track_mbid TEXT,
|
||||
track_name TEXT
|
||||
);
|
||||
`)
|
||||
|
||||
const insertQuery = db.prepare(`
|
||||
INSERT INTO scrobbles (
|
||||
date_uts,
|
||||
artist_mbid,
|
||||
artist_name,
|
||||
album_mbid,
|
||||
album_name,
|
||||
track_mbid,
|
||||
track_name
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`)
|
||||
|
||||
const file = nodeReadableToFuman(createReadStream(csvPath))
|
||||
const csv = new CsvReader(file, {
|
||||
schema: ['uts', 'utc_time', 'artist', 'artist_mbid', 'album', 'album_mbid', 'track', 'track_mbid'],
|
||||
})
|
||||
|
||||
let i = 0
|
||||
while (true) {
|
||||
const obj = await csv.read()
|
||||
if (!obj) break
|
||||
|
||||
i += 1
|
||||
if (i % 1000 === 0) {
|
||||
console.log('inserted', i)
|
||||
}
|
||||
|
||||
insertQuery.run(
|
||||
obj.uts,
|
||||
obj.artist_mbid,
|
||||
obj.artist,
|
||||
obj.album_mbid,
|
||||
obj.album,
|
||||
obj.track_mbid,
|
||||
obj.track,
|
||||
)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue