Fix base definition of the watcher

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

@ -3,18 +3,35 @@ 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):
@ -22,24 +39,28 @@ class ChangeDetectionStore:
self.data = {} self.data = {}
self.data['watching'] = [] 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', 'url': 'https://changedetection.io',
'tag': 'general', 'tag': 'general',
'last_checked': 0,
'last_changed' : 0,
'uuid': str(uuid.uuid4()) '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', 'url': 'http://www.quotationspage.com/random.php',
'tag': 'test', 'tag': 'test',
'last_checked': 0,
'last_changed' : 0,
'uuid': str(uuid.uuid4()) 'uuid': str(uuid.uuid4())
}) })
self.data['watching'].append(_blank)
with open('/datastore/url-watches.json', 'w') as json_file:
json.dump(self.data, json_file)
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,7 +70,6 @@ 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...
@ -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