2024-06-22 16:40:56 +02:00
|
|
|
#!/usr/bin/env nix-shell
|
|
|
|
#!nix-shell -p "pkgs.python3.withPackages (p: with p; [tabulate])"
|
|
|
|
#!nix-shell -i python3
|
|
|
|
import csv
|
|
|
|
import os
|
|
|
|
import sqlite3
|
|
|
|
import sys
|
|
|
|
|
|
|
|
data_dir = os.environ.get("SHIMMERING_DATA_DIR")
|
|
|
|
db_path = data_dir + "/db.sqlite"
|
|
|
|
# if not os.path.exists(db_path):
|
|
|
|
# run(f"cat ./schema.sql | sqlite3 {db_path}")
|
|
|
|
conn = sqlite3.connect(db_path)
|
|
|
|
|
|
|
|
|
|
|
|
# {{{ Import songs
|
2024-06-23 02:51:50 +02:00
|
|
|
def import_charts_from_csv():
|
|
|
|
chart_count = 0
|
2024-07-01 18:00:03 +02:00
|
|
|
song_count = 0
|
|
|
|
shorthand_count = 0
|
2024-06-22 16:40:56 +02:00
|
|
|
|
2024-06-23 02:51:50 +02:00
|
|
|
with open(data_dir + "/charts.csv", mode="r") as file:
|
2024-07-01 18:00:03 +02:00
|
|
|
for i, row in enumerate(csv.reader(file)):
|
|
|
|
if i == 0 or len(row) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
song_count += 1
|
|
|
|
[
|
|
|
|
title,
|
|
|
|
artist,
|
|
|
|
pack,
|
|
|
|
*charts,
|
|
|
|
side,
|
|
|
|
bpm,
|
|
|
|
version,
|
|
|
|
date,
|
|
|
|
ext_version,
|
|
|
|
ext_date,
|
|
|
|
original,
|
|
|
|
] = map(lambda v: v.strip().replace("\n", " "), row)
|
|
|
|
|
|
|
|
song_id = conn.execute(
|
2024-06-22 16:40:56 +02:00
|
|
|
"""
|
2024-07-01 18:00:03 +02:00
|
|
|
INSERT INTO songs(title,artist,pack,side,bpm)
|
|
|
|
VALUES (?,?,?,?,?)
|
|
|
|
RETURNING id
|
|
|
|
""",
|
|
|
|
(title, artist, pack, side.lower(), bpm),
|
|
|
|
).fetchone()[0]
|
|
|
|
|
|
|
|
for i in range(4):
|
|
|
|
[note_design, level, cc, note_count] = charts[i * 4 : (i + 1) * 4]
|
|
|
|
if note_design == "N/A":
|
|
|
|
continue
|
|
|
|
chart_count += 2
|
|
|
|
|
|
|
|
[difficulty, level] = level.split(" ")
|
|
|
|
|
|
|
|
conn.execute(
|
|
|
|
"""
|
|
|
|
INSERT INTO charts(song_id, difficulty, level, note_count, chart_constant, note_design)
|
2024-06-23 02:51:50 +02:00
|
|
|
VALUES(?,?,?,?,?, ?)
|
2024-06-22 16:40:56 +02:00
|
|
|
""",
|
2024-07-01 18:00:03 +02:00
|
|
|
(
|
|
|
|
song_id,
|
|
|
|
difficulty,
|
|
|
|
level,
|
|
|
|
int(note_count.replace(",", "").replace(".", "")),
|
|
|
|
int(float(cc) * 100),
|
|
|
|
note_design if len(note_design) else None,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(data_dir + "/shorthands.csv", mode="r") as file:
|
|
|
|
for i, row in enumerate(csv.reader(file)):
|
|
|
|
if i == 0 or len(row) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
shorthand_count += 1
|
|
|
|
[name, difficulty, artist, shorthand] = map(lambda v: v.strip(), row)
|
|
|
|
conn.execute(
|
|
|
|
f"""
|
|
|
|
UPDATE charts
|
|
|
|
SET shorthand=?
|
|
|
|
WHERE EXISTS (
|
|
|
|
SELECT 1 FROM songs s
|
|
|
|
WHERE s.id = charts.song_id
|
|
|
|
AND s.title=?
|
|
|
|
{"" if artist=="" else "AND artist=?"}
|
|
|
|
)
|
|
|
|
{"" if difficulty=="" else "AND difficulty=?"}
|
|
|
|
""",
|
|
|
|
[
|
|
|
|
shorthand,
|
|
|
|
name,
|
|
|
|
*([] if artist == "" else [artist]),
|
|
|
|
*([] if difficulty == "" else [difficulty]),
|
|
|
|
],
|
2024-06-23 02:51:50 +02:00
|
|
|
)
|
2024-06-22 16:40:56 +02:00
|
|
|
|
2024-06-23 02:51:50 +02:00
|
|
|
conn.commit()
|
2024-06-22 16:40:56 +02:00
|
|
|
|
2024-07-01 18:00:03 +02:00
|
|
|
print(
|
|
|
|
f"Imported {chart_count} charts, {song_count} songs, and {shorthand_count} shorthands"
|
|
|
|
)
|
2024-06-22 16:40:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
command = sys.argv[1]
|
|
|
|
subcommand = sys.argv[2]
|
|
|
|
|
|
|
|
if command == "import" and subcommand == "charts":
|
2024-06-23 02:51:50 +02:00
|
|
|
import_charts_from_csv()
|
2024-06-23 04:20:32 +02:00
|
|
|
if command == "export" and subcommand == "jackets":
|
|
|
|
import_charts_from_csv()
|