Moved the db

This commit is contained in:
Connor Johnstone
2026-03-03 13:10:37 -05:00
parent ba094a5dc5
commit f5f0273853
3 changed files with 23 additions and 10 deletions

View File

@@ -2,18 +2,19 @@
"""Fuzzy search artists in playlists.db and show their similar artists.""" """Fuzzy search artists in playlists.db and show their similar artists."""
import curses import curses
import os
import sqlite3 import sqlite3
import sys import sys
from pathlib import Path from pathlib import Path
def find_db(): def find_db():
"""Look for playlists.db in cwd, then script's parent dir.""" """Find playlists.db in XDG data dir."""
for base in [Path.cwd(), Path(__file__).resolve().parent.parent]: data_home = os.environ.get("XDG_DATA_HOME", Path.home() / ".local" / "share")
p = base / "playlists.db" p = Path(data_home) / "playlists" / "playlists.db"
if p.exists(): if p.exists():
return str(p) return str(p)
print("Could not find playlists.db", file=sys.stderr) print(f"Could not find {p}", file=sys.stderr)
sys.exit(1) sys.exit(1)

View File

@@ -2,7 +2,7 @@ use rusqlite::Connection;
use crate::lastfm::{SimilarArtist, TopTrack}; use crate::lastfm::{SimilarArtist, TopTrack};
pub fn open(path: &str) -> Result<Connection, rusqlite::Error> { pub fn open(path: &std::path::Path) -> Result<Connection, rusqlite::Error> {
let conn = Connection::open(path)?; let conn = Connection::open(path)?;
conn.execute_batch( conn.execute_batch(
"CREATE TABLE IF NOT EXISTS artists ( "CREATE TABLE IF NOT EXISTS artists (

View File

@@ -7,11 +7,23 @@ mod tui;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::path::Path; use std::path::{Path, PathBuf};
use rand::distr::weighted::WeightedIndex; use rand::distr::weighted::WeightedIndex;
use rand::prelude::*; use rand::prelude::*;
fn db_path() -> PathBuf {
let data_dir = env::var("XDG_DATA_HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| {
let home = env::var("HOME").expect("HOME not set");
PathBuf::from(home).join(".local/share")
});
let dir = data_dir.join("playlists");
std::fs::create_dir_all(&dir).expect("failed to create data directory");
dir.join("playlists.db")
}
fn usage(program: &str) -> ! { fn usage(program: &str) -> ! {
eprintln!("Usage:"); eprintln!("Usage:");
eprintln!(" {program} index [-v] <directory>"); eprintln!(" {program} index [-v] <directory>");
@@ -50,7 +62,7 @@ fn cmd_index(args: &[String]) {
std::process::exit(1); std::process::exit(1);
} }
let conn = db::open("playlists.db").expect("failed to open database"); let conn = db::open(&db_path()).expect("failed to open database");
let lastfm = lastfm::LastfmClient::new(api_key); let lastfm = lastfm::LastfmClient::new(api_key);
let dir = Path::new(rest[0].as_str()); let dir = Path::new(rest[0].as_str());
@@ -166,7 +178,7 @@ fn cmd_build(args: &[String]) {
} }
dotenvy::dotenv().ok(); dotenvy::dotenv().ok();
let conn = db::open("playlists.db").expect("failed to open database"); let conn = db::open(&db_path()).expect("failed to open database");
let (artist_mbid, seed_name) = if let Some(file_arg) = rest.first() { let (artist_mbid, seed_name) = if let Some(file_arg) = rest.first() {
let path = Path::new(file_arg.as_str()); let path = Path::new(file_arg.as_str());