Initial commit: LastRecc discovery feed and weekly engine
This commit is contained in:
40 files changed
+6972
No files matched your search
@@ -0,0 +1,231 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultDays = 7
|
||||
defaultSeedArtists = 10
|
||||
defaultSeedTracks = 10
|
||||
defaultTracksPerAlbum = 3
|
||||
defaultPlaylistMax = 60
|
||||
defaultRepeatWeeks = 4
|
||||
defaultScanTimeout = 30 * time.Minute
|
||||
defaultSettle = 5 * time.Second
|
||||
defaultSearchDelay = 800 * time.Millisecond
|
||||
defaultPlaylistName = "LastRecc Weekly"
|
||||
defaultScan = true
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
EnvPath string
|
||||
|
||||
NavidromeURL string
|
||||
NavidromeUser string
|
||||
NavidromePassword string
|
||||
BasicUser string
|
||||
BasicPassword string
|
||||
ScanUser string
|
||||
ScanPassword string
|
||||
|
||||
LastFMKey string
|
||||
|
||||
Days int
|
||||
SeedArtists int
|
||||
SeedTracks int
|
||||
MaxArtists int
|
||||
TracksPerAlbum int
|
||||
PlaylistMaxTracks int
|
||||
PlaylistName string
|
||||
RepeatWeeks int
|
||||
Scan bool
|
||||
ScanTimeout time.Duration
|
||||
Settle time.Duration
|
||||
SearchDelay time.Duration
|
||||
RipBinary string
|
||||
RipConfig string
|
||||
Quality int
|
||||
StateDir string
|
||||
Verbose bool
|
||||
DryRun bool
|
||||
}
|
||||
|
||||
func Load(envPath string, getenv func(string) string) (*Config, error) {
|
||||
path := findEnv(envPath)
|
||||
if path != "" {
|
||||
if err := loadDotEnv(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
cfg := &Config{
|
||||
EnvPath: path,
|
||||
NavidromeURL: strings.TrimRight(getenv("NAVIDROME_URL"), "/"),
|
||||
NavidromeUser: getenv("NAVIDROME_USER"),
|
||||
NavidromePassword: getenv("NAVIDROME_PASSWORD"),
|
||||
BasicUser: getenv("NAVIDROME_BASIC_USER"),
|
||||
BasicPassword: getenv("NAVIDROME_BASIC_PASSWORD"),
|
||||
ScanUser: getenv("SCAN_USER"),
|
||||
ScanPassword: getenv("SCAN_PASSWORD"),
|
||||
LastFMKey: getenv("LASTFM_API_KEY"),
|
||||
Days: envInt(getenv, "DAYS", defaultDays),
|
||||
SeedArtists: envInt(getenv, "SEED_ARTISTS", defaultSeedArtists),
|
||||
SeedTracks: envInt(getenv, "SEED_TRACKS", defaultSeedTracks),
|
||||
MaxArtists: envInt(getenv, "MAX_ARTISTS", 30),
|
||||
TracksPerAlbum: envInt(getenv, "TRACKS_PER_ALBUM", defaultTracksPerAlbum),
|
||||
PlaylistMaxTracks: envInt(getenv, "PLAYLIST_MAX_TRACKS", defaultPlaylistMax),
|
||||
PlaylistName: envString(getenv, "PLAYLIST_NAME", defaultPlaylistName),
|
||||
RepeatWeeks: envInt(getenv, "REPEAT_WINDOW_WEEKS", defaultRepeatWeeks),
|
||||
Scan: envBool(getenv, "SCAN", defaultScan),
|
||||
ScanTimeout: time.Duration(envInt(getenv, "SCAN_TIMEOUT_SECONDS", int(defaultScanTimeout.Seconds()))) * time.Second,
|
||||
Settle: time.Duration(envInt(getenv, "SCAN_SETTLE_SECONDS", int(defaultSettle.Seconds()))) * time.Second,
|
||||
SearchDelay: time.Duration(envInt(getenv, "SEARCH_DELAY_MS", int(defaultSearchDelay.Milliseconds()))) * time.Millisecond,
|
||||
RipBinary: envString(getenv, "RIP_BINARY", defaultRipBinary()),
|
||||
RipConfig: getenv("RIP_CONFIG"),
|
||||
Quality: envInt(getenv, "QUALITY", -1),
|
||||
StateDir: envString(getenv, "STATE_DIR", "state"),
|
||||
}
|
||||
if cfg.ScanUser == "" {
|
||||
cfg.ScanUser = cfg.NavidromeUser
|
||||
cfg.ScanPassword = cfg.NavidromePassword
|
||||
}
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (c *Config) Validate() error {
|
||||
var missing []string
|
||||
if c.NavidromeURL == "" {
|
||||
missing = append(missing, "NAVIDROME_URL")
|
||||
}
|
||||
if c.NavidromeUser == "" {
|
||||
missing = append(missing, "NAVIDROME_USER")
|
||||
}
|
||||
if c.NavidromePassword == "" {
|
||||
missing = append(missing, "NAVIDROME_PASSWORD")
|
||||
}
|
||||
if c.LastFMKey == "" {
|
||||
missing = append(missing, "LASTFM_API_KEY")
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return fmt.Errorf("missing %s (looked at %s)", strings.Join(missing, ", "), c.EnvPath)
|
||||
}
|
||||
if c.Days < 1 || c.Days > 90 {
|
||||
return fmt.Errorf("DAYS must be between 1 and 90")
|
||||
}
|
||||
if c.TracksPerAlbum < 1 {
|
||||
c.TracksPerAlbum = defaultTracksPerAlbum
|
||||
}
|
||||
if c.PlaylistMaxTracks < 0 {
|
||||
c.PlaylistMaxTracks = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func findEnv(explicit string) string {
|
||||
if explicit != "" {
|
||||
return explicit
|
||||
}
|
||||
for _, candidate := range []string{".env", "../.env", "../../.env"} {
|
||||
if fileExists(candidate) {
|
||||
abs, _ := filepath.Abs(candidate)
|
||||
return abs
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func loadDotEnv(path string) error {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
line = strings.TrimPrefix(line, "export ")
|
||||
key, value, ok := strings.Cut(line, "=")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
key = strings.TrimSpace(key)
|
||||
value = strings.TrimSpace(value)
|
||||
if len(value) >= 2 && ((value[0] == '"' && value[len(value)-1] == '"') || (value[0] == '\'' && value[len(value)-1] == '\'')) {
|
||||
value = value[1 : len(value)-1]
|
||||
}
|
||||
if _, exists := os.LookupEnv(key); !exists {
|
||||
_ = os.Setenv(key, value)
|
||||
}
|
||||
}
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
func defaultRipBinary() string {
|
||||
// Prefer a bundled Go `rip` next to this binary, then the local clone.
|
||||
// A Python streamrip `rip` may also be on PATH and has a different
|
||||
// CLI/config format.
|
||||
if exe, err := os.Executable(); err == nil {
|
||||
sibling := filepath.Join(filepath.Dir(exe), "rip")
|
||||
if fileExists(sibling) {
|
||||
return sibling
|
||||
}
|
||||
}
|
||||
home, _ := os.UserHomeDir()
|
||||
fallback := filepath.Join(home, "cloned", "streamrip-go", "rip")
|
||||
if fileExists(fallback) {
|
||||
return fallback
|
||||
}
|
||||
if p, err := exec.LookPath("rip"); err == nil {
|
||||
return p
|
||||
}
|
||||
return "rip"
|
||||
}
|
||||
|
||||
func fileExists(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
return err == nil && !info.IsDir()
|
||||
}
|
||||
|
||||
func envString(getenv func(string) string, key, fallback string) string {
|
||||
if value := strings.TrimSpace(getenv(key)); value != "" {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func envInt(getenv func(string) string, key string, fallback int) int {
|
||||
value := strings.TrimSpace(getenv(key))
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
func envBool(getenv func(string) string, key string, fallback bool) bool {
|
||||
value := strings.ToLower(strings.TrimSpace(getenv(key)))
|
||||
switch value {
|
||||
case "1", "true", "yes", "on":
|
||||
return true
|
||||
case "0", "false", "no", "off":
|
||||
return false
|
||||
default:
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user