From 54c79665a98e0457c80c022b53b42c3446602633 Mon Sep 17 00:00:00 2001 From: Vlad Pronsky Date: Sat, 29 Jun 2024 17:32:30 +0300 Subject: [PATCH] fix bookmarks --- test_bookmarks.py | 17 -------------- twscrape/api.py | 59 +++++++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 42 deletions(-) delete mode 100644 test_bookmarks.py diff --git a/test_bookmarks.py b/test_bookmarks.py deleted file mode 100644 index 5b97b69..0000000 --- a/test_bookmarks.py +++ /dev/null @@ -1,17 +0,0 @@ -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()) \ No newline at end of file diff --git a/twscrape/api.py b/twscrape/api.py index a2760fd..4f47821 100644 --- a/twscrape/api.py +++ b/twscrape/api.py @@ -121,7 +121,14 @@ class API: obj = rep.json() els = get_by_path(obj, "entries") or [] - els = [x for x in els if not (x["entryId"].startswith("cursor-") or x["entryId"].startswith("messageprompt-"))] + els = [ + x + for x in els + if not ( + x["entryId"].startswith("cursor-") + or x["entryId"].startswith("messageprompt-") + ) + ] cur = self._get_cursor(obj, cursor_type) rep, cnt, active = self._is_end(rep, queue, els, cur, cnt, limit) @@ -386,30 +393,6 @@ class API: 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 @@ -478,3 +461,29 @@ class API: async for rep in gen: for x in parse_tweets(rep.json(), limit): yield x + + # Get current user bookmarks + + 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