Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sonarr V4 #458

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ http://midarr:4000/api/webhooks/series?token=some-api-token
We support the following integration versions:

* Radarr `v5.x`
* Sonarr `v3.x`
* Sonarr `v4.x`

## Contributing

Expand Down
6 changes: 2 additions & 4 deletions lib/media_server_web/controllers/images_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ defmodule MediaServerWeb.ImagesController do
end

def index(conn, %{"series" => id, "type" => "poster"}) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} =
HTTPoison.get(MediaServerWeb.Repositories.Series.get_url("mediacover/#{id}/poster-500.jpg"))
body = MediaServerWeb.Repositories.Series.get("mediacover/#{id}/poster.jpg")

conn
|> put_resp_header("content-type", "image/image")
Expand All @@ -65,8 +64,7 @@ defmodule MediaServerWeb.ImagesController do
end

def index(conn, %{"series" => id, "type" => "background"}) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} =
HTTPoison.get(MediaServerWeb.Repositories.Series.get_url("mediacover/#{id}/fanart.jpg"))
body = MediaServerWeb.Repositories.Series.get("mediacover/#{id}/fanart.jpg")

conn
|> put_resp_header("content-type", "image/image")
Expand Down
17 changes: 3 additions & 14 deletions lib/media_server_web/repositories/episodes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,22 @@ defmodule MediaServerWeb.Repositories.Episodes do
alias MediaServer.Subtitles

def get_all(series_id, season_number) do
HTTPoison.get("#{Series.get_url("episode")}&seriesId=#{series_id}")
|> Series.handle_response()
Series.get("episode?seriesId=#{series_id}&includeImages=true")
|> Enum.filter(fn episode ->
episode["seasonNumber"] === String.to_integer(season_number)
end)
|> replace_each_with_episode_show_response()
end

def get_episode(id) do
HTTPoison.get("#{Series.get_url("episode/#{id}")}")
|> Series.handle_response()
Series.get("episode/#{id}")
end

def get_episode_path(id) do
episode =
HTTPoison.get("#{Series.get_url("episode/#{id}")}")
|> Series.handle_response()
episode = Series.get("episode/#{id}")

episode["episodeFile"]["path"]
end

def replace_each_with_episode_show_response(episodes) do
Enum.map(episodes, fn episode ->
get_episode(episode["id"])
end)
end

def handle_image(nil) do
nil
end
Expand Down
17 changes: 12 additions & 5 deletions lib/media_server_web/repositories/series.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
defmodule MediaServerWeb.Repositories.Series do
def get_url(url) do
"#{System.get_env("SONARR_BASE_URL")}/api/v3/#{url}?apikey=#{System.get_env("SONARR_API_KEY")}"
def get(url) do
HTTPoison.get("#{System.get_env("SONARR_BASE_URL")}/api/v3/#{url}", %{
"X-Api-Key" => System.get_env("SONARR_API_KEY")
})
|> handle_response()
end

def handle_response({:ok, %HTTPoison.Response{status_code: 200, body: body}}) do
Jason.decode!(body)
case Jason.decode(body) do
{:ok, decoded} ->
decoded
_ ->
body
end
end

def handle_response(_) do
nil
end

def get_all() do
HTTPoison.get(get_url("series"))
|> handle_response()
get("series")
|> Enum.sort_by(& &1["title"], :asc)
end
end
Loading