aboutsummaryrefslogtreecommitdiff
path: root/SiliciumCache/__init__.py
blob: 44e2b518bad8a15bc3625dea695c675cab26489f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#******************************************************************************
# Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.
#******************************************************************************
""" Cache management for the Silicium Bot.
	This is the main object when interacting with the site's data.
"""

import pickle, datetime
import Silicium

class CacheManager:
	def __init__(self, path):
		self.__path = path
		try:
			self.forums = pickle.load(open(self.__path, 'rb'))
		except: self.forums = {}

	def __refresh_forum(self, forum):
		topics = []

		try: title = forum.get_title()
		except NotEnoughPermissionsError: return []

		print("[f] Gathering from forum {}: '{}'".format(forum.id, title))

		# Refresh subforums.
		for subforum in forum.get_forums():
			if subforum.id in self.forums:
				u0 = self.forums[subforum.id]['updated']
				u1 = subforum.updated
				if not u1 or (u0 and u0 >= u1):
					continue
			topics.extend(self.__refresh_forum(subforum))

		# Check if the entry exists, create it otherwise.
		if not forum.id in self.forums:
			self.forums[forum.id] = {
				'updated': None,
				'topics': []
			}

		# Check all of the 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):
			if not topic.id in self.forums[forum.id]['topics']:
				topics.append(topic)
				self.forums[forum.id]['topics'].append(topic.id)

		# Update.
		self.forums[forum.id]['updated'] = forum.updated
		return topics

	def refresh(self):
		topics = self.__refresh_forum(Silicium.Forum(0))
		pickle.dump(self.forums, open(self.__path, 'wb'))
		return topics

# End of file.