Skip to content

Commit

Permalink
Avoid KeyErrors in parsing post result
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanC committed May 4, 2024
1 parent 4ac82ad commit 26db942
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions services/x.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ def GetUserPosts(
continue

try:
result: dict = content["itemContent"]["tweet_results"]["result"]
result: dict = content["itemContent"]["tweet_results"].get("result")

if not result:
raise RuntimeError("unable to locate data in tweet_results")

if not includeReposts:
if result["legacy"].get("retweeted_status_result"):
Expand All @@ -184,7 +187,9 @@ def GetUserPosts(
continue

if onlyMedia:
if not result["legacy"]["entities"].get("media"):
legacy: dict = result.get("legacy")

if (legacy) and (not legacy["entities"].get("media")):
logger.debug(
f"[@{username}] Skipped {entry["entryId"]} due to lack of media"
)
Expand All @@ -193,7 +198,10 @@ def GetUserPosts(

# result data is sometimes within a tweet object.
if (not result.get("rest_id")) or (not result.get("legacy")):
result = result["tweet"]
result = result.get("tweet")

if not result:
raise RuntimeError("unable to locate post data in result")

posts.append(
{
Expand Down

0 comments on commit 26db942

Please sign in to comment.