Move refresh config and callers to ListeningStats

This commit is contained in:
Claudio Ortolina
2026-03-27 23:14:09 +00:00
parent fa5e13f303
commit 61dd3f45c7
12 changed files with 23 additions and 40 deletions
+1 -7
View File
@@ -3,15 +3,9 @@ defmodule LastFm do
Last.fm API facade for scrobbling and listening history.
"""
alias LastFm.{API, Feed, Refresh, Scrobble, Track, Worker}
alias LastFm.{API, Scrobble, Track, Worker}
alias MusicLibrary.{BackgroundRepo, Repo}
@spec subscribe_to_feed() :: :ok
def subscribe_to_feed, do: Feed.subscribe()
@spec refresh_scrobbled_tracks() :: :ok | {:error, term()}
def refresh_scrobbled_tracks, do: Refresh.refresh()
@spec get_tracks(keyword()) :: {:ok, [Track.t()]} | {:error, term()}
def get_tracks(opts) do
last_fm_config = last_fm_config()
-14
View File
@@ -3,8 +3,6 @@ defmodule LastFm.Config do
api_key: String.t(),
shared_secret: String.t(),
user: String.t(),
auto_refresh: boolean(),
refresh_interval: pos_integer(),
user_agent: String.t(),
req_options: Keyword.t(),
api_cooldown: non_neg_integer()
@@ -14,8 +12,6 @@ defmodule LastFm.Config do
defstruct api_key: "",
shared_secret: "",
user: "",
auto_refresh: true,
refresh_interval: 60_000,
user_agent: "change me",
req_options: [],
api_cooldown: 500
@@ -33,16 +29,6 @@ defmodule LastFm.Config do
type: :string,
required: true
],
auto_refresh: [
type: :boolean,
required: false,
default: true
],
refresh_interval: [
type: :pos_integer,
required: false,
default: 60_000
],
user_agent: [
type: :string,
required: false,
+1 -1
View File
@@ -4,7 +4,7 @@ defmodule LastFm.Import do
@spec batch(keyword()) :: {:ok, non_neg_integer()} | {:error, term()}
def batch(opts) do
with {:ok, tracks} <- LastFm.get_tracks(opts) do
LastFm.Feed.update(tracks)
MusicLibrary.ListeningStats.update(tracks)
end
end
end
+3 -1
View File
@@ -24,7 +24,9 @@ defmodule MusicLibrary.Application do
repos: Application.fetch_env!(:music_library, :ecto_repos), skip: skip_migrations?()},
{Task.Supervisor, name: MusicLibrary.TaskSupervisor},
{Phoenix.PubSub, name: MusicLibrary.PubSub},
{LastFm.Supervisor, LastFm.Config.resolve(:music_library)},
{MusicLibrary.ListeningStats.Refresh,
{LastFm.Config.resolve(:music_library),
Application.fetch_env!(:music_library, MusicLibrary.ListeningStats.Refresh)}},
# Start a worker by calling: MusicLibrary.Worker.start_link(arg)
# {MusicLibrary.Worker, arg},
# Start to serve requests, typically the last entry
@@ -222,7 +222,7 @@ defmodule MusicLibraryWeb.ScrobbledTracksLive.Index do
)
if connected?(socket) do
LastFm.subscribe_to_feed()
ListeningStats.subscribe()
end
{:ok, socket}
@@ -288,7 +288,7 @@ defmodule MusicLibraryWeb.ScrobbledTracksLive.Index do
end
def handle_event("refresh_lastfm_feed", _, socket) do
LastFm.refresh_scrobbled_tracks()
ListeningStats.refresh()
{:noreply, socket}
end
@@ -65,7 +65,7 @@ defmodule MusicLibraryWeb.StatsLive.Index do
|> Collection.group_records_by_release_group()
if connected?(socket) do
LastFm.subscribe_to_feed()
ListeningStats.subscribe()
end
{:ok,
@@ -93,7 +93,7 @@ defmodule MusicLibraryWeb.StatsLive.Index do
@impl true
def handle_event("refresh_lastfm_feed", _, socket) do
LastFm.refresh_scrobbled_tracks()
ListeningStats.refresh()
{:noreply, socket}
end