main: I extended the recent_user function to call on the fetch_avatar function.
user: I created and empty object avatar, and defined the function fetch_avatar. and then in the routes stored the fetch_avatar in avatar.
And adjusted the HTML's, to fetch the api function in the py file, and if that fails to return the default image.
However, because the api return an svg code to fit all sizes, i had to alter the css code for the user_page because the auto format failed the api from returning anything.
Still, the api only fetches a maximum of 10 avatars per minute. So when the limit is exceeded or the api starts to bug, the old "fallback" avatar is returned.
I tried to make an avatar_cache library to store in all the fetched avatars up to an included point, so these can be recalled upon at anytime. This should fix the limit of 10. However, I ran out of time so this is as far as I have gotten:
Initialize avatar cache dictionary
avatar_cache = {}
Function to fetch avatar for a user
def fetch_avatar(username):
Check if the avatar is already in the cache
if username in avatar_cache:
return avatar_cache[username]
# If not in cache, fetch from API
api_url = f"https://api.multiavatar.com/{username}"
response = requests.get(api_url)
if response.status_code == 200:
avatar = response.content.decode('utf-8')
# Store in cache
avatar_cache[username] = avatar
return avatar
else:
# Return default avatar if API call fails
return None
I included an api : https://github.com/multiavatar/Multiavatar to automatically fetch a deterministic avatar.
To do this I created a function in the .py files
And adjusted the HTML's, to fetch the api function in the py file, and if that fails to return the default image.
However, because the api return an svg code to fit all sizes, i had to alter the css code for the user_page because the auto format failed the api from returning anything.
Still, the api only fetches a maximum of 10 avatars per minute. So when the limit is exceeded or the api starts to bug, the old "fallback" avatar is returned. I tried to make an avatar_cache library to store in all the fetched avatars up to an included point, so these can be recalled upon at anytime. This should fix the limit of 10. However, I ran out of time so this is as far as I have gotten:
Initialize avatar cache dictionary
avatar_cache = {}
Function to fetch avatar for a user
def fetch_avatar(username):
Check if the avatar is already in the cache