From c35dcb902ca8ee813c0128ebe4318fe8b1be4b06 Mon Sep 17 00:00:00 2001 From: Vlad Pronsky Date: Thu, 6 Jul 2023 20:26:48 +0300 Subject: [PATCH] fix: tweet_details crash when tweet not found (now is None) --- readme.md | 4 ++-- twscrape/api.py | 3 ++- twscrape/cli.py | 5 ++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index bb781a8..700421c 100644 --- a/readme.md +++ b/readme.md @@ -186,7 +186,7 @@ The default output is in the console (stdout), one document per line. So it can twscrape search "elon mask lang:es" --limit=20 > data.txt ``` -By default, parsed data is returned. The original tweet responses can be retrieved with `--raw` +By default, parsed data is returned. The original tweet responses can be retrieved with `--raw` flag. ```sh twscrape search "elon mask lang:es" --limit=20 --raw @@ -194,7 +194,7 @@ twscrape search "elon mask lang:es" --limit=20 --raw ## Limitations -NOTE: After 1 July 2023 Twitter [introduced limits](https://twitter.com/elonmusk/status/1675187969420828672) on the number of tweets per day per account (and these continue to change), so the values below may not be fully correct. +**NOTE:** After 1 July 2023 Twitter [introduced limits](https://twitter.com/elonmusk/status/1675187969420828672) on the number of tweets per day per account, so the values below may not be fully correct. API rate limits (per account): - Search API – 250 req / 15 min diff --git a/twscrape/api.py b/twscrape/api.py index ee4d577..2b457bb 100644 --- a/twscrape/api.py +++ b/twscrape/api.py @@ -152,7 +152,8 @@ class API: async def tweet_details(self, twid: int, kv=None): rep = await self.tweet_details_raw(twid, kv=kv) obj = to_old_rep(rep.json()) - return Tweet.parse(obj["tweets"][str(twid)], obj) + doc = obj["tweets"].get(str(twid), None) + return Tweet.parse(doc, obj) if doc else None # followers diff --git a/twscrape/cli.py b/twscrape/cli.py index 01431a5..1712b06 100644 --- a/twscrape/cli.py +++ b/twscrape/cli.py @@ -31,7 +31,10 @@ def get_fn_arg(args): exit(1) -def to_str(doc: httpx.Response | Tweet | User) -> str: +def to_str(doc: httpx.Response | Tweet | User | None) -> str: + if doc is None: + return "Not Found. See --raw for more details." + tmp = doc.json() return tmp if isinstance(tmp, str) else json.dumps(tmp, default=str)