From ff416c9c3daa75ac5c65e6851c2818225bef25b4 Mon Sep 17 00:00:00 2001 From: Mujy Date: Sun, 27 Jun 2021 12:02:54 -0400 Subject: [PATCH] Added option for loading older posts --- README.md | 3 +++ Weverse/weverseasync.py | 25 +++++++++++++++++++------ Weverse/weversesync.py | 25 ++++++++++++++++++++----- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 85c6cdf..0e27605 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ ### What is it? Weverse creates internal cache for the communities a user follows on [weverse.io](https://www.weverse.io/). +This is a **wrapper** for Weverse's private API, but will be referred to as an API on this repository. ### **[API Documentation](https://weverse.readthedocs.io/en/latest/)** @@ -70,6 +71,8 @@ except InvalidToken: print("Invalid Token") # After calling the start method, you now have all the objects you would want to modify. +# The start method takes in parameters that can disable old posts from loading up +# if only the newer posts are wanted. More info on the documentation. ``` diff --git a/Weverse/weverseasync.py b/Weverse/weverseasync.py index d715735..322e1a2 100644 --- a/Weverse/weverseasync.py +++ b/Weverse/weverseasync.py @@ -23,7 +23,6 @@ class WeverseClientAsync(WeverseClient): loop: Asyncio Event Loop - Attributes are the same as :ref:`WeverseClient`. """ @@ -31,13 +30,16 @@ def __init__(self, loop=get_event_loop(), **kwargs): self.loop = loop super().__init__(**kwargs) - async def start(self): + async def start(self, create_old_posts=True, create_notifications=True): """Creates internal cache. This is the main process that should be run. This is a coroutine and must be awaited. + :parameter create_old_posts: (:class:`bool`) Whether to create cache for old posts. + :parameter create_notifications: (:class:`bool`) Whether to create/update cache for old notifications. + :raises: :class:`Weverse.error.PageNotFound` :raises: :class:`Weverse.error.InvalidToken` :raises: :class:`Weverse.error.BeingRateLimited` @@ -45,11 +47,22 @@ async def start(self): try: if not self.web_session: self.web_session = aiohttp.ClientSession() - await self.create_communities() + + # create all communities that are subscribed to + await self.create_communities() # communities should be created no matter what + + # create and update community artists and their tabs await self.create_community_artists_and_tabs() - await self.get_user_notifications() - for community in self.all_communities.values(): - await self.create_posts(community) + + # create and update user notifications + if create_notifications: + await self.get_user_notifications() + + # load up posts + if create_old_posts: + for community in self.all_communities.values(): + await self.create_posts(community) + self.cache_loaded = True except Exception as err: raise err diff --git a/Weverse/weversesync.py b/Weverse/weversesync.py index bea9528..1dd5572 100644 --- a/Weverse/weversesync.py +++ b/Weverse/weversesync.py @@ -20,11 +20,15 @@ class WeverseClientSync(WeverseClient): def __init__(self, **kwargs): super().__init__(**kwargs) - def start(self): + def start(self, create_old_posts=True, create_notifications=True): """Creates internal cache. This is the main process that should be run. + :parameter create_old_posts: (:class:`bool`) Whether to create cache for old posts. + :parameter create_notifications: (:class:`bool`) Whether to create/update cache for old notifications. + + :raises: :class:`Weverse.error.PageNotFound` :raises: :class:`Weverse.error.InvalidToken` :raises: :class:`Weverse.error.BeingRateLimited` @@ -32,11 +36,22 @@ def start(self): try: if not self.web_session: self.web_session = requests.Session() - self.create_communities() + + # create all communities that are subscribed to + self.create_communities() # communities should be created no matter what + + # create and update community artists and their tabs self.create_community_artists_and_tabs() - self.get_user_notifications() - for community in self.all_communities.values(): - self.create_posts(community) + + # create and update user notifications + if create_notifications: + self.get_user_notifications() + + # load up posts + if create_old_posts: + for community in self.all_communities.values(): + self.create_posts(community) + self.cache_loaded = True except Exception as err: raise err