Fix empty X search result bug (#54)
Before:  Now: 
This commit is contained in:
parent
c1a66a6170
commit
8d66b52512
1 changed files with 20 additions and 1 deletions
|
|
@ -9,7 +9,7 @@ def get_tweet_url(tweet_id: str) -> str:
|
||||||
|
|
||||||
def parse_search_recent_tweets_response(response: Response) -> str:
|
def parse_search_recent_tweets_response(response: Response) -> str:
|
||||||
"""
|
"""
|
||||||
Parse the response from the X API search recent tweets endpoint.
|
Parses response from the X API search recent tweets endpoint.
|
||||||
Returns a JSON string with the tweets data.
|
Returns a JSON string with the tweets data.
|
||||||
|
|
||||||
Example parsed response:
|
Example parsed response:
|
||||||
|
|
@ -27,7 +27,14 @@ def parse_search_recent_tweets_response(response: Response) -> str:
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
"""
|
"""
|
||||||
|
if response.status_code != 200:
|
||||||
|
return json.dumps({"tweets": []})
|
||||||
|
|
||||||
tweets_data = json.loads(response.text)
|
tweets_data = json.loads(response.text)
|
||||||
|
|
||||||
|
if not sanity_check_tweets_data(tweets_data):
|
||||||
|
return json.dumps({"tweets": []})
|
||||||
|
|
||||||
for tweet in tweets_data["data"]:
|
for tweet in tweets_data["data"]:
|
||||||
tweet["tweet_url"] = get_tweet_url(tweet["id"])
|
tweet["tweet_url"] = get_tweet_url(tweet["id"])
|
||||||
|
|
||||||
|
|
@ -38,3 +45,15 @@ def parse_search_recent_tweets_response(response: Response) -> str:
|
||||||
tweet_data["author_name"] = user_data["name"]
|
tweet_data["author_name"] = user_data["name"]
|
||||||
|
|
||||||
return json.dumps({"tweets": tweets_data["data"]})
|
return json.dumps({"tweets": tweets_data["data"]})
|
||||||
|
|
||||||
|
|
||||||
|
def sanity_check_tweets_data(tweets_data: dict) -> bool:
|
||||||
|
"""
|
||||||
|
Sanity check the tweets data.
|
||||||
|
Returns True if the tweets data is valid and contains tweets, False otherwise.
|
||||||
|
"""
|
||||||
|
if not tweets_data.get("data", []):
|
||||||
|
return False
|
||||||
|
if not tweets_data.get("includes", {}).get("users", []):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue