Fix base definition of the watcher

pull/1/head
Leigh Morresi 4 years ago
parent c73b9efe9e
commit 57fd3a7c3c

@ -3,43 +3,64 @@ import uuid
import validators 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? # 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 :) # Open a github issue if you know something :)
# https://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change # https://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change
class ChangeDetectionStore: class ChangeDetectionStore:
def __init__(self): 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: try:
with open('/datastore/url-watches.json') as json_file: with open('/datastore/url-watches.json') as json_file:
self.data = json.load(json_file) self.data = json.load(json_file)
for p in self.data['watching']: # Reinitialise each `watching` with our generic_definition in the case that we add a new var in the future.
print("Watching:", p['url']) 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. # First time ran, doesnt exist.
except (FileNotFoundError, json.decoder.JSONDecodeError): except (FileNotFoundError, json.decoder.JSONDecodeError):
print ("Resetting JSON store") print("Resetting JSON store")
self.data = {} self.data = {}
self.data['watching'] = [] self.data['watching'] = []
self.data['watching'].append({ self._init_blank_data()
'url': 'https://changedetection.io', self.sync_to_json()
'tag': 'general',
'last_checked': 0, def _init_blank_data(self):
'last_changed' : 0,
'uuid': str(uuid.uuid4()) # Test site
}) _blank = self.generic_definition.copy()
self.data['watching'].append({ _blank.update({
'url': 'http://www.quotationspage.com/random.php', 'url': 'https://changedetection.io',
'tag': 'test', 'tag': 'general',
'last_checked': 0, 'uuid': str(uuid.uuid4())
'last_changed' : 0, })
'uuid': str(uuid.uuid4()) self.data['watching'].append(_blank)
})
# Test site
_blank = self.generic_definition.copy()
with open('/datastore/url-watches.json', 'w') as json_file: _blank.update({
json.dump(self.data, json_file) 'url': 'http://www.quotationspage.com/random.php',
'tag': 'test',
'uuid': str(uuid.uuid4())
})
self.data['watching'].append(_blank)
def update_watch(self, uuid, val, var): def update_watch(self, uuid, val, var):
# Probably their should be dict... # Probably their should be dict...
@ -49,13 +70,12 @@ class ChangeDetectionStore:
# print("Updated..", val) # print("Updated..", val)
self.sync_to_json() self.sync_to_json()
def url_exists(self, url): def url_exists(self, url):
# Probably their should be dict... # Probably their should be dict...
for watch in self.data['watching']: for watch in self.data['watching']:
if watch['url'] == url: if watch['url'] == url:
return True return True
return False return False
@ -74,13 +94,15 @@ class ChangeDetectionStore:
validators.url(url) validators.url(url)
# @todo use a common generic version of this # @todo use a common generic version of this
self.data['watching'].append({
_blank = self.generic_definition.copy()
_blank.update({
'url': url, 'url': url,
'tag': tag, 'tag': tag,
'last_checked':0,
'last_changed': 0,
'uuid': str(uuid.uuid4()) 'uuid': str(uuid.uuid4())
}) })
self.data['watching'].append(_blank)
self.sync_to_json() self.sync_to_json()
# @todo throw custom exception # @todo throw custom exception

Loading…
Cancel
Save