|
1 | 1 | defmodule Moos2.Spotify do |
2 | | - use GenServer |
| 2 | + use Agent |
3 | 3 |
|
4 | 4 | alias Spotify |
5 | 5 |
|
6 | | - def init(args \\ %{}) do |
7 | | - {:ok, args} |
| 6 | + defp open_browser(nil), do: {[], 0} |
| 7 | + |
| 8 | + defp open_browser(path) do |
| 9 | + case :os.type() do |
| 10 | + {:unix, :darwin} -> System.cmd("open", [path]) |
| 11 | + _ -> open_browser(nil) |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + @default_state %{status: :unauthenticated} |
| 16 | + |
| 17 | + def start_link(_ \\ []) do |
| 18 | + Agent.start_link(fn -> @default_state end, name: __MODULE__) |
| 19 | + end |
| 20 | + |
| 21 | + def put(:credentials, %Spotify.Credentials{} = credentials) do |
| 22 | + Agent.update(__MODULE__, &Map.put(&1, :credentials, credentials)) |
8 | 23 | end |
9 | 24 |
|
10 | | - def start_link(options) do |
11 | | - GenServer.start_link(__MODULE__, %Spotify.Credentials{}, options) |
| 25 | + def get(key) do |
| 26 | + Agent.get(__MODULE__, fn state -> state[key] end) |
12 | 27 | end |
13 | 28 |
|
14 | | - defp get_creds, do: Agent.get(__MODULE__, & &1) |
| 29 | + def credentials, do: get(:credentials) |
15 | 30 |
|
16 | 31 | def authenticate(%Plug.Conn{} = conn, params) do |
17 | 32 | case Spotify.Authentication.authenticate(conn, params) do |
18 | | - {:ok, creds} -> |
19 | | - Agent.update(__MODULE__, fn _ -> creds end) |
20 | | - |
21 | | - {:error, _} -> |
22 | | - :error |
| 33 | + {:ok, credentials} -> put(:credentials, Spotify.Credentials.new(credentials)) |
| 34 | + {:error, _} -> :error |
23 | 35 | end |
24 | 36 | end |
25 | 37 |
|
26 | | - def authenticated? do |
27 | | - if get_creds(), do: true, else: false |
| 38 | + def authentication_loop do |
| 39 | + case get(:credentials) do |
| 40 | + %Spotify.Credentials{} = credentials -> |
| 41 | + {:ok, credentials} |
| 42 | + |
| 43 | + _ -> |
| 44 | + :timer.sleep(150) |
| 45 | + authentication_loop() |
| 46 | + end |
28 | 47 | end |
29 | 48 |
|
30 | | - def sign_in do |
31 | | - System.cmd("open", [Spotify.Authorization.url()]) |
32 | | - :ok |
| 49 | + def sign_in() do |
| 50 | + Spotify.Authorization.url() |
| 51 | + |> open_browser() |
| 52 | + |
| 53 | + authorize = |
| 54 | + Task.async(fn -> |
| 55 | + authentication_loop() |
| 56 | + end) |
| 57 | + |
| 58 | + Task.await(authorize, 30000) |
33 | 59 | end |
34 | 60 |
|
35 | | - def search_for_track([artist: _artist, track: _track] = song_info) do |
36 | | - creds = get_creds() |
37 | | - q = format_query(song_info) |
| 61 | + def search_for_track({artist, track} = _song_info) do |
| 62 | + q = format_query(artist: artist, track: track) |
38 | 63 |
|
39 | 64 | result = |
40 | | - case Spotify.Search.query(creds, q: q, type: "track", limit: 1) do |
| 65 | + case Spotify.Search.query(credentials(), q: q, type: "track", limit: 1) do |
41 | 66 | {:ok, result} -> result |
42 | 67 | end |
43 | 68 |
|
44 | | - Map.get(result, :items, []) |> List.first() |
| 69 | + Map.get(result, :items, []) |> List.first() |> get_track_uri() |
| 70 | + end |
| 71 | + |
| 72 | + def create_playlist(name) do |
| 73 | + body = Poison.encode!(%{name: name}) |
| 74 | + Spotify.Playlist.create_playlist(credentials(), Spotify.current_user(), body) |
| 75 | + end |
| 76 | + |
| 77 | + def add_playlist_tracks(%Spotify.Playlist{id: id}, tracks) do |
| 78 | + uris = Poison.encode!(%{uris: tracks}) |
| 79 | + Spotify.Playlist.add_tracks(credentials(), Spotify.current_user(), id, uris, []) |
45 | 80 | end |
46 | 81 |
|
47 | | - def get_track_id(%Spotify.Track{id: id}), do: id |
48 | | - def get_track_id(_track), do: nil |
| 82 | + def get_track_uri(%Spotify.Track{uri: uri}), do: uri |
| 83 | + def get_track_uri(_track), do: nil |
49 | 84 |
|
50 | 85 | defp format_query([artist: _artist, track: _track] = song_info) do |
51 | 86 | Enum.map_join(song_info, fn {k, v} -> "#{k}:#{v} " end) |
|
0 commit comments