diff --git a/config/config.exs b/config/config.exs index e6fe950e..8ea98577 100644 --- a/config/config.exs +++ b/config/config.exs @@ -53,14 +53,16 @@ user_agent = "MusicLibrary/0.1.0 ( cloud8421@gmail.com )" config :music_library, LastFm, user: "username", - auto_refresh: true, - # refresh every 5 minutes - refresh_interval: System.convert_time_unit(300, :second, :millisecond), api_key: "change me", shared_secret: "change me", user_agent: user_agent, api_cooldown: 500 +config :music_library, MusicLibrary.ListeningStats.Refresh, + auto_refresh: true, + # refresh every 5 minutes + refresh_interval: System.convert_time_unit(300, :second, :millisecond) + config :music_library, MusicBrainz, user_agent: user_agent, api_cooldown: 500 config :music_library, Discogs, diff --git a/config/dev.exs b/config/dev.exs index 6c752446..1a6ca95a 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -81,7 +81,7 @@ config :music_library, monitoring_routes: true config :music_library, MusicLibrary.Mailer, adapter: Swoosh.Adapters.Local -config :music_library, LastFm, +config :music_library, MusicLibrary.ListeningStats.Refresh, refresh_interval: System.convert_time_unit(500, :second, :millisecond) # Do not include metadata nor timestamps in development logs diff --git a/config/test.exs b/config/test.exs index 641b1ed8..5ff7d4b8 100644 --- a/config/test.exs +++ b/config/test.exs @@ -56,13 +56,14 @@ config :phoenix_live_view, config :music_library, monitoring_routes: true config :music_library, LastFm, - auto_refresh: false, req_options: [ plug: {Req.Test, LastFm.API}, max_retries: 0 ], api_cooldown: 0 +config :music_library, MusicLibrary.ListeningStats.Refresh, auto_refresh: false + config :music_library, MusicBrainz, req_options: [ plug: {Req.Test, MusicBrainz.API}, diff --git a/lib/last_fm.ex b/lib/last_fm.ex index 7c263cc7..b4d8f01c 100644 --- a/lib/last_fm.ex +++ b/lib/last_fm.ex @@ -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() diff --git a/lib/last_fm/config.ex b/lib/last_fm/config.ex index 870ce38b..cde83344 100644 --- a/lib/last_fm/config.ex +++ b/lib/last_fm/config.ex @@ -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, diff --git a/lib/last_fm/import.ex b/lib/last_fm/import.ex index fe59868b..33488323 100644 --- a/lib/last_fm/import.ex +++ b/lib/last_fm/import.ex @@ -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 diff --git a/lib/music_library/application.ex b/lib/music_library/application.ex index 0bfe4485..823af511 100644 --- a/lib/music_library/application.ex +++ b/lib/music_library/application.ex @@ -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 diff --git a/lib/music_library_web/live/scrobbled_tracks_live/index.ex b/lib/music_library_web/live/scrobbled_tracks_live/index.ex index a99ffeb2..ce930e63 100644 --- a/lib/music_library_web/live/scrobbled_tracks_live/index.ex +++ b/lib/music_library_web/live/scrobbled_tracks_live/index.ex @@ -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 diff --git a/lib/music_library_web/live/stats_live/index.ex b/lib/music_library_web/live/stats_live/index.ex index bdab3621..27898318 100644 --- a/lib/music_library_web/live/stats_live/index.ex +++ b/lib/music_library_web/live/stats_live/index.ex @@ -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 diff --git a/test/last_fm/config_test.exs b/test/last_fm/config_test.exs index 1b5b6844..6a53852b 100644 --- a/test/last_fm/config_test.exs +++ b/test/last_fm/config_test.exs @@ -6,8 +6,6 @@ defmodule LastFm.ConfigTest do assert %LastFm.Config{ api_key: api_key, user: user, - auto_refresh: false, - refresh_interval: refresh_interval, user_agent: user_agent } = LastFm.Config.resolve(:music_library) @@ -15,7 +13,6 @@ defmodule LastFm.ConfigTest do assert is_binary(api_key) assert is_binary(user) assert is_binary(user_agent) - assert is_integer(refresh_interval) end end end diff --git a/test/last_fm/feed_test.exs b/test/last_fm/feed_test.exs index d209498e..85a47e5c 100644 --- a/test/last_fm/feed_test.exs +++ b/test/last_fm/feed_test.exs @@ -1,7 +1,8 @@ defmodule LastFm.FeedTest do use MusicLibrary.DataCase - alias LastFm.{Album, Artist, Feed, Track} + alias LastFm.{Album, Artist, Track} + alias MusicLibrary.ListeningStats @track_one %Track{ musicbrainz_id: "5689211e-9afa-3c3e-8e34-63dc0de45ef1", @@ -38,13 +39,13 @@ defmodule LastFm.FeedTest do describe "update and broadcast" do test "stores the track and broadcasts the updated track count" do - :ok = Feed.subscribe() + :ok = ListeningStats.subscribe() - assert {:ok, 2} == Feed.update([@track_two, @track_one]) + assert {:ok, 2} == ListeningStats.update([@track_two, @track_one]) assert_receive %{track_count: 2} # Tracks have already been inserted, count of new tracks is 0 - assert {:ok, 0} == Feed.update([@track_two, @track_one]) + assert {:ok, 0} == ListeningStats.update([@track_two, @track_one]) assert_receive %{track_count: 0} end end diff --git a/test/music_library_web/live/stats_live/index_test.exs b/test/music_library_web/live/stats_live/index_test.exs index c0c3a1d6..b372554f 100644 --- a/test/music_library_web/live/stats_live/index_test.exs +++ b/test/music_library_web/live/stats_live/index_test.exs @@ -225,7 +225,7 @@ defmodule MusicLibraryWeb.StatsLive.IndexTest do last_fm_data: %{} } - LastFm.Feed.update([ + MusicLibrary.ListeningStats.update([ machinarium_soundtrack_track, the_last_flight_track, the_mystery_of_time_track,