aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas "Cakeisalie5" Touhey <thomas@touhey.fr>2017-08-05 03:09:53 +0200
committerThomas "Cakeisalie5" Touhey <thomas@touhey.fr>2017-08-05 03:09:53 +0200
commitc26b71c66d42d040857e7da74f0506d03ba0b148 (patch)
tree338b0f6b940198415a938680c4976a10e1eaf642
parent904021c53a51b3ae1a563bb8ba618e147c0ca30f (diff)
Smarter cache (once a forum is made, even if another one doesn't work, it will be saved)
-rwxr-xr-xSiliciumCache/__init__.py28
-rwxr-xr-xmain.py3
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.
diff --git a/main.py b/main.py
index 16b4ff3..20fb9f2 100755
--- a/main.py
+++ b/main.py
@@ -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.