mirror of
https://github.com/dgtlmoon/changedetection.io.git
synced 2025-12-12 02:55:43 +00:00
Fix base definition of the watcher
This commit is contained in:
@@ -3,43 +3,64 @@ 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):
|
||||
print ("Resetting JSON store")
|
||||
print("Resetting JSON store")
|
||||
|
||||
self.data = {}
|
||||
self.data['watching'] = []
|
||||
self.data['watching'].append({
|
||||
'url': 'https://changedetection.io',
|
||||
'tag': 'general',
|
||||
'last_checked': 0,
|
||||
'last_changed' : 0,
|
||||
'uuid': str(uuid.uuid4())
|
||||
})
|
||||
self.data['watching'].append({
|
||||
'url': 'http://www.quotationspage.com/random.php',
|
||||
'tag': 'test',
|
||||
'last_checked': 0,
|
||||
'last_changed' : 0,
|
||||
'uuid': str(uuid.uuid4())
|
||||
})
|
||||
self._init_blank_data()
|
||||
self.sync_to_json()
|
||||
|
||||
def _init_blank_data(self):
|
||||
|
||||
with open('/datastore/url-watches.json', 'w') as json_file:
|
||||
json.dump(self.data, json_file)
|
||||
# Test site
|
||||
_blank = self.generic_definition.copy()
|
||||
_blank.update({
|
||||
'url': 'https://changedetection.io',
|
||||
'tag': 'general',
|
||||
'uuid': str(uuid.uuid4())
|
||||
})
|
||||
self.data['watching'].append(_blank)
|
||||
|
||||
# Test site
|
||||
_blank = self.generic_definition.copy()
|
||||
_blank.update({
|
||||
'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):
|
||||
# Probably their should be dict...
|
||||
@@ -49,13 +70,12 @@ class ChangeDetectionStore:
|
||||
# print("Updated..", val)
|
||||
self.sync_to_json()
|
||||
|
||||
|
||||
def url_exists(self, url):
|
||||
|
||||
# Probably their should be dict...
|
||||
for watch in self.data['watching']:
|
||||
if watch['url'] == url:
|
||||
return True
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -88,4 +110,4 @@ class ChangeDetectionStore:
|
||||
with open('/datastore/url-watches.json', 'w') as json_file:
|
||||
json.dump(self.data, json_file, indent=4)
|
||||
|
||||
# body of the constructor
|
||||
# body of the constructor
|
||||
|
||||
Reference in New Issue
Block a user