#include "echoion/core/Logger.hpp" #include "echoion/core/UrlShortener.hpp" #include #include namespace Echoion::Core { UrlShortener::UrlShortener(std::shared_ptr networkClient) : networkClient_(std::move(networkClient)) { } UrlShortener::~UrlShortener() = default; Api::Result UrlShortener::shortenYouTubeUrl(std::string_view videoId) { std::string originalUrl = std::format("https://www.youtube.com/watch?v={}", videoId); return shortenUrl(originalUrl); } Api::Result UrlShortener::shortenUrl(std::string_view originalUrl) { // Check cache first { std::lock_guard lock(cacheMutex_); auto it = cache_.find(std::string(originalUrl)); if (it != cache_.end()) { auto now = std::chrono::system_clock::now(); if (now - it->second.timestamp < cacheTtl_) { ECHOION_LOG_DEBUG(std::format("URL cache hit: {} -> {}", originalUrl, it->second.shortenedUrl)); return it->second.shortenedUrl; } // Expired, remove from cache cache_.erase(it); } } if (!networkClient_) { return std::unexpected( Api::Error(Api::ErrorCode::ServiceUnavailable, "Network client not available")); } // faumaray.ru API endpoint for URL shortening // POST /shorten with JSON body {"longUrl": "https://..."} // Response: {"shortUrl": "https://faumaray.ru/xxxxx"} std::string apiUrl = "https://faumaray.ru/shorten"; // Build JSON request body - API expects "longUrl" field std::string jsonBody = std::format(R"({{"longUrl":"{}"}})", originalUrl); std::vector headers = { {"Content-Type", "application/json"}, {"User-Agent", "EchoionBot/1.0"}}; ECHOION_LOG_DEBUG( std::format("Shortening URL via faumaray.ru: {}", originalUrl)); auto response = networkClient_->post(apiUrl, jsonBody, headers); if (!response) { ECHOION_LOG_WARNING(std::format("URL shortening failed: {}", response.error().message)); return std::unexpected(Api::Error(Api::ErrorCode::NetworkError, "Failed to shorten URL")); } if (!response->isSuccess()) { ECHOION_LOG_WARNING(std::format("URL shortening returned HTTP {}", response->statusCode)); return std::unexpected( Api::Error(Api::ErrorCode::NetworkError, std::format("Shortening service returned HTTP {}", response->statusCode))); } try { // Parse response - expecting {"shortUrl": // "https://faumaray.ru/xxxxx"} auto parsed = Void::Json::parse(response->body); if (!parsed) { ECHOION_LOG_WARNING("Failed to parse URL shortening response"); return std::unexpected( Api::Error(Api::ErrorCode::ParseError, "Failed to parse shortening response")); } std::string shortenedUrl; const Void::Json::Value* shortUrl = parsed->find("shortUrl"); if (shortUrl && shortUrl->is_string()) { shortenedUrl = shortUrl->as_string(); } else { ECHOION_LOG_WARNING("No shortUrl field in response"); return std::unexpected(Api::Error(Api::ErrorCode::ParseError, "No shortUrl in response")); } // Cache the result { std::lock_guard lock(cacheMutex_); cache_[std::string(originalUrl)] = { .shortenedUrl = shortenedUrl, .timestamp = std::chrono::system_clock::now()}; } ECHOION_LOG_DEBUG(std::format("URL shortened: {} -> {}", originalUrl, shortenedUrl)); return shortenedUrl; } catch (const std::exception& e) { ECHOION_LOG_WARNING(std::format( "Exception parsing shortening response: {}", e.what())); return std::unexpected(Api::Error( Api::ErrorCode::ParseError, "Exception during URL shortening")); } } std::optional UrlShortener::getCachedUrl(std::string_view originalUrl) const { std::lock_guard lock(cacheMutex_); auto it = cache_.find(std::string(originalUrl)); if (it != cache_.end()) { auto now = std::chrono::system_clock::now(); if (now - it->second.timestamp < cacheTtl_) { return it->second.shortenedUrl; } } return std::nullopt; } void UrlShortener::clearCache() { std::lock_guard lock(cacheMutex_); cache_.clear(); } void UrlShortener::setCacheTtl(std::chrono::hours ttl) { cacheTtl_ = ttl; } void UrlShortener::cleanExpiredCache() { std::lock_guard lock(cacheMutex_); auto now = std::chrono::system_clock::now(); for (auto it = cache_.begin(); it != cache_.end();) { if (now - it->second.timestamp >= cacheTtl_) { it = cache_.erase(it); } else { ++it; } } } } // namespace Echoion::Core