|
|
|
@ -3,18 +3,35 @@ import uuid
|
|
|
|
|
import validators
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# @TODO Have a var which is the base value, this is referred to even in the templating.. merge and append,not just append
|
|
|
|
|
# Is there an existing library to ensure some data store (JSON etc) is in sync with CRUD methods?
|
|
|
|
|
# Open a github issue if you know something :)
|
|
|
|
|
# https://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change
|
|
|
|
|
class ChangeDetectionStore:
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
|
|
# Base definition for all watchers
|
|
|
|
|
self.generic_definition = {
|
|
|
|
|
'url': None,
|
|
|
|
|
'tag': None,
|
|
|
|
|
'last_checked': 0,
|
|
|
|
|
'last_changed': 0,
|
|
|
|
|
'title': None,
|
|
|
|
|
'uuid': str(uuid.uuid4())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with open('/datastore/url-watches.json') as json_file:
|
|
|
|
|
self.data = json.load(json_file)
|
|
|
|
|
for p in self.data['watching']:
|
|
|
|
|
print("Watching:", p['url'])
|
|
|
|
|
# Reinitialise each `watching` with our generic_definition in the case that we add a new var in the future.
|
|
|
|
|
i = 0
|
|
|
|
|
while i < len(self.data['watching']):
|
|
|
|
|
_blank = self.generic_definition.copy()
|
|
|
|
|
_blank.update(self.data['watching'][i])
|
|
|
|
|
self.data['watching'][i] = _blank
|
|
|
|
|
|
|
|
|
|
print("Watching:", self.data['watching'][i]['url'])
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
# First time ran, doesnt exist.
|
|
|
|
|
except (FileNotFoundError, json.decoder.JSONDecodeError):
|
|
|
|
@ -22,24 +39,28 @@ class ChangeDetectionStore:
|
|
|
|
|
|
|
|
|
|
self.data = {}
|
|
|
|
|
self.data['watching'] = []
|
|
|
|
|
self.data['watching'].append({
|
|
|
|
|
self._init_blank_data()
|
|
|
|
|
self.sync_to_json()
|
|
|
|
|
|
|
|
|
|
def _init_blank_data(self):
|
|
|
|
|
|
|
|
|
|
# Test site
|
|
|
|
|
_blank = self.generic_definition.copy()
|
|
|
|
|
_blank.update({
|
|
|
|
|
'url': 'https://changedetection.io',
|
|
|
|
|
'tag': 'general',
|
|
|
|
|
'last_checked': 0,
|
|
|
|
|
'last_changed' : 0,
|
|
|
|
|
'uuid': str(uuid.uuid4())
|
|
|
|
|
})
|
|
|
|
|
self.data['watching'].append({
|
|
|
|
|
self.data['watching'].append(_blank)
|
|
|
|
|
|
|
|
|
|
# Test site
|
|
|
|
|
_blank = self.generic_definition.copy()
|
|
|
|
|
_blank.update({
|
|
|
|
|
'url': 'http://www.quotationspage.com/random.php',
|
|
|
|
|
'tag': 'test',
|
|
|
|
|
'last_checked': 0,
|
|
|
|
|
'last_changed' : 0,
|
|
|
|
|
'uuid': str(uuid.uuid4())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open('/datastore/url-watches.json', 'w') as json_file:
|
|
|
|
|
json.dump(self.data, json_file)
|
|
|
|
|
self.data['watching'].append(_blank)
|
|
|
|
|
|
|
|
|
|
def update_watch(self, uuid, val, var):
|
|
|
|
|
# Probably their should be dict...
|
|
|
|
@ -49,7 +70,6 @@ class ChangeDetectionStore:
|
|
|
|
|
# print("Updated..", val)
|
|
|
|
|
self.sync_to_json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def url_exists(self, url):
|
|
|
|
|
|
|
|
|
|
# Probably their should be dict...
|
|
|
|
@ -74,13 +94,15 @@ class ChangeDetectionStore:
|
|
|
|
|
validators.url(url)
|
|
|
|
|
|
|
|
|
|
# @todo use a common generic version of this
|
|
|
|
|
self.data['watching'].append({
|
|
|
|
|
|
|
|
|
|
_blank = self.generic_definition.copy()
|
|
|
|
|
_blank.update({
|
|
|
|
|
'url': url,
|
|
|
|
|
'tag': tag,
|
|
|
|
|
'last_checked':0,
|
|
|
|
|
'last_changed': 0,
|
|
|
|
|
'uuid': str(uuid.uuid4())
|
|
|
|
|
})
|
|
|
|
|
self.data['watching'].append(_blank)
|
|
|
|
|
|
|
|
|
|
self.sync_to_json()
|
|
|
|
|
# @todo throw custom exception
|
|
|
|
|
|
|
|
|
|