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
|
|
|
|
songs = dict()
|
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-06-22 16:40:56 +02:00
|
|
|
for row in csv.reader(file):
|
|
|
|
if len(row) > 0:
|
|
|
|
chart_count += 1
|
|
|
|
[title, difficulty, level, cc, _, note_count, _, _, _] = row
|
|
|
|
if songs.get(title) is None:
|
2024-06-23 02:51:50 +02:00
|
|
|
songs[title] = {"charts": [], "shorthand": None}
|
|
|
|
songs[title]["charts"].append([difficulty, level, cc, note_count, None])
|
2024-06-22 16:40:56 +02:00
|
|
|
|
2024-06-23 02:51:50 +02:00
|
|
|
with open(data_dir + "/jackets.csv", mode="r") as file:
|
|
|
|
for row in csv.reader(file):
|
|
|
|
if len(row) > 0:
|
|
|
|
[title, jacket, difficulty] = row
|
|
|
|
if difficulty.strip() != "":
|
|
|
|
changed = 0
|
|
|
|
|
|
|
|
for i in range(len(songs[title]["charts"])):
|
|
|
|
if songs[title]["charts"][i][0] == difficulty:
|
|
|
|
songs[title]["charts"][i][4] = jacket
|
|
|
|
changed += 1
|
|
|
|
|
|
|
|
if changed == 0:
|
|
|
|
raise f"Nothing changed for chart {title} [{difficulty}]"
|
|
|
|
else:
|
|
|
|
for i in range(len(songs[title]["charts"])):
|
|
|
|
songs[title]["charts"][i][4] = jacket
|
|
|
|
|
|
|
|
with open(data_dir + "/shorthands.csv", mode="r") as file:
|
|
|
|
for row in csv.reader(file):
|
|
|
|
if len(row) > 0:
|
|
|
|
[title, shorthand] = row
|
|
|
|
songs[title]["shorthand"] = shorthand
|
|
|
|
|
|
|
|
for title, entry in songs.items():
|
|
|
|
artist = None
|
|
|
|
|
|
|
|
if title.startswith("Quon"):
|
|
|
|
artist = title[6:-1]
|
|
|
|
title = "Quon"
|
2024-06-22 16:40:56 +02:00
|
|
|
|
2024-06-23 02:51:50 +02:00
|
|
|
row = conn.execute(
|
|
|
|
"""
|
|
|
|
INSERT INTO songs(title,artist,ocr_alias)
|
|
|
|
VALUES (?,?,?)
|
|
|
|
RETURNING id
|
|
|
|
""",
|
|
|
|
(title, artist, entry.get("shorthand")),
|
|
|
|
).fetchone()
|
|
|
|
song_id = row[0]
|
2024-06-22 16:40:56 +02:00
|
|
|
|
2024-06-23 02:51:50 +02:00
|
|
|
for difficulty, level, cc, note_count, jacket in entry["charts"]:
|
|
|
|
conn.execute(
|
2024-06-22 16:40:56 +02:00
|
|
|
"""
|
2024-06-23 02:51:50 +02:00
|
|
|
INSERT INTO charts(song_id, difficulty, level, note_count, chart_constant, jacket)
|
|
|
|
VALUES(?,?,?,?,?, ?)
|
2024-06-22 16:40:56 +02:00
|
|
|
""",
|
2024-06-23 02:51:50 +02:00
|
|
|
(
|
|
|
|
song_id,
|
|
|
|
difficulty,
|
|
|
|
level,
|
|
|
|
int(note_count.replace(",", "").replace(".", "")),
|
|
|
|
int(float(cc) * 100),
|
|
|
|
jacket,
|
|
|
|
),
|
|
|
|
)
|
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-06-23 02:51:50 +02:00
|
|
|
print(f"Imported {chart_count} charts and {len(songs)} songs")
|
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()
|