Jan 18, 2025
🔊 Nesse vídeo mostramos como utilizar a Curlpp que é um wrapper para a libcurl consumindo APIs em conjunto com JSON++ e Tabulate.
Instale a cURLpp:
Pesquise no seu gerenciador de pacotes, exemplo no Ubuntu:
sudo apt install libcurlpp-dev libcurl4-openssl-dev libcurlpp0t64
main.cpp
#include <iostream> #include <sstream> #include <curlpp/Easy.hpp> #include <curlpp/Options.hpp> #include <curlpp/cURLpp.hpp> #include <memory> //#include <regex> #include <nlohmann/json.hpp> #include <tabulate/table.hpp> using namespace tabulate; #include <unordered_map> /*constexpr auto change_ip = [](const std::string& str){ //std::regex ip("\\b(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\b"); std::regex ip("Follow.*"); return std::regex_replace(str, ip, ""); };*/ constexpr auto unicode = [](const std::string& str){ static const std::unordered_map<std::string, std::string> html = { {"€", "\u20AC"}, {"£", "\u00A3"}, {"$", "$"}, }; for(auto var : html){ if(var.first == str){ return var.second; } } return std::string{}; }; int main(){ try{ auto cleanup = std::make_unique<curlpp::Cleanup>(); auto response = std::make_unique<std::ostringstream>(); auto request = std::make_unique<curlpp::Easy>(); //request->setOpt<curlpp::options::Url>("https://gnu.terminalroot.com.br/ip.php"); //request->setOpt<curlpp::options::Url>("https://v2d.wttr.in/Londrina"); request->setOpt<curlpp::options::Url>("https://api.coindesk.com/v1/bpi/currentprice.json"); request->setOpt<curlpp::options::UserAgent>("curl/7.68.0 GNU/Linux Ubuntu x86_64"); request->setOpt<curlpp::options::WriteStream>(response.get()); request->perform(); //std::cout << change_ip(response->str()) << '\n'; //std::cout << response->str() << '\n'; nlohmann::json json = nlohmann::json::parse(response->str()); //std::cout << std::setw(2) << json["bpi"]["USD"] << '\n'; Table table; table.add_row({"CURRENCY", "DESCRIPTION", "RATE", "RATE_FLOAT", "SYMBOL"}); for(auto& [key, value]: json["bpi"].items()){ table.add_row({ key, value["description"], value["rate"], std::to_string(value["rate_float"].get<double>()), unicode(value["symbol"]), }); } table.format() .multi_byte_characters(true) .font_style({FontStyle::bold}) .border_top("─") .border_bottom("─") .border_left("│") .border_right("│") .corner("∎"); for(size_t i = {}; i <= table.size(); ++i){ table[0][i].format() .font_background_color(Color::blue) .font_color(Color::white); table[1][i].format() .font_background_color(Color::red) .font_color(Color::white); table[2][i].format() .font_background_color(Color::green) .font_color(Color::white); table[3][i].format() .font_background_color(Color::magenta) .font_color(Color::white); } std::cout << table << std::endl; }catch(const curlpp::RuntimeError& e){ std::cerr << "Runtime error: " << e.what() << '\n'; }catch(const curlpp::LogicError& e){ std::cerr << "Logic error: " << e.what() << '\n'; } return EXIT_SUCCESS; }
curl cpp json tui regex
Inscreva-se
13 Jan 2025
06 Jan 2025
18 Dec 2024
Saiba Mais
Após cadastro e confirmação do e-mail, enviaremos semanalmente resumos e também sempre que houver novidades por aqui para que você mantenha-se atualizado!