diff options
-rwxr-xr-x | SiliciumCache/__init__.py | 28 | ||||
-rwxr-xr-x | main.py | 3 |
2 files changed, 23 insertions, 8 deletions
diff --git a/SiliciumCache/__init__.py b/SiliciumCache/__init__.py index 8c0cb6f..eb7f1a6 100755 --- a/SiliciumCache/__init__.py +++ b/SiliciumCache/__init__.py @@ -25,20 +25,25 @@ import pickle, datetime import Silicium +_VER = 'magick' + class CacheManager: def __init__(self, path): self.__path = path try: self.forums = pickle.load(open(self.__path, 'rb')) + self.forums except: self.forums = {} - def __refresh_forum(self, forum): - topics = [] + def save(self): + data = {'ver': _VER, 'forums': self.forums, 'topics': self.topics} + pickle.dump(self.forums, open(self.__path, 'wb')) + def __refresh_forum(self, forum): try: title = forum.get_title() except Silicium.NotEnoughPermissionsError: - return [] + return print("[f] Gathering from forum {}: '{}'".format(forum.id, title)) @@ -49,7 +54,7 @@ class CacheManager: u1 = subforum.updated if not u1 or (u0 and u0 >= u1): continue - topics.extend(self.__refresh_forum(subforum)) + self.__refresh_forum(subforum) # Check if the entry exists, create it otherwise. if not forum.id in self.forums: @@ -59,6 +64,7 @@ class CacheManager: } # Check all of the topics. + topics = [] since = self.forums[forum.id]['updated'] if not since: since = datetime.datetime(1970, 1, 1, 0, 0) for topic in forum.get_latest_topics(since): @@ -68,11 +74,19 @@ class CacheManager: # Update. self.forums[forum.id]['updated'] = forum.updated + self.topics.extend(topics) + self.save() + + def get_topic(self): + if not topics: return None + return topics.pop() + + def get_topics(self): + topics = self.topics + self.topics = [] return topics def refresh(self): - topics = self.__refresh_forum(Silicium.Forum(0)) - pickle.dump(self.forums, open(self.__path, 'wb')) - return topics + self.__refresh_forum(Silicium.Forum(0)) # End of file. @@ -37,7 +37,8 @@ if __name__ == "__main__": # Make the cache manager, refresh. cache = SiliciumCache.CacheManager(args.cache) - print(cache.refresh()) + cache.refresh() + print(cache.topics) # TODO: Mastodon and stuff. |