|
| 1 | +defmodule Pling.Playlists.MusicLibrary do |
| 2 | + @moduledoc """ |
| 3 | + Manages music playlists and track selection from the database. |
| 4 | + Playlists are organized by decades, with tracks associated to each playlist. |
| 5 | + """ |
| 6 | + import Ecto.Query |
| 7 | + require Logger |
| 8 | + alias Pling.Repo |
| 9 | + alias Pling.Playlists.{Playlist, Track} |
| 10 | + |
| 11 | + @doc """ |
| 12 | + Loads all playlists from the database and returns them as a map with spotify_id keys. |
| 13 | + """ |
| 14 | + def load_playlists do |
| 15 | + Playlist |
| 16 | + |> preload(:tracks) |
| 17 | + |> Repo.all() |
| 18 | + |> Enum.map(fn playlist -> {playlist.spotify_id, playlist} end) |
| 19 | + |> Enum.into(%{}) |
| 20 | + end |
| 21 | + |
| 22 | + @doc """ |
| 23 | + Gets all tracks from all playlists when playlist is "mix", |
| 24 | + otherwise gets tracks for the specified playlist. |
| 25 | + """ |
| 26 | + def get_tracks(_playlists, "mix") do |
| 27 | + Repo.all(Track) |
| 28 | + end |
| 29 | + |
| 30 | + def get_tracks(_playlists, playlist_id) when is_binary(playlist_id) do |
| 31 | + Track |
| 32 | + |> where([t], t.playlist_spotify_id == ^playlist_id) |
| 33 | + |> Repo.all() |
| 34 | + end |
| 35 | + |
| 36 | + @doc """ |
| 37 | + Selects a random track from the given list of tracks. |
| 38 | + """ |
| 39 | + def random_track([]), do: nil |
| 40 | + def random_track(tracks), do: Enum.random(tracks) |
| 41 | + |
| 42 | + @doc """ |
| 43 | + Selects a random track from the specified playlist. |
| 44 | + Returns nil if no tracks are found. |
| 45 | + """ |
| 46 | + def select_track(playlists, nil) when is_map(playlists) and map_size(playlists) > 0 do |
| 47 | + # If no playlist specified, pick a random one |
| 48 | + {playlist_id, _} = Enum.random(playlists) |
| 49 | + select_track(playlists, playlist_id) |
| 50 | + end |
| 51 | + |
| 52 | + def select_track(_playlists, nil), do: nil |
| 53 | + |
| 54 | + def select_track(playlists, playlist_id) do |
| 55 | + playlists |
| 56 | + |> get_tracks(playlist_id) |
| 57 | + |> random_track() |
| 58 | + end |
| 59 | + |
| 60 | + @doc """ |
| 61 | + Gets a playlist by Spotify ID, fetching it from Spotify if it doesn't exist locally. |
| 62 | + Returns the playlist as soon as it has basic info and at least one track. |
| 63 | + """ |
| 64 | + def get_or_fetch_playlist(spotify_id, timeout \\ 5000) do |
| 65 | + case Repo.get(Playlist, spotify_id) do |
| 66 | + nil -> |
| 67 | + ref = make_ref() |
| 68 | + parent = self() |
| 69 | + |
| 70 | + callback = fn |
| 71 | + %{"playlist_info" => info} -> |
| 72 | + playlist = %Playlist{ |
| 73 | + spotify_id: spotify_id, |
| 74 | + name: info["name"], |
| 75 | + owner: info["owner"], |
| 76 | + image_url: get_first_image_url(info["images"]), |
| 77 | + official: false |
| 78 | + } |
| 79 | + |
| 80 | + {:ok, saved_playlist} = Repo.insert(playlist) |
| 81 | + saved_playlist |
| 82 | + |
| 83 | + %{"track" => track} -> |
| 84 | + track_entry = %Track{ |
| 85 | + spotify_id: track["id"], |
| 86 | + title: track["name"], |
| 87 | + artists: Enum.map(track["artists"], & &1["name"]), |
| 88 | + uri: track["uri"], |
| 89 | + popularity: track["popularity"], |
| 90 | + album: track["album"]["name"], |
| 91 | + playlist_spotify_id: spotify_id |
| 92 | + } |
| 93 | + |
| 94 | + {:ok, saved_track} = Repo.insert(track_entry, on_conflict: :nothing) |
| 95 | + |
| 96 | + # Notify parent process of first track |
| 97 | + if not Process.get({:notified_first_track, ref}) do |
| 98 | + Process.put({:notified_first_track, ref}, true) |
| 99 | + send(parent, {:first_track_saved, ref, spotify_id}) |
| 100 | + end |
| 101 | + |
| 102 | + saved_track |
| 103 | + end |
| 104 | + |
| 105 | + Task.start(fn -> |
| 106 | + case Pling.Services.Spotify.stream_playlist(spotify_id, callback) do |
| 107 | + :ok -> |
| 108 | + :ok |
| 109 | + |
| 110 | + {:error, reason} -> |
| 111 | + Logger.error("Failed to fetch complete playlist: #{inspect(reason)}") |
| 112 | + # Notify parent in case of immediate error |
| 113 | + send(parent, {:playlist_error, ref, reason}) |
| 114 | + end |
| 115 | + end) |
| 116 | + |
| 117 | + receive do |
| 118 | + {:first_track_saved, ^ref, playlist_id} -> |
| 119 | + case Repo.get(Playlist, playlist_id) do |
| 120 | + nil -> {:error, :playlist_fetch_failed} |
| 121 | + playlist -> {:ok, :first_track_saved, playlist} |
| 122 | + end |
| 123 | + |
| 124 | + {:playlist_error, ^ref, reason} -> |
| 125 | + {:error, reason} |
| 126 | + after |
| 127 | + timeout -> |
| 128 | + case Repo.get(Playlist, spotify_id) do |
| 129 | + nil -> {:error, :playlist_fetch_timeout} |
| 130 | + playlist -> {:ok, :timeout, playlist} |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + playlist -> |
| 135 | + {:ok, :exists, playlist} |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + @doc """ |
| 140 | + Returns a callback that just outputs the streaming data. |
| 141 | + Useful for debugging or monitoring the stream. |
| 142 | + """ |
| 143 | + def debug_callback do |
| 144 | + fn |
| 145 | + %{"playlist_info" => info} -> |
| 146 | + IO.puts("Playlist Info:") |
| 147 | + IO.inspect(info, pretty: true) |
| 148 | + :ok |
| 149 | + |
| 150 | + %{"track" => track} -> |
| 151 | + IO.puts("\nTrack:") |
| 152 | + IO.inspect(track, pretty: true) |
| 153 | + :ok |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + defp get_first_image_url([%{"url" => url} | _]), do: url |
| 158 | + defp get_first_image_url(_), do: nil |
| 159 | +end |
0 commit comments