Skip to content

Commit

Permalink
Merge pull request #172 from bjsi/add-bookmarks-support
Browse files Browse the repository at this point in the history
feat: add support for getting bookmarks
  • Loading branch information
vladkens authored Jun 29, 2024
2 parents 09d035c + 6b67421 commit 7e82ff7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions test_bookmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import asyncio

import twscrape
from twscrape.utils import gather

async def main():
api = twscrape.API()
# add accounts here or before from cli (see README.md for examples)
await api.pool.add_account("BookmarkTe60140", "", '', '')
await api.pool.login_all()
bms = await gather(api.bookmarks())
for bm in bms:
print(bm.rawContent)
print('-----')

if __name__ == "__main__":
asyncio.run(main())
25 changes: 25 additions & 0 deletions twscrape/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
OP_BlueVerifiedFollowers = "AXsZSOWx3FCvneEIzxDj6A/BlueVerifiedFollowers"
OP_UserCreatorSubscriptions = "NHT8e7FjnCS3TP0QfP_OUQ/UserCreatorSubscriptions"
OP_UserMedia = "aQQLnkexAl5z9ec_UgbEIA/UserMedia"
OP_UserBookmarks = "yzqS_xq0glDD7YZJ2YDaiA/Bookmarks"


GQL_URL = "https://twitter.com/i/api/graphql"
Expand Down Expand Up @@ -385,6 +386,30 @@ async def user_tweets_and_replies(self, uid: int, limit=-1, kv=None):
yield x

# user_media

async def bookmarks_raw(self, limit=-1, kv=None):
op = OP_UserBookmarks
kv = {
"count": 20,
"includePromotedContent": False,
"withClientEventToken": False,
"withBirdwatchNotes": False,
"withVoice": True,
"withV2Timeline": True,
**(kv or {}),
}
ft = {
'graphql_timeline_v2_bookmark_timeline': True,
}
async with aclosing(self._gql_items(op, kv, ft, limit=limit)) as gen:
async for x in gen:
yield x

async def bookmarks(self, limit=-1, kv=None):
async with aclosing(self.bookmarks_raw(limit=limit, kv=kv)) as gen:
async for rep in gen:
for x in parse_tweets(rep.json(), limit):
yield x

async def user_media_raw(self, uid: int, limit=-1, kv=None):
op = OP_UserMedia
Expand Down

0 comments on commit 7e82ff7

Please sign in to comment.