Documentation
¶
Overview ¶
Package cache manages favorites, blocklist, and offline cache storage
Index ¶
- func CalculateFLACBytesPerSecond(favoritesDir string) int64
- func DeleteCache(offlineDir, name string) error
- func EstimateDiskUsage(bitrate, targetSeconds int, flacBytesPerSec int64) map[string]int64
- func FormatBytes(b int64) string
- func FormatDuration(seconds int64) string
- func GenerateCacheName(station, bitrate int) string
- func GetFreeDiskSpace(path string) (int64, error)
- func ParseBitrate(s string) (int, error)
- func ParseDuration(s string) (int, error)
- type BlockFetcher
- type BlockInfo
- type CacheConfig
- type CacheEntry
- type CacheIndex
- type CacheManager
- func (c *CacheManager) AddBlocklist(song *models.Song, autoBlocked bool) error
- func (c *CacheManager) AddFavorite(song *models.Song, fileExt string) (string, error)
- func (c *CacheManager) EnsureDirectories() error
- func (c *CacheManager) GetBlocklist() ([]CachedSong, error)
- func (c *CacheManager) GetBlocklistCount() (int, error)
- func (c *CacheManager) GetFavoriteByEventID(eventID int64) (*CachedSong, error)
- func (c *CacheManager) GetFavoriteCount() (int, error)
- func (c *CacheManager) GetFavorites() ([]CachedSong, error)
- func (c *CacheManager) GetFavoritesDiskSpace() string
- func (c *CacheManager) GetOfflineDir() string
- func (c *CacheManager) IsBlocked(song *models.Song) bool
- func (c *CacheManager) IsFavorite(song *models.Song) bool
- func (c *CacheManager) RemoveBlocklist(songID int64) error
- func (c *CacheManager) RemoveFavorite(eventID int64) error
- func (c *CacheManager) RemoveFavoriteBySong(song *models.Song) error
- func (c *CacheManager) SetOfflineDir(dir string)
- func (c *CacheManager) StartFavoriteDownload(song *models.Song, fileExt string, onDone func(success bool))
- func (c *CacheManager) SyncAutoBlocklist(songIDs map[int64]string) error
- func (c *CacheManager) ToggleBlocklist(song *models.Song) (bool, error)
- func (c *CacheManager) ToggleFavorite(song *models.Song, fileExt string) (bool, string, error)
- func (c *CacheManager) WaitForDownloads(timeout time.Duration)
- type CacheRecorder
- type CachedSong
- type ProgressInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateFLACBytesPerSecond ¶
CalculateFLACBytesPerSecond calculates average FLAC bytes/second from favorites
func DeleteCache ¶
DeleteCache removes a cache by name
func EstimateDiskUsage ¶
EstimateDiskUsage estimates disk usage for cache recording
func FormatBytes ¶
FormatBytes formats bytes into human-readable string
func FormatDuration ¶
FormatDuration formats seconds to human-readable string
func GenerateCacheName ¶
GenerateCacheName creates an auto-generated cache name
func GetFreeDiskSpace ¶
GetFreeDiskSpace returns free disk space in bytes for the given path. Returns 0 if the platform doesn't support querying disk space.
func ParseBitrate ¶
ParseBitrate parses bitrate string like "320", "flac" to int
func ParseDuration ¶
ParseDuration parses duration string like "2h", "3.5h" to seconds
Types ¶
type BlockFetcher ¶
BlockFetcher is a function that fetches a block of songs from RP API
type BlockInfo ¶
type BlockInfo struct {
BlockID string
Songs []CachedSong
}
BlockInfo holds metadata about a fetched block
type CacheConfig ¶
type CacheConfig struct {
Station int `json:"station"`
Bitrate int `json:"bitrate"`
TargetSeconds int `json:"target_seconds"`
CreatedAt time.Time `json:"created_at"`
}
CacheConfig stores cache recording configuration
type CacheEntry ¶
type CacheEntry struct {
Name string `json:"name"`
Station int `json:"station"`
Bitrate int `json:"bitrate"`
TargetSeconds int `json:"target_seconds"`
ActualSeconds int `json:"actual_seconds"`
SongCount int `json:"song_count"`
SizeBytes int64 `json:"size_bytes"`
CreatedAt time.Time `json:"created_at"`
}
CacheEntry represents a completed offline cache
func ListCaches ¶
func ListCaches(offlineDir string) ([]CacheEntry, error)
ListCaches returns all available caches
type CacheIndex ¶
type CacheIndex struct {
Caches []CacheEntry `json:"caches"`
}
CacheIndex holds all cache entries
func LoadCacheIndex ¶
func LoadCacheIndex(offlineDir string) (*CacheIndex, error)
LoadCacheIndex loads the cache index from file
func (*CacheIndex) Save ¶
func (ci *CacheIndex) Save(offlineDir string) error
Save writes the cache index to file
type CacheManager ¶
type CacheManager struct {
// contains filtered or unexported fields
}
CacheManager manages favorites, blocklist, and offline caches
func NewCacheManager ¶
func NewCacheManager(favoritesDir, blocklistDir string, maxFavorites int) *CacheManager
NewCacheManager creates a new cache manager
func (*CacheManager) AddBlocklist ¶
func (c *CacheManager) AddBlocklist(song *models.Song, autoBlocked bool) error
AddBlocklist adds a song to blocklist
func (*CacheManager) AddFavorite ¶
AddFavorite adds a song to favorites and triggers audio download
func (*CacheManager) EnsureDirectories ¶
func (c *CacheManager) EnsureDirectories() error
EnsureDirectories creates the cache directories and loads metadata
func (*CacheManager) GetBlocklist ¶
func (c *CacheManager) GetBlocklist() ([]CachedSong, error)
GetBlocklist returns all blocklisted songs (newest first)
func (*CacheManager) GetBlocklistCount ¶
func (c *CacheManager) GetBlocklistCount() (int, error)
GetBlocklistCount returns the number of blocklisted songs
func (*CacheManager) GetFavoriteByEventID ¶
func (c *CacheManager) GetFavoriteByEventID(eventID int64) (*CachedSong, error)
GetFavoriteByEventID returns a favorite song by event ID
func (*CacheManager) GetFavoriteCount ¶
func (c *CacheManager) GetFavoriteCount() (int, error)
GetFavoriteCount returns the number of favorites
func (*CacheManager) GetFavorites ¶
func (c *CacheManager) GetFavorites() ([]CachedSong, error)
GetFavorites returns all favorite songs (newest first)
func (*CacheManager) GetFavoritesDiskSpace ¶
func (c *CacheManager) GetFavoritesDiskSpace() string
GetFavoritesDiskSpace calculates total disk space used by favorites
func (*CacheManager) GetOfflineDir ¶
func (c *CacheManager) GetOfflineDir() string
GetOfflineDir returns the offline cache directory
func (*CacheManager) IsBlocked ¶
func (c *CacheManager) IsBlocked(song *models.Song) bool
IsBlocked checks if a song is in blocklist by SongID
func (*CacheManager) IsFavorite ¶
func (c *CacheManager) IsFavorite(song *models.Song) bool
IsFavorite checks if a song is in favorites
func (*CacheManager) RemoveBlocklist ¶
func (c *CacheManager) RemoveBlocklist(songID int64) error
RemoveBlocklist removes a song from blocklist by SongID
func (*CacheManager) RemoveFavorite ¶
func (c *CacheManager) RemoveFavorite(eventID int64) error
RemoveFavorite removes a song from favorites by event ID
func (*CacheManager) RemoveFavoriteBySong ¶ added in v1.2.0
func (c *CacheManager) RemoveFavoriteBySong(song *models.Song) error
RemoveFavoriteBySong removes a song from favorites using stable identity
func (*CacheManager) SetOfflineDir ¶
func (c *CacheManager) SetOfflineDir(dir string)
SetOfflineDir sets the offline cache directory
func (*CacheManager) StartFavoriteDownload ¶
func (c *CacheManager) StartFavoriteDownload(song *models.Song, fileExt string, onDone func(success bool))
StartFavoriteDownload downloads the audio for a favorite and adds it to metadata.
func (*CacheManager) SyncAutoBlocklist ¶
func (c *CacheManager) SyncAutoBlocklist(songIDs map[int64]string) error
SyncAutoBlocklist synchronizes the auto-blocklist with the fetched RP low-rated songs. It removes stale AutoBlocked=true entries whose SongID is not in the new list, and adds new entries for songs that aren't already in the blocklist. songIDs are the RP song IDs that should be auto-blocked.
func (*CacheManager) ToggleBlocklist ¶
func (c *CacheManager) ToggleBlocklist(song *models.Song) (bool, error)
ToggleBlocklist adds or removes a song from blocklist (manual, not auto)
func (*CacheManager) ToggleFavorite ¶
ToggleFavorite checks if a song is already a favorite.
func (*CacheManager) WaitForDownloads ¶
func (c *CacheManager) WaitForDownloads(timeout time.Duration)
WaitForDownloads waits for all in-progress downloads to complete, up to a timeout.
type CacheRecorder ¶
type CacheRecorder struct {
// contains filtered or unexported fields
}
CacheRecorder handles recording a cache session
func NewCacheRecorder ¶
func NewCacheRecorder(offlineDir, name string, station, bitrate, targetSeconds int) *CacheRecorder
NewCacheRecorder creates a new cache recorder
func (*CacheRecorder) Cleanup ¶
func (r *CacheRecorder) Cleanup() error
Cleanup removes the entire cache directory (used on interrupt)
func (*CacheRecorder) Record ¶
func (r *CacheRecorder) Record(blockFetcher BlockFetcher, progressFn func(ProgressInfo)) error
Record downloads songs until target duration is reached. It fetches a block, downloads all songs, then sleeps until ~120s before the last song's scheduled end time. Then it polls every 5s for a new block. Returns error or nil on success.
func (*CacheRecorder) Setup ¶
func (r *CacheRecorder) Setup() error
Setup creates the cache directory structure and config
type CachedSong ¶
type CachedSong struct {
EventID int64 `json:"event_id"`
SongID int64 `json:"song_id,omitempty"`
Title string `json:"title"`
Artist string `json:"artist"`
Album string `json:"album"`
Year string `json:"year"`
Duration int64 `json:"duration"`
GaplessURL string `json:"gapless_url"`
CoverLarge string `json:"cover_large"`
Rating string `json:"rating"`
ListenerRating string `json:"listener_rating"`
AudioPath string `json:"audio_path,omitempty"`
SchedTimeMillis int64 `json:"sched_time_millis,omitempty"`
AddedAt int64 `json:"added_at"`
AutoBlocked bool `json:"auto_blocked,omitempty"`
}
CachedSong represents a song stored in cache
func LoadCache ¶
func LoadCache(offlineDir, name string) ([]CachedSong, error)
LoadCache loads a cache's songs for offline playback
func (*CachedSong) ToSong ¶
func (cs *CachedSong) ToSong() *models.Song
ToSong converts a CachedSong to a models.Song
func (*CachedSong) UnmarshalJSON ¶
func (cs *CachedSong) UnmarshalJSON(data []byte) error
UnmarshalJSON implements custom JSON unmarshaling to handle string event_id