Skip to content

Commit

Permalink
chore: Update to Metalorgie v5 API (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
papey authored Dec 1, 2024
1 parent 055178f commit 30bcbb5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 124 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
run: mix deps.get

- name: Test
run: mix test --no-start
run: mix test --exclude no_ci --no-start

container:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ COPY . .
RUN mix release

# App is build, setup runtime
FROM elixir:1.15 AS runtime
FROM elixir:1.17 AS runtime

# Install openssl
RUN apt-get update -y \
Expand Down
36 changes: 17 additions & 19 deletions lib/commands/mo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ defmodule O2M.Commands.Mo do
"band" ->
band(args)

"album" ->
album(args)
"albums" ->
albums(args)

"help" ->
help(args)
Expand All @@ -41,8 +41,8 @@ defmodule O2M.Commands.Mo do
"""
def band(args) do
case get_band(args) do
{:ok, band} ->
{:ok, forge_band_url(band["slug"])}
{:ok, %{url: url, desc: desc}} ->
{:ok, "[#{desc}](#{url})"}

error ->
error
Expand All @@ -52,21 +52,19 @@ defmodule O2M.Commands.Mo do
@doc """
Search for an album from a specified band on Metalorgie
"""
def album([]) do
"Missing band name and album name for `album` subcommand"
def albums([]) do
"Missing band name `albums` subcommand"
end

# If args provided
def album(args) do
[band | album] =
args
|> Enum.join(" ")
|> String.split("//")
|> Enum.map(fn e -> String.trim(e) end)
def albums(args) do
case get_albums(args) do
{:ok, albums} ->
message =
Enum.map(albums, fn %{title: title, url: url, year: year} -> "[#{title} (#{year})](#{url})" end)
|> Enum.join(" · ")

case get_album(String.split(band, " "), String.split(Enum.at(album, 0), " ")) do
{:ok, album} ->
{:ok, forge_album_url(band, album["name"], album["id"])}
{:ok, message}

error ->
error
Expand All @@ -78,8 +76,8 @@ defmodule O2M.Commands.Mo do
"""
def help([]) do
reply = "Available **mo** subcommands are :
**album**: to get album info (try _#{Config.get(:prefix)}mo help album_)
**band**: to get page band info (try _#{Config.get(:prefix)}mo help band_)
**albums**: to get albums (try _#{Config.get(:prefix)}mo help albums_)
**band**: to get page band (try _#{Config.get(:prefix)}mo help band_)
**help**: to get this help message"

{:ok, reply}
Expand All @@ -89,8 +87,8 @@ defmodule O2M.Commands.Mo do
def help(args) do
reply =
case Enum.join(args, " ") do
"album" ->
"Here is an example of \`album\` subcommand : \`\`\`#{Config.get(:prefix)}mo album korn // follow the leader \`\`\`"
"albums" ->
"Here is an example of \`albums\` subcommand : \`\`\`#{Config.get(:prefix)}mo albums korn\`\`\`"

"band" ->
"Here is an example of \`band\` subcommand : \`\`\`#{Config.get(:prefix)}mo band korn\`\`\`"
Expand Down
108 changes: 29 additions & 79 deletions lib/metalorgie/metalorgie.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,112 +20,62 @@ defmodule Metalorgie do
end

@doc """
Get json data for a specific band from Metalorgie
Get data for a specific band from Metalorgie
Returns ok + `json object` or error + message
Returns ok + map or error + message
"""
def get_band(band) do
# Concat string, but before apply a downcase operation on all list members
terms =
band
|> Enum.map(fn e -> String.downcase(e) end)

match = Enum.join(terms, " ")

# HTTP get call
resp =
HTTPoison.get!(
Metalorgie.get_config_url() <>
"/api/band.php" <>
"?filter=[%7B\"property\":\"name\",\"value\":\"#{Enum.join(terms, "%20")}\"%7D]"
"/api/bands" <>
"?name=#{Enum.join(terms, "%20")}"
)

# Decode json
{:ok, json} = Jason.decode(resp.body)

# Filter on band name, hard search
filter = Enum.filter(json, fn e -> String.downcase(e["name"]) == match end)
{:ok, json} =
Jason.decode(resp.body)

# If a band is found
case filter do
[band | _] ->
{:ok, band}
case json do
%{"bands" => [%{"url" => url, "shortDescription" => desc, "id" => id} | _]} ->
{:ok, %{url: url, desc: desc, id: id}}

_ ->
# if not
# Filter on band name, soft search
filter =
Enum.filter(json, fn e -> String.contains?(String.downcase(e["name"]), terms) end)

# If a band is found
case filter do
[band | _] -> {:ok, band}
_ -> {:error, "No band with name **#{Enum.join(band, " ")}** found"}
end
{:error, "No band with name **#{Enum.join(band, " ")}** found"}
end
end

@doc """
Get json data for a specific album from Metalorgie
Get albums data for a specific band from Metalorgie
Returns ok + `json object` or error + message
Returns ok + map or error + message
"""
def get_album(artist, album) do
# First get band data
case get_band(artist) do
{:ok, data} ->
# Album title
album = Enum.join(album, " ")

# Filter albums by name
filtered =
Enum.filter(data["discography"], fn e ->
String.contains?(String.downcase(e["name"]), String.downcase(album))
end)

case filtered do
[album | _] ->
{:ok, album}
def get_albums(band) do
case get_band(band) do
{:ok, %{id: id}} ->
resp =
HTTPoison.get!(
Metalorgie.get_config_url() <>
"/api/albums" <>
"?band=#{id}"
)

{:ok, json} =
Jason.decode(resp.body)

case json do
%{"albums" => albums} ->
{:ok, Enum.map(albums, fn album -> %{title: album["title"], url: album["url"], year: album["year"]} end)}

_ ->
{:error, "No album named **#{album}** found for artist **#{Enum.join(artist, " ")}**"}
{:error, "No album found for band **#{band}**"}
end

{:error, message} ->
{:error, message}
end
end

@doc """
Forge a band url from a slug
Returns formatted url containing band slug
## Examples
iex> Metalorgie.forge_band_url("korn")
"https://www.metalorgie.com/groupe/korn"
"""
def forge_band_url(slug) do
Metalorgie.get_config_url() <> "/groupe/" <> slug
end

@doc """
Forge an album url from a band, album name and album id
Returns formatted url containing album slug
## Examples
iex> Metalorgie.forge_album_url("korn", "the nothing", "31745")
"https://www.metalorgie.com/groupe/korn/31745_the-nothing"
"""
def forge_album_url(band, album, id) do
"#{Metalorgie.get_config_url()}/groupe/#{slug_term(band)}/#{id}_#{slug_term(album)}"
end

defp slug_term(term) do
term
|> String.replace(":", "")
|> String.replace(" ", "-")
end
end
12 changes: 6 additions & 6 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
"ed25519": {:hex, :ed25519, "1.4.1", "479fb83c3e31987c9cad780e6aeb8f2015fb5a482618cdf2a825c9aff809afc4", [:mix], [], "hexpm", "0dacb84f3faa3d8148e81019ca35f9d8dcee13232c32c9db5c2fb8ff48c80ec7"},
"elixir_feed_parser": {:hex, :elixir_feed_parser, "2.1.0", "bb96fb6422158dc7ad59de62ef211cc69d264acbbe63941a64a5dce97bbbc2e6", [:mix], [{:timex, "~> 3.4", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "2d3c62fe7b396ee3b73d7160bc8fadbd78bfe9597c98c7d79b3f1038d9cba28f"},
"equivalex": {:hex, :equivalex, "1.0.3", "170d9a82ae066e0020dfe1cf7811381669565922eb3359f6c91d7e9a1124ff74", [:mix], [], "hexpm", "46fa311adb855117d36e461b9c0ad2598f72110ad17ad73d7533c78020e045fc"},
"ex_doc": {:hex, :ex_doc, "0.34.2", "13eedf3844ccdce25cfd837b99bea9ad92c4e511233199440488d217c92571e8", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "5ce5f16b41208a50106afed3de6a2ed34f4acfd65715b82a0b84b49d995f95c1"},
"expo": {:hex, :expo, "0.5.2", "beba786aab8e3c5431813d7a44b828e7b922bfa431d6bfbada0904535342efe2", [:mix], [], "hexpm", "8c9bfa06ca017c9cb4020fabe980bc7fdb1aaec059fd004c2ab3bff03b1c599c"},
"ex_doc": {:hex, :ex_doc, "0.35.1", "de804c590d3df2d9d5b8aec77d758b00c814b356119b3d4455e4b8a8687aecaf", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "2121c6402c8d44b05622677b761371a759143b958c6c19f6558ff64d0aed40df"},
"expo": {:hex, :expo, "1.1.0", "f7b9ed7fb5745ebe1eeedf3d6f29226c5dd52897ac67c0f8af62a07e661e5c75", [:mix], [], "hexpm", "fbadf93f4700fb44c331362177bdca9eeb8097e8b0ef525c9cc501cb9917c960"},
"forecastle": {:hex, :forecastle, "0.1.2", "f8dab08962c7a33010ebd39182513129f17b8814aa16fa453ddd536040882daf", [:mix], [], "hexpm", "8efaeb2e7d0fa24c605605e42562e2dbb0ffd11dc1dd99ef77d78884536ce501"},
"gen_stage": {:hex, :gen_stage, "1.2.0", "ee49244b57803f54bdab08a60a927e1b4e5bb5d635c52eca0f376a0673af3f8c", [:mix], [], "hexpm", "c3e40992c72e74d9c4eda16d7515bf32c9e7b634e827ab11091fff3290f7b503"},
"gen_state_machine": {:hex, :gen_state_machine, "3.0.0", "1e57f86a494e5c6b14137ebef26a7eb342b3b0070c7135f2d6768ed3f6b6cdff", [:mix], [], "hexpm", "0a59652574bebceb7309f6b749d2a41b45fdeda8dbb4da0791e355dd19f0ed15"},
"gettext": {:hex, :gettext, "0.24.0", "6f4d90ac5f3111673cbefc4ebee96fe5f37a114861ab8c7b7d5b30a1108ce6d8", [:mix], [{:expo, "~> 0.5.1", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "bdf75cdfcbe9e4622dd18e034b227d77dd17f0f133853a1c73b97b3d6c770e8b"},
"gettext": {:hex, :gettext, "0.26.2", "5978aa7b21fada6deabf1f6341ddba50bc69c999e812211903b169799208f2a8", [:mix], [{:expo, "~> 0.5.1 or ~> 1.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "aa978504bcf76511efdc22d580ba08e2279caab1066b76bb9aa81c4a1e0a32a5"},
"gun": {:hex, :gun, "2.1.0", "b4e4cbbf3026d21981c447e9e7ca856766046eff693720ba43114d7f5de36e87", [:make, :rebar3], [{:cowlib, "2.13.0", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm", "52fc7fc246bfc3b00e01aea1c2854c70a366348574ab50c57dfe796d24a0101d"},
"hackney": {:hex, :hackney, "1.17.1", "08463f93d2cc1a03817bf28d8dae6021543f773bd436c9377047224856c4422c", [:rebar3], [{:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "~> 3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "d2cba9e3c8103ad0320623e9f1c33e8d378a15eaabe2ee8ae441898f3d35a18c"},
"httpoison": {:hex, :httpoison, "2.2.1", "87b7ed6d95db0389f7df02779644171d7319d319178f6680438167d7b69b1f3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "51364e6d2f429d80e14fe4b5f8e39719cacd03eb3f9a9286e61e216feac2d2df"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
"kcl": {:hex, :kcl, "1.4.2", "8b73a55a14899dc172fcb05a13a754ac171c8165c14f65043382d567922f44ab", [:mix], [{:curve25519, ">= 1.0.4", [hex: :curve25519, repo: "hexpm", optional: false]}, {:ed25519, "~> 1.3", [hex: :ed25519, repo: "hexpm", optional: false]}, {:poly1305, "~> 1.0", [hex: :poly1305, repo: "hexpm", optional: false]}, {:salsa20, "~> 1.0", [hex: :salsa20, repo: "hexpm", optional: false]}], "hexpm", "9f083dd3844d902df6834b258564a82b21a15eb9f6acdc98e8df0c10feeabf05"},
"makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
"makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"},
"makeup_elixir": {:hex, :makeup_elixir, "1.0.0", "74bb8348c9b3a51d5c589bf5aebb0466a84b33274150e3b6ece1da45584afc82", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "49159b7d7d999e836bedaf09dcf35ca18b312230cf901b725a64f3f42e407983"},
"makeup_erlang": {:hex, :makeup_erlang, "1.0.1", "c7f58c120b2b5aa5fd80d540a89fdf866ed42f1f3994e4fe189abebeab610839", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "8a89a1eeccc2d798d6ea15496a6e4870b75e014d1af514b1b71fa33134f57814"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"},
Expand All @@ -39,6 +39,6 @@
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
"tesla": {:hex, :tesla, "1.5.0", "7ee3616be87024a2b7231ae14474310c9b999c3abb1f4f8dbc70f86bd9678eef", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.13", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:msgpax, "~> 2.3", [hex: :msgpax, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "1d0385e41fbd76af3961809088aef15dec4c2fdaab97b1c93c6484cb3695a122"},
"timex": {:hex, :timex, "3.7.11", "bb95cb4eb1d06e27346325de506bcc6c30f9c6dea40d1ebe390b262fad1862d1", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.20", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 1.1", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "8b9024f7efbabaf9bd7aa04f65cf8dcd7c9818ca5737677c7b76acbc6a94d1aa"},
"tzdata": {:hex, :tzdata, "1.1.1", "20c8043476dfda8504952d00adac41c6eda23912278add38edc140ae0c5bcc46", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a69cec8352eafcd2e198dea28a34113b60fdc6cb57eb5ad65c10292a6ba89787"},
"tzdata": {:hex, :tzdata, "1.1.2", "45e5f1fcf8729525ec27c65e163be5b3d247ab1702581a94674e008413eef50b", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "cec7b286e608371602318c414f344941d5eb0375e14cfdab605cca2fe66cba8b"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
}
29 changes: 11 additions & 18 deletions test/metalorgie_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,27 @@ defmodule MetalorgieTest do
use ExUnit.Case
doctest Metalorgie

@tag :down
@tag :no_ci
test "get_band for band `korn`" do
{:ok, json} = Metalorgie.get_band(["korn"])
assert json["name"] == "Korn"
{:ok, %{url: url}} = Metalorgie.get_band(["korn"])
assert url == "http://www.metalorgie.com/groupe/Korn"
end

@tag :down
@tag :no_ci
test "get_band for band `opeth`" do
{:ok, json} = Metalorgie.get_band(["opeth"])
assert json["name"] == "Opeth"
{:ok, result} = Metalorgie.get_band(["opeth"])
assert result[:desc] == "Les plus atypiques du death suédois"
end

@tag :down
@tag :no_ci
test "get_band for band `nopnop`" do
{:error, message} = Metalorgie.get_band(["nopnop"])
assert message == "No band with name **nopnop** found"
end

@tag :down
test "get_album for album `follow the leader` by `korn`" do
{:ok, album} = Metalorgie.get_album(["korn"], String.split("Follow the Leader", " "))
assert album["name"] == "Follow the leader"
assert album["year"] == "1998"
end

@tag :down
test "get_album for album `nop` by band `iron maiden`" do
{:error, message} = Metalorgie.get_album(["iron", "maiden"], ["nop"])
assert message == "No album named **nop** found for artist **iron maiden**"
@tag :no_ci
test "get_albums for band `iron maiden`" do
{:ok, result} = Metalorgie.get_albums(["iron", "maiden"])
assert length(result) >= 10
end
end

0 comments on commit 30bcbb5

Please sign in to comment.