@ -2,11 +2,14 @@ import base64
from bs4 import BeautifulSoup as bsoup
from flask import Request
import hashlib
import io
import os
import re
from requests import exceptions , get
from urllib . parse import urlparse
ddg_favicon_site = ' http://icons.duckduckgo.com/ip2 '
empty_gif = base64 . b64decode (
' R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== ' )
@ -21,6 +24,29 @@ placeholder_img = base64.b64decode(
)
def fetch_favicon ( url : str ) - > bytes :
""" Fetches a favicon using DuckDuckGo ' s favicon retriever
Args :
url : The url to fetch the favicon from
Returns :
bytes - the favicon bytes , or a placeholder image if one
was not returned
"""
domain = urlparse ( url ) . netloc
response = get ( f ' { ddg_favicon_site } / { domain } .ico ' )
if response . status_code == 200 and len ( response . content ) > 0 :
tmp_mem = io . BytesIO ( )
tmp_mem . write ( response . content )
tmp_mem . seek ( 0 )
return tmp_mem . read ( )
else :
return placeholder_img
def gen_file_hash ( path : str , static_file : str ) - > str :
file_contents = open ( os . path . join ( path , static_file ) , ' rb ' ) . read ( )
file_hash = hashlib . md5 ( file_contents ) . hexdigest ( ) [ : 8 ]