호그와트

윌의 마음을 이해하는 건 니체를 읽으며 타노스 고기를 뜯어먹을때 온다

영웅*^%&$ 2024. 8. 3. 15:51
728x90
import hashlib

class URLShortener:
    def __init__(self):
        self.url_map = {}
        self.short_to_long = {}
        self.base_url = "http://thanks.ly/"

    def _hash_url(self, url):
        # Generate a hash for the given URL
        return hashlib.md5(url.encode()).hexdigest()[:6]

    def shorten_url(self, url: str) -> str:
        # Check if the URL has already been shortened
        if url in self.url_map:
            return self.url_map[url]
       
        # Generate a short URL
        short_hash = self._hash_url(url)
        short_url = self.base_url + short_hash
       
        # Store the mappings
        self.url_map[url] = short_url
        self.short_to_long[short_url] = url
       
        return short_url

    def retrieve_url(self, short_url: str) -> str:
        # Retrieve the original URL from the shortened version
        return self.short_to_long.get(short_url, "URL not found")

# Example usage
url_shortener = URLShortener()

short_url = url_shortener.shorten_url(long_url)
print("Shortened URL:", short_url)  

original_url = url_shortener.retrieve_url(short_url)
print("Original URL:", original_url)  
728x90