From b6d1abc5c28c623c23087c5062a5e4e1eb1b3b0b Mon Sep 17 00:00:00 2001 From: G D U L A <142990595+dzidulajubilee@users.noreply.github.com> Date: Sun, 15 Feb 2026 16:12:22 +0000 Subject: [PATCH] Create favicon_downloader.py This change helps fix favicon error for Newsfeed upload. --- .../newsfeed/utils/favicon_downloader.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 backend/app/features/newsfeed/utils/favicon_downloader.py diff --git a/backend/app/features/newsfeed/utils/favicon_downloader.py b/backend/app/features/newsfeed/utils/favicon_downloader.py new file mode 100644 index 0000000..612a90d --- /dev/null +++ b/backend/app/features/newsfeed/utils/favicon_downloader.py @@ -0,0 +1,30 @@ +import requests +from urllib.parse import urlparse, urljoin + + +class FaviconDownloader: + def __init__(self, timeout=5): + self.timeout = timeout + + def get_favicon(self, url: str): + """ + Return favicon URL if available, otherwise None + """ + try: + if not url.startswith(("http://", "https://")): + url = "http://" + url + + parsed = urlparse(url) + base_url = f"{parsed.scheme}://{parsed.netloc}" + favicon_url = urljoin(base_url, "/favicon.ico") + + response = requests.head(favicon_url, timeout=self.timeout) + + if response.status_code == 200: + return favicon_url + + return None + + except Exception as e: + print(f"[FaviconDownloader] Error: {e}") + return None