Files
EchoionCore/include/echoion/core/UrlShortener.hpp
T
2026-02-03 05:07:15 +03:00

51 lines
1.4 KiB
C++

#pragma once
#include <echoion/api.hpp>
#include "NetworkClient.hpp"
#include <string>
#include <optional>
#include <mutex>
#include <unordered_map>
#include <chrono>
namespace Echoion::Core {
// URL shortener service for faumaray.ru
class UrlShortener {
public:
explicit UrlShortener(std::shared_ptr<NetworkClient> networkClient);
~UrlShortener();
// Shorten a YouTube URL via faumaray.ru
// Returns shortened URL in format: https://faumaray.ru/xxxxx
[[nodiscard]] Api::Result<std::string> shortenYouTubeUrl(std::string_view videoId);
// Shorten any URL via faumaray.ru
[[nodiscard]] Api::Result<std::string> shortenUrl(std::string_view originalUrl);
// Get cached shortened URL if available
[[nodiscard]] std::optional<std::string> getCachedUrl(std::string_view originalUrl) const;
// Clear the URL cache
void clearCache();
// Set cache TTL (default: 24 hours)
void setCacheTtl(std::chrono::hours ttl);
private:
struct CacheEntry {
std::string shortenedUrl;
std::chrono::system_clock::time_point timestamp;
};
std::shared_ptr<NetworkClient> networkClient_;
mutable std::mutex cacheMutex_;
std::unordered_map<std::string, CacheEntry> cache_;
std::chrono::hours cacheTtl_{24};
// Clean expired entries from cache
void cleanExpiredCache();
};
} // namespace Echoion::Core