Skip to content

Commit

Permalink
Merge pull request #321 from alexweav/query-files
Browse files Browse the repository at this point in the history
Query files by term
  • Loading branch information
alexweav authored Jun 18, 2020
2 parents f30af8f + b8445e2 commit 1ee138f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions apps/services/lib/services/audio/downloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ defmodule Services.Audio.Downloader do
end
end

@doc """
Queries files by name.
"""
def query_files(term) do
uri = "/files?name=" <> URI.encode(term)

case Downloader.get(uri) do
{:ok, %Response{status_code: 200, body: body}} -> {:ok, body}
response -> {:error, response}
end
end

@doc """
Gets the metadata for a file managed the downloader, by path.
"""
Expand Down
12 changes: 11 additions & 1 deletion apps/services/lib/services/voice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ defmodule Services.Voice do

def handle_message("!play " <> audio_file_to_play, message, _) do
with :ok <- Downloader.available?(),
{:ok, file_metadata} <- Downloader.get_file(audio_file_to_play),
{:ok, file_response} <- Downloader.query_files(audio_file_to_play),
# Downloader.get_file(audio_file_to_play),
{:ok, file_metadata} <- get_file_by_term(file_response),
{:ok, audio_file} <- Briefly.create(),
:ok <- Downloader.download_file(file_metadata["path"], audio_file) do
transcode_and_send(audio_file, message)
Expand Down Expand Up @@ -85,6 +87,14 @@ defmodule Services.Voice do
end
end

defp get_file_by_term(%{"files" => []}) do
{:error, :notfound}
end

defp get_file_by_term(resp) do
{:ok, Enum.at(resp["files"], 0)}
end

defp send_silence_sigil(connection) do
_ =
Enum.reduce(1..5, {nil, connection}, fn _, {last_time, conn} ->
Expand Down

0 comments on commit 1ee138f

Please sign in to comment.