Skip to content

Commit

Permalink
fix attributeError of persona, httpxTimeout tweeter (#1957)
Browse files Browse the repository at this point in the history
```
File "/app/utils/social.py", line 118, in add_twitter_to_persona
AttributeError: 'NoneType' object has no attribute 'replace'
```

```
 File "/app/utils/social.py", line 72, in upsert_persona_from_twitter_profile
AttributeError: 'NoneType' object has no attribute 'replace'
```

 ```
File "/app/routers/apps.py", line 224, in get_or_create_user_persona
AttributeError: 'NoneType' object has no attribute 'replace'
```

```
File "/app/utils/social.py", line 67, in verify_latest_tweet
UnboundLocalError: cannot access local variable 'latest_tweet' where it
is not associated with a value
```

```
File "/app/utils/social.py", line 24, in get_twitter_profile
    response = await client.get(url, headers=headers)
httpx.ReadTimeout
```
  • Loading branch information
beastoin authored Mar 11, 2025
2 parents cc722b2 + 03afd71 commit df577ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion backend/routers/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,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()),
'description': f"This is {user.get('display_name', 'my')} personal AI clone.",
'image': '', # Empty image as specified in the task
'uid': uid,
Expand Down
49 changes: 23 additions & 26 deletions backend/utils/social.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
rapid_api_host = os.getenv('RAPID_API_HOST')
rapid_api_key = os.getenv('RAPID_API_KEY')

defaultTimeoutSec = 15

async def get_twitter_profile(handle: str) -> Dict[str, Any]:
url = f"https://{rapid_api_host}/screenname.php?screenname={handle}"
Expand All @@ -20,12 +21,10 @@ async def get_twitter_profile(handle: str) -> Dict[str, Any]:
"X-RapidAPI-Host": rapid_api_host
}

async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers)
response.raise_for_status()
data = response.json()
return data

response = httpx.get(url, headers=headers, timeout=defaultTimeoutSec)
response.raise_for_status()
data = response.json()
return data

async def get_twitter_timeline(handle: str) -> Dict[str, Any]:
print(f"Fetching Twitter timeline for {handle}...")
Expand All @@ -36,12 +35,10 @@ async def get_twitter_timeline(handle: str) -> Dict[str, Any]:
"X-RapidAPI-Host": rapid_api_host
}

async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers)
response.raise_for_status()
data = response.json()
return data

response = httpx.get(url, headers=headers, timeout=defaultTimeoutSec)
response.raise_for_status()
data = response.json()
return data

async def verify_latest_tweet(username: str, handle: str) -> Dict[str, Any]:
print(f"Fetching latest tweet for {handle}, username {username}...")
Expand All @@ -52,24 +49,24 @@ async def verify_latest_tweet(username: str, handle: str) -> Dict[str, Any]:
"X-RapidAPI-Host": rapid_api_host
}

async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers)
response.raise_for_status()
data = response.json()
# from the timeline, the first tweet is the latest
timeline = data.get('timeline', [])
if len(timeline) > 0:
latest_tweet = timeline[0]
# check if latest_tweet['text'] contains the word "verifying my clone"
if f'Verifying my clone({username})' in latest_tweet['text']:
return {"tweet": latest_tweet['text'], 'verified': True}
response = httpx.get(url, headers=headers, timeout=defaultTimeoutSec)
response.raise_for_status()
data = response.json()
# from the timeline, the first tweet is the latest
latest_tweet = None
timeline = data.get('timeline', [])
if len(timeline) > 0:
latest_tweet = timeline[0]
# check if latest_tweet['text'] contains the word "verifying my clone"
if f'Verifying my clone({username})' in latest_tweet['text']:
return {"tweet": latest_tweet['text'], 'verified': True}

return {"tweet": latest_tweet['text'], 'verified': False}
return {"tweet": latest_tweet['text'], 'verified': False}


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', '')
persona = get_persona_by_username_twitter_handle_db(username, handle)

if not persona:
Expand Down Expand Up @@ -115,7 +112,7 @@ async def upsert_persona_from_twitter_profile(username: str, handle: str, uid: s
async def add_twitter_to_persona(handle: str, persona_id) -> Dict[str, Any]:
persona = get_persona_by_id_db(persona_id)
twitter = await get_twitter_profile(handle)
twitter['avatar'] = twitter['avatar'].replace('_normal', '')
twitter['avatar'] = (twitter.get('avatar') or '').replace('_normal', '')
if 'twitter' not in persona['connected_accounts']:
persona['connected_accounts'].append('twitter')
persona['twitter'] = {
Expand Down

0 comments on commit df577ef

Please sign in to comment.