Skip to content

Commit

Permalink
add pinned tweets info to user profile #201
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Jun 29, 2024
1 parent fe18dd1 commit 664114e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ def check_user(doc: User):

assert doc.username is not None
assert doc.descriptionLinks is not None
assert doc.pinnedIds is not None
if doc.pinnedIds:
for x in doc.pinnedIds:
assert isinstance(x, int)

if len(doc.descriptionLinks) > 0:
for x in doc.descriptionLinks:
Expand Down Expand Up @@ -311,8 +315,12 @@ async def test_user_tweets():
tweets = await gather(api.user_tweets(2244994945))
assert len(tweets) > 0

is_any_pinned = False
for doc in tweets:
check_tweet(doc)
is_any_pinned = is_any_pinned or doc.id in doc.user.pinnedIds

assert is_any_pinned, "at least one tweet should be pinned (or bad luck with data)"


async def test_user_tweets_and_replies():
Expand Down
2 changes: 2 additions & 0 deletions twscrape/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class User(JSONTrait):
blue: bool | None = None
blueType: str | None = None
descriptionLinks: list[TextLink] = field(default_factory=list)
pinnedIds: list[int] = field(default_factory=list)
_type: str = "snscrape.modules.twitter.User"

# todo:
Expand Down Expand Up @@ -153,6 +154,7 @@ def parse(obj: dict, res=None):
blueType=obj.get("verified_type"),
protected=obj.get("protected"),
descriptionLinks=_parse_links(obj, ["entities.description.urls", "entities.url.urls"]),
pinnedIds=[int(x) for x in obj.get("pinned_tweet_ids_str", [])],
)


Expand Down
2 changes: 1 addition & 1 deletion twscrape/queue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ async def req(self, method: str, url: str, params: ReqParams = None) -> Response
msg = [
"Unknown error. Account timeouted for 15 minutes.",
"Create issue please: https://github.com/vladkens/twscrape/issues",
f"If it mistake, you can unlock account now with `twscrape reset_locks`. Err: {type(e)}: {e}",
f"If it mistake, you can unlock accounts with `twscrape reset_locks`. Err: {type(e)}: {e}",
]

logger.warning(" ".join(msg))
Expand Down

0 comments on commit 664114e

Please sign in to comment.