-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix attributeError of persona, httpxTimeout tweeter #1957
base: main
Are you sure you want to change the base?
Conversation
nquang29
commented
Mar 6, 2025
@@ -221,7 +221,7 @@ async def get_or_create_user_persona(uid: str = Depends(auth.get_current_user_ui | |||
persona_data = { | |||
'id': persona_id, | |||
'name': user.get('display_name', 'My Persona'), | |||
'username': increment_username(user.get('display_name', 'MyPersona').replace(' ', '').lower()), | |||
'username': increment_username((user.get('display_name') or 'MyPersona').replace(' ', '').lower()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is weird 🌚, please check it carefully / go deeper to increment username
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this still return 'MyPersona' if display_name is empty. I using this code to handle case display_name is None
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so you mean user.get('display_name', 'My Persona') will not return My Persona if the dict user does not have display_name ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
user = {
'uid': "aaa",
'email': "bbbb",
'display_name': None,
}
i mean with this case, user.get('display_name', 'My Persona') will not return My Persona
backend/utils/social.py
Outdated
@@ -20,7 +20,7 @@ async def get_twitter_profile(handle: str) -> Dict[str, Any]: | |||
"X-RapidAPI-Host": rapid_api_host | |||
} | |||
|
|||
async with httpx.AsyncClient() as client: | |||
async with httpx.AsyncClient(timeout=rapid_api_timeout) as client: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the default value of timeout ? and, should we use the simple request instead of asyncclient ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default value is 5s, i noticed that timeout error rarely occur (< 10 / day) so i believe increasing the timeout for this request could help reduce errors
because it just a single request so we dont need to use asyncClient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok please simplify it, use request, and do hard-code 15s timeout, we dont really need then env for this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i replaced asyncclient with simple request
@@ -69,7 +70,7 @@ async def verify_latest_tweet(username: str, handle: str) -> Dict[str, Any]: | |||
|
|||
async def upsert_persona_from_twitter_profile(username: str, handle: str, uid: str) -> Dict[str, Any]: | |||
profile = await get_twitter_profile(handle) | |||
profile['avatar'] = profile['avatar'].replace('_normal', '') | |||
profile['avatar'] = (profile.get('avatar') or '').replace('_normal', '') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't you use profile.get('avatar', <default>) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if avartar = None -> profile.get('avatar', '') will return None, which can't be used with .replace
so i use (profile.get('avatar') or '') to ensure string always return for using .replace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weird, please double check it. the bellow code is from my playground :)
>>> a = {}
>>> print(a.get('x'))
None
>>> print(a.get('x', 'default'))
default
>>> print(a.get('x', 'default').replace('d', 'a'))
aefault
>>>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i mean this case: a = {'x': None}
> print(a.get('x', 'default').replace('d', 'a'))
Traceback (most recent call last):
File "/tmp/1741493933617c793c7bde2bf4/code/main.py", line 25, in <module>
print(a.get('x', 'default').replace('d', 'a'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'replace'