4

I want to make a program which will display currently playing songs from an internet radio stream(SomaFM). I am using HTTPoison library in Elixir. But I am failing to get a response. It just hangs.

I am using the following code:

HTTPoison.start
url = "http://ice1.somafm.com/lush-128-mp3"
headers = [{"Icy-Metadata", "1"}]
with {:ok, %HTTPoison.Response{body: body}} <- HTTPoison.get(url, headers) do
  body |> Poison.decode! |> IO.inspect
  else
    {:error, %HTTPoison.Error{reason: reason}} ->
      IO.inspect reason  
  end
end

I am actually very new to elixir, so if anyone can help me I would be really grateful.

Adam Millerchip
  • 11,422
  • 3
  • 29
  • 48
aeroith
  • 99
  • 2
  • 5

1 Answers1

5

When you use the get request you are requesting the audio file. If it's streaming then I guess it would never stop "downloading". You will need to do it differently.

I actually wrote up a quick example library. You could copy this module code, since you already have HTTPoison, you should have hackney as a dependency already.

Example module: https://github.com/ryanwinchester/shoutcast_ex/blob/master/lib/shoutcast.ex

defmodule Shoutcast do

  defmodule Meta do
    defstruct [:offset, :length, :data, :raw, :string]
    @type t :: %__MODULE__{
      data: map,
      offset: integer,
      length: integer,
      raw: binary,
      string: String.t
    }
  end

  def read_meta(url) do
    {:ok, _status, headers, ref} = :hackney.get(url, [{'Icy-Metadata', '1'}], "", [])

    offset = get_offset(headers)

    {:ok, data} = read_body(offset + 4081, ref, <<>>)

    {meta_length, meta} = extract_meta(data, offset)

    {:ok,
      %Meta{
        data: process_meta(meta),
        offset: offset,
        length: meta_length,
        raw: meta,
        string: String.trim(meta, <<0>>)
      }
    }
  end

  # Stream the body until we get what we want.
  defp read_body(max_length, ref, acc) when max_length > byte_size(acc) do
    case :hackney.stream_body(ref) do
      {:ok, data}      -> read_body(max_length, ref, <<acc::binary, data::binary>>)
      :done            -> {:ok, acc}
      {:error, reason} -> {:error, reason}
    end
  end

  defp read_body(_, _, acc), do: {:ok, acc}

  # Get the byte offset from the `icy-metaint` header.
  defp get_offset(headers) do
    headers
    |> Enum.into(%{})
    |> Map.get("icy-metaint")
    |> String.to_integer()
  end

  # Extract the meta data from the binary file stream.
  defp extract_meta(data, offset) do
    << _::binary-size(offset), length::binary-size(1), chunk::binary >> = data

    # The `length` byte will equal the metadata length/16.
    # Multiply by 16 to get the actual metadata length.
    <<l>> = length
    meta_length = l * 16

    << meta::binary-size(meta_length), _::binary >> = chunk

    {meta_length, meta}
  end

  # Process the binary meta data into a map.
  defp process_meta(meta) do
    meta
    |> String.trim_trailing(<<0>>)
    |> String.split(";")
    |> Enum.map(&String.split(&1, "="))
    |> Enum.reject(&(&1 == [""]))
    |> Enum.map(fn [k, v] -> {k, String.trim(v, "'")} end)
    |> Enum.into(%{})
  end
end

shoutcast meta

# Get meta from stream
{:ok, meta} = Shoutcast.read_meta("http://ice1.somafm.com/lush-128-mp3")

# Get title
meta.data["StreamTitle"]

I added the Meta struct to save some of the data I find interesting, you could easily remove that and modify the function it to just return the title, if you were only interested in that.

ryanwinchester
  • 10,239
  • 4
  • 25
  • 40
  • Actually I was trying to produce these steps in http://www.smackfu.com/stuff/programming/shoutcast.html. Metadata is actually inside the mp3 stream. How do I process the stream? – aeroith Jan 13 '18 at 20:45
  • I will update my answer for that. I think you will need to stream chunks until you get the one you want. – ryanwinchester Jan 13 '18 at 20:48
  • Thanks for the detailed answer. Much appreciated. – aeroith Jan 14 '18 at 05:53