From e92da2bd6626472810d05436e57cc8e5c8ed6fdd Mon Sep 17 00:00:00 2001 From: BttrDrgn <49764143+BttrDrgn@users.noreply.github.com> Date: Sat, 14 May 2022 00:43:10 -0500 Subject: [PATCH] Share station via api call Currently goes to radio.garden frontend --- src/app/api/api.cpp | 56 +++++++++++++++++++++-------------------- src/app/api/api.hpp | 4 +-- src/app/audio/audio.cpp | 2 +- src/app/menus/menus.cpp | 20 +++++++++++++++ 4 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/app/api/api.cpp b/src/app/api/api.cpp index f8e91ec..6757db6 100644 --- a/src/app/api/api.cpp +++ b/src/app/api/api.cpp @@ -17,11 +17,11 @@ void api::get_places() std::thread([] { - httplib::Client cli(API_URL); + httplib::Client cli(HOST_NAME); if (httplib::Result res = cli.Get(PLACES_ENDPOINT)) { - logger::log_info(logger::va("Accessing %s%s", API_URL, PLACES_ENDPOINT)); + logger::log_info(logger::va("Accessing %s%s", HOST_NAME, PLACES_ENDPOINT)); if (res->status == 200) { @@ -91,11 +91,11 @@ void api::get_details(const place_t& place_in) std::thread([place_in] { - httplib::Client cli(API_URL); + httplib::Client cli(HOST_NAME); if (httplib::Result res = cli.Get(PLACE_DETAIL_ENDPOINT(&place_in.id[0]))) { - logger::log_info(logger::va("Accessing %s%s", API_URL, PLACE_DETAIL_ENDPOINT(&place_in.id[0]))); + logger::log_info(logger::va("Accessing %s%s", HOST_NAME, PLACE_DETAIL_ENDPOINT(&place_in.id[0]))); if (res->status == 200) { nl::json json = nl::json::parse(res->body); @@ -135,32 +135,34 @@ void api::get_details(const place_t& place_in) }).detach(); } -void api::get_station(const std::string& id) +std::string api::get_shareable_url(const std::string& id) { - api::stations_done = false; + std::string url; - std::thread([id] + //This one isnt threaded; I believe this should be fast enough to not cause issues when playing + httplib::Client cli(HOST_NAME); + + if (httplib::Result res = cli.Get(STATION_ENDPOINT(&id[0]))) + { + logger::log_info(logger::va("Accessing %s%s", HOST_NAME, STATION_ENDPOINT(&id[0]))); + + if (res->status == 200) { - httplib::Client cli(API_URL); + nl::json json = nl::json::parse(res->body); + api::version_check(json); - if (httplib::Result res = cli.Get(STATION_ENDPOINT(&id[0]))) - { - logger::log_info(logger::va("Accessing %s%s", API_URL, STATION_ENDPOINT(&id[0]))); - if (res->status == 200) - { - nl::json json = nl::json::parse(res->body); - api::version_check(json); + url = json["data"]["url"].dump(); + url.erase(std::remove(url.begin(), url.end(), '\"'), url.end()); - //Finish - api::stations_done = true; - json.clear(); - } - else - { - logger::log_error("An error occured when gathering station data!"); - } - } - }).detach(); + json.clear(); + } + else + { + logger::log_error("An error occured when getting shareable url!"); + } + } + + return url; } void api::search_stations(const std::string& str) @@ -176,11 +178,11 @@ void api::search_stations(const std::string& str) std::thread([str] { - httplib::Client cli(API_URL); + httplib::Client cli(HOST_NAME); if (httplib::Result res = cli.Get(SEARCH_ENDPOINT(&str[0]))) { - logger::log_info(logger::va("Accessing %s%s", API_URL, SEARCH_ENDPOINT(&str[0]))); + logger::log_info(logger::va("Accessing %s%s", HOST_NAME, SEARCH_ENDPOINT(&str[0]))); if (res->status == 200) { nl::json json = nl::json::parse(res->body); diff --git a/src/app/api/api.hpp b/src/app/api/api.hpp index 2098e26..faa9dd8 100644 --- a/src/app/api/api.hpp +++ b/src/app/api/api.hpp @@ -1,6 +1,6 @@ #pragma once -#define API_URL "http://radio.garden" +#define HOST_NAME "http://radio.garden" #define VERSION 1 #define VERSION_HASH "\"bd23e54\"" //radio.garden git commit hash #define PLACES_ENDPOINT "/api/ara/content/places" @@ -27,7 +27,7 @@ class api static void get_places(); static void filter_place(const std::string& key); static void get_details(const place_t& place_in); - static void get_station(const std::string& id); + static std::string get_shareable_url(const std::string& id); static void search_stations(const std::string& str); static std::vector places; diff --git a/src/app/audio/audio.cpp b/src/app/audio/audio.cpp index c75b0ac..a28b8bd 100644 --- a/src/app/audio/audio.cpp +++ b/src/app/audio/audio.cpp @@ -67,7 +67,7 @@ void audio::play(const std::string& url) { std::thread([url] { - std::string final_url = logger::va("%s%s", API_URL, AUDIO_ENDPOINT(&url[0])); + std::string final_url = logger::va("%s%s", HOST_NAME, AUDIO_ENDPOINT(&url[0])); audio::currently_playing.title = "N/A"; audio::currently_playing.url = url; audio::paused = false; diff --git a/src/app/menus/menus.cpp b/src/app/menus/menus.cpp index 49a355c..562fc2d 100644 --- a/src/app/menus/menus.cpp +++ b/src/app/menus/menus.cpp @@ -234,6 +234,26 @@ void menus::actions() api::get_places(); } + ImGui::SameLine(); + + if (audio::currently_playing.station.title != "N/A") + { + if (ImGui::Button("Share Station")) + { + std::string url = logger::va("%s%s", HOST_NAME, &api::get_shareable_url(audio::currently_playing.station.id)[0]); + + ImGuiIO& io = ImGui::GetIO(); + io.SetClipboardTextFn(io.ClipboardUserData, &url[0]); + } + } + + if (ImGui::IsItemHovered()) + { + ImGui::BeginTooltip(); + ImGui::Text("Copies the link to the\ncurrently playing station"); + ImGui::EndTooltip(); + } + ImGui::NewLine(); ImGui::Text("Audio Controls");