Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Merge pull request #38 from The-B1T-Foundation/added_meme_api
Browse files Browse the repository at this point in the history
Added meme api
  • Loading branch information
b1tflyyyy authored May 7, 2024
2 parents 70d5825 + ab6c5e5 commit 56524c6
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 7 deletions.
6 changes: 6 additions & 0 deletions source/business_logic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(MODEL_HEADERS
model/config/english_words_info_api_config/english_words_info_api_config_model.hpp
model/metrics/metrics_model.hpp
model/config/tg_root_user_config/tg_root_user_config_model.hpp
model/config/meme_api_config/meme_api_config_model.hpp
)

set(MODEL_SOURCES
Expand All @@ -45,6 +46,7 @@ set(MODEL_SOURCES
model/config/english_words_info_api_config/english_words_info_api_config_model.cpp
model/metrics/metrics_model.cpp
model/config/tg_root_user_config/tg_root_user_config_model.cpp
model/config/meme_api_config/meme_api_config_model.cpp
)

set(CONTROLLER_HEADERS
Expand All @@ -62,6 +64,8 @@ set(CONTROLLER_HEADERS
controller/config/english_words_info_api_config/english_words_info_api_config_controller.hpp
controller/db/metrics_db/metrics_db_controller.hpp
controller/config/tg_root_user_config/tg_root_user_config_controller.hpp
controller/api/meme_api/meme_api_controller.hpp
controller/config/meme_api_config/meme_api_config_controller.hpp
)

set(CONTROLLER_SOURCES
Expand All @@ -79,6 +83,8 @@ set(CONTROLLER_SOURCES
controller/config/english_words_info_api_config/english_words_info_api_config_controller.cpp
controller/db/metrics_db/metrics_db_controller.cpp
controller/config/tg_root_user_config/tg_root_user_config_controller.cpp
controller/api/meme_api/meme_api_controller.cpp
controller/config/meme_api_config/meme_api_config_controller.cpp
)

add_library(${LIBRARY_NAME} STATIC
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// MIT License

// Copyright (c) 2024 The B1T Foundation

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#include "meme_api_controller.hpp"

// ---------------------------------------------------------------------------------------------------------------------
nljson AMeme_API_Controller::Json_Response{};

// ---------------------------------------------------------------------------------------------------------------------
AMeme_API_Controller::AMeme_API_Controller(const AMeme_API_Config& meme_api_config) :
ABase_API_Controller{ "https://programming-memes-images.p.rapidapi.com/v1/memes" }, Headers{ nullptr }
{
Headers = curl_slist_append(Headers, std::format("X-RapidAPI-Key: {}", meme_api_config.Get_API_Key()).c_str());
Headers = curl_slist_append(Headers, std::format("X-RapidAPI-Host: {}", meme_api_config.Get_API_Host()).c_str());
}

// ---------------------------------------------------------------------------------------------------------------------
AMeme_API_Controller::~AMeme_API_Controller()
{
curl_slist_free_all(Headers);
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMeme_API_Controller::Get_Meme()
{
if (Meme_Links_Stack.empty())
{
Send_Request("GET");

for (std::size_t i{}; !Json_Response.empty() && i < 12; ++i) // 12 - default count of meme links
{
Meme_Links_Stack.push(Json_Response[i]["image"]);
}
}

auto tmp{ Meme_Links_Stack.top() };
Meme_Links_Stack.pop();

return tmp;
}

// ---------------------------------------------------------------------------------------------------------------------
bool AMeme_API_Controller::Send_Request(std::string_view request_type)
{
if (Curl = curl_easy_init(); Curl != nullptr)
{
curl_easy_setopt(Curl, CURLOPT_URL, Url.data());
curl_easy_setopt(Curl, CURLOPT_CUSTOMREQUEST, request_type.data());
curl_easy_setopt(Curl, CURLOPT_HTTPHEADER, Headers);
curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, &ABase_API_Controller::Write_Callback);
curl_easy_setopt(Curl, CURLOPT_WRITEDATA, &AMeme_API_Controller::Json_Response);

curl_easy_perform(Curl);

std::size_t resp_code{};
curl_easy_getinfo(Curl, CURLINFO_RESPONSE_CODE, &resp_code);

curl_easy_cleanup(Curl);

if (resp_code != EHTTP_STATUS::OK)
{
return false;
}
}

return Curl != nullptr;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// MIT License

// Copyright (c) 2024 The B1T Foundation

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#pragma once

#include <format>
#include <stack>
#include <vector>
#include <string>

#include <controller/api/base_api/base_api_controller.hpp>

#include <model/config/meme_api_config/meme_api_config_model.hpp>

class AMeme_API_Controller : public ABase_API_Controller
{
public:
explicit AMeme_API_Controller(const AMeme_API_Config& meme_api_config);
~AMeme_API_Controller() override;

std::string Get_Meme();

private:
bool Send_Request(std::string_view request_type);

private:
std::stack<std::string, std::vector<std::string>> Meme_Links_Stack;
static nljson Json_Response;
curl_slist* Headers;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// MIT License

// Copyright (c) 2024 The B1T Foundation

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#include "meme_api_config_controller.hpp"

// ---------------------------------------------------------------------------------------------------------------------
std::optional<AMeme_API_Config> AMeme_API_Config_Controller::Load_Config()
{
char* api_key{ std::getenv("MEME_API_KEY") };
if (!api_key)
{
return std::nullopt;
}

char* api_host{ std::getenv("MEME_API_HOST") };
if (!api_host)
{
return std::nullopt;
}

return AMeme_API_Config{ api_key, api_host };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// MIT License

// Copyright (c) 2024 The B1T Foundation

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#pragma once

#include <optional>

#include <model/config/meme_api_config/meme_api_config_model.hpp>

class AMeme_API_Config_Controller
{
public:
constexpr AMeme_API_Config_Controller() = default;
constexpr ~AMeme_API_Config_Controller() = default;

static std::optional<AMeme_API_Config> Load_Config();
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AEnglish_Words_Info_API_Config
{
public:
explicit AEnglish_Words_Info_API_Config(std::string_view api_key, std::string_view api_host);
~AEnglish_Words_Info_API_Config() = default;
constexpr ~AEnglish_Words_Info_API_Config() = default;

[[nodiscard]] std::string_view Get_API_Key() const;
[[nodiscard]] std::string_view Get_API_Host() const;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// MIT License

// Copyright (c) 2024 The B1T Foundation

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#include "meme_api_config_model.hpp"

// ---------------------------------------------------------------------------------------------------------------------
AMeme_API_Config::AMeme_API_Config(std::string_view api_key, std::string_view api_host) :
API_Key{ api_key }, API_Host{ api_host }
{ }

// ---------------------------------------------------------------------------------------------------------------------
std::string_view AMeme_API_Config::Get_API_Key() const
{
return API_Key;
}

// ---------------------------------------------------------------------------------------------------------------------
std::string_view AMeme_API_Config::Get_API_Host() const
{
return API_Host;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// MIT License

// Copyright (c) 2024 The B1T Foundation

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


#pragma once

#include <cstdint>
#include <string>

class AMeme_API_Config
{
public:
explicit AMeme_API_Config(std::string_view api_key, std::string_view api_host);
constexpr ~AMeme_API_Config() = default;

[[nodiscard]] std::string_view Get_API_Key() const;
[[nodiscard]] std::string_view Get_API_Host() const;

private:
std::string_view API_Key;
std::string_view API_Host;
};
14 changes: 12 additions & 2 deletions source/view/handlers/message_handler/message_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
std::mutex AMessage_Handler::Mutex{ };

// ---------------------------------------------------------------------------------------------------------------------
AMessage_Handler::AMessage_Handler(TgBot::Bot& tg_bot, AUser_DB_Controller& user_db_controller, AState_DB_Controller& state_db_controller, ATask_DB_Controller& task_db_controller, AStats_DB_Controller& stats_db_controller, AEnglish_Words_Info_API_Controller& english_words_api_controller, AMetrics_DB_Controller& metrics_db_controller, ATG_Root_User_Config& tg_root_user_cfg) :
TG_Bot{ tg_bot }, User_DB_Controller{ user_db_controller }, State_DB_Controller{ state_db_controller }, Task_DB_Controller{ task_db_controller }, Stats_DB_Controller{ stats_db_controller }, English_Words_API_Controller{ english_words_api_controller }, Metrics_DB_Controller{ metrics_db_controller }, TG_Root_User_Cfg{ tg_root_user_cfg }, Words_Api_Requests_Limiter{ 2500, 24 }
AMessage_Handler::AMessage_Handler(TgBot::Bot& tg_bot, AUser_DB_Controller& user_db_controller, AState_DB_Controller& state_db_controller, ATask_DB_Controller& task_db_controller, AStats_DB_Controller& stats_db_controller, AMeme_API_Controller& meme_api_controller, AEnglish_Words_Info_API_Controller& english_words_api_controller, AMetrics_DB_Controller& metrics_db_controller, ATG_Root_User_Config& tg_root_user_cfg) :
TG_Bot{ tg_bot }, TG_Root_User_Cfg{ tg_root_user_cfg }, User_DB_Controller{ user_db_controller }, State_DB_Controller{ state_db_controller }, Task_DB_Controller{ task_db_controller }, Stats_DB_Controller{ stats_db_controller },Metrics_DB_Controller{ metrics_db_controller }, English_Words_API_Controller{ english_words_api_controller }, Meme_API_Controller{ meme_api_controller }, Words_Api_Requests_Limiter{ 2500, 24 }, Meme_Api_Requests_Limiter{ 150, 24 }
{ }

// ---------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -167,6 +167,16 @@ void AMessage_Handler::Bind_Commands()

TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Not_Found_Synonym_Msg());
});
TG_Bot.getEvents().onCommand(SMessage_Commands::Get_Meme.data(), [this](TgBot::Message::Ptr message) -> void
{
if (!Meme_Api_Requests_Limiter.Can_Send_Request())
{
TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Limit_Api_Requests_Msg());
return;
}

TG_Bot.getApi().sendMessage(message->chat->id, Meme_API_Controller.Get_Meme());
});
TG_Bot.getEvents().onCommand(SMessage_Commands::Metrics_Range.data(), [this](TgBot::Message::Ptr message) -> void
{
if (!Is_Root_User(message->from->id))
Expand Down
Loading

0 comments on commit 56524c6

Please sign in to comment.