Skip to content

Commit c55bedb

Browse files
committed
escript complete oh my godgaa
1 parent e3201e2 commit c55bedb

File tree

12 files changed

+129
-45
lines changed

12 files changed

+129
-45
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import Config
22

3-
config :moos, []
43
import_config("spotify.exs")
4+
import_config("spotify.secret.exs")
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import Config
22

33
config :spotify_ex,
4-
user_id: "ughitsaaron",
4+
callback_url: "http://localhost:4000/auth/callback",
55
scopes: [
66
"user-read-private",
77
"user-read-email",
88
"playlist-modify-private",
99
"playlist-modify-public"
10-
],
11-
callback_url: "http://localhost:4000/auth/callback"
10+
]

config/spotify.secret.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Config
2+
3+
config :spotify_ex,
4+
client_id: "d5e3977ccb2a4f919c90b91932116a72",
5+
secret_key: "01a31e1f4f5c42a5ae9ede257b240bdf",
6+
user_id: "ughitsaaron"

config/spotify.secret.sample.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Config
2+
3+
config :spotify_ex,
4+
client_id: "<CLIENT_ID>",
5+
secret_key: "<SECRET_KEY>",
6+
user_id: "<USERNAME>"

lib/helpers.ex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule Moos2.Helpers do
2+
alias Plug.Cowboy
3+
alias Moos2.Router
4+
5+
def start_router do
6+
port = Router.port()
7+
Cowboy.http(Moos2.Router, [], port: port)
8+
end
9+
10+
def shutdown_router do
11+
Cowboy.shutdown(Moos2.Router.HTTP)
12+
end
13+
14+
def restart_router do
15+
case shutdown_router() do
16+
_ -> start_router()
17+
end
18+
end
19+
end

lib/moos2.ex

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
defmodule Moos2.Main do
2-
@base_url "https://wfmu.org/"
2+
def main(_args) do
3+
children = [
4+
{Plug.Cowboy, scheme: :http, plug: Moos2.Router, options: [port: 4000]},
5+
{Moos2.Spotify, []}
6+
]
37

4-
def main(args) do
5-
children = [{Plug.Cowboy, scheme: :http, plug: Moos2.Router, options: [port: 4000]}]
6-
IO.inspect(args)
7-
[url] = args
8+
Supervisor.start_link(children, strategy: :one_for_one, name: Moos2.Supervisor)
89

9-
playlist =
10-
case String.starts_with?(url, @base_url) do
11-
true -> Moos2.Scrape.get_playlist(url)
12-
false -> {:error, "Error during auth"}
13-
end
10+
[path, name] = System.argv()
11+
{:ok, _credentials} = Moos2.Spotify.sign_in()
1412

15-
IO.inspect(playlist)
13+
track_info = Moos2.Scrape.get_playlist(path)
1614

17-
Supervisor.start_link(children, strategy: :one_for_one, name: Moos2.Supervisor)
15+
IO.inspect(track_info)
16+
17+
tracks = Enum.map(track_info, &Moos2.Spotify.search_for_track/1) |> Enum.reject(&is_nil/1)
18+
19+
IO.inspect(tracks)
20+
21+
{:ok, playlist} = Moos2.Spotify.create_playlist(name)
22+
{:ok, _result} = Moos2.Spotify.add_playlist_tracks(playlist, tracks)
23+
24+
IO.inspect("okay uplaoded yay!")
1825
end
1926

2027
@dialyzer {:no_return, stop: 0}

lib/router.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ defmodule Moos2.Router do
1111
plug(:match)
1212
plug(:dispatch)
1313

14+
@port 4000
15+
1416
@response_body """
1517
<html><script>(() => window.close())();</script></html>
1618
"""
@@ -20,8 +22,18 @@ defmodule Moos2.Router do
2022

2123
get "/auth/callback" do
2224
case Moos2.Spotify.authenticate(conn, conn.params) do
23-
:ok -> send(:ok, 200)
24-
_ -> Moos2.Error.error(1)
25+
:ok -> redirect(conn, "/success")
26+
_ -> redirect(conn, Moos2.Error.error(1))
2527
end
2628
end
29+
30+
match(_, do: send_resp(conn, 404, Jason.encode!(nil)))
31+
32+
defp redirect(conn, url) do
33+
conn
34+
|> put_resp_header("location", url)
35+
|> send_resp(conn.status || 302, "Redirecting")
36+
end
37+
38+
def port, do: @port
2739
end

lib/scrape.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Moos2.Scrape do
22
@type playlist :: {title :: String.t(), href :: String.t()}
33
def get_playlist(path) do
4-
response = Req.get!(path)
4+
response = HTTPoison.get!(path)
55
{:ok, document} = Floki.parse_document(response.body)
66

77
Floki.find(document, "#songs table tr")

lib/spotify.ex

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,86 @@
11
defmodule Moos2.Spotify do
2-
use GenServer
2+
use Agent
33

44
alias Spotify
55

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))
823
end
924

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)
1227
end
1328

14-
defp get_creds, do: Agent.get(__MODULE__, & &1)
29+
def credentials, do: get(:credentials)
1530

1631
def authenticate(%Plug.Conn{} = conn, params) do
1732
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
2335
end
2436
end
2537

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
2847
end
2948

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)
3359
end
3460

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)
3863

3964
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
4166
{:ok, result} -> result
4267
end
4368

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, [])
4580
end
4681

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
4984

5085
defp format_query([artist: _artist, track: _track] = song_info) do
5186
Enum.map_join(song_info, fn {k, v} -> "#{k}:#{v} " end)

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ defmodule Moos2.MixProject do
1919
# Run "mix help compile.app" to learn about applications.
2020
def application do
2121
[
22-
extra_applications: [:logger, :plug_cowboy, :spotify_ex, :floki]
22+
extra_applications: [:plug_cowboy, :spotify_ex, :floki]
2323
]
2424
end
2525

2626
# Run "mix help deps" to learn about dependencies.
2727
defp deps do
2828
[
2929
{:floki, "~> 0.34.0"},
30+
{:httpoison, "~> 1.0.0"},
3031
{:jason, "~> 1.3"},
3132
{:plug_cowboy, "~> 2.6.1"},
32-
{:req, "~> 0.3.10"},
3333
{:spotify_ex, "~> 2.2.1"}
3434
]
3535
end

0 commit comments

Comments
 (0)