2021-01-27 11:39:46 +00:00
|
|
|
import json
|
|
|
|
import uuid
|
2021-01-27 12:17:35 +00:00
|
|
|
import validators
|
|
|
|
|
2021-01-27 14:56:59 +00:00
|
|
|
|
|
|
|
# @TODO Have a var which is the base value, this is referred to even in the templating.. merge and append,not just append
|
2021-01-27 11:39:46 +00:00
|
|
|
# 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 :)
|
2021-01-27 12:17:35 +00:00
|
|
|
# https://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change
|
2021-01-27 11:39:46 +00:00
|
|
|
class ChangeDetectionStore:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
try:
|
|
|
|
with open('/datastore/url-watches.json') as json_file:
|
|
|
|
self.data = json.load(json_file)
|
|
|
|
for p in self.data['watching']:
|
2021-01-27 16:35:32 +00:00
|
|
|
print("Watching:", p['url'])
|
2021-01-27 11:39:46 +00:00
|
|
|
|
|
|
|
# First time ran, doesnt exist.
|
|
|
|
except (FileNotFoundError, json.decoder.JSONDecodeError):
|
|
|
|
print ("Resetting JSON store")
|
|
|
|
|
|
|
|
self.data = {}
|
|
|
|
self.data['watching'] = []
|
|
|
|
self.data['watching'].append({
|
|
|
|
'url': 'https://changedetection.io',
|
|
|
|
'tag': 'general',
|
2021-01-27 12:17:35 +00:00
|
|
|
'last_checked': 0,
|
2021-01-27 14:12:31 +00:00
|
|
|
'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,
|
2021-01-27 11:39:46 +00:00
|
|
|
'uuid': str(uuid.uuid4())
|
|
|
|
})
|
|
|
|
|
2021-01-27 14:12:31 +00:00
|
|
|
|
2021-01-27 11:39:46 +00:00
|
|
|
with open('/datastore/url-watches.json', 'w') as json_file:
|
|
|
|
json.dump(self.data, json_file)
|
|
|
|
|
2021-01-27 14:12:31 +00:00
|
|
|
def update_watch(self, uuid, val, var):
|
|
|
|
# Probably their should be dict...
|
|
|
|
for watch in self.data['watching']:
|
|
|
|
if watch['uuid'] == uuid:
|
|
|
|
watch[val] = var
|
|
|
|
# print("Updated..", val)
|
|
|
|
self.sync_to_json()
|
|
|
|
|
|
|
|
|
2021-01-27 16:35:32 +00:00
|
|
|
def url_exists(self, url):
|
|
|
|
|
|
|
|
# Probably their should be dict...
|
|
|
|
for watch in self.data['watching']:
|
|
|
|
if watch['url'] == url:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2021-01-27 14:12:31 +00:00
|
|
|
def get_val(self, uuid, val):
|
|
|
|
# Probably their should be dict...
|
|
|
|
for watch in self.data['watching']:
|
|
|
|
if watch['uuid'] == uuid:
|
|
|
|
if val in watch:
|
|
|
|
return watch[val]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return None
|
2021-01-27 11:39:46 +00:00
|
|
|
|
2021-01-27 12:17:35 +00:00
|
|
|
def add_watch(self, url, tag):
|
|
|
|
validators.url(url)
|
|
|
|
|
2021-01-27 14:12:31 +00:00
|
|
|
# @todo use a common generic version of this
|
2021-01-27 12:17:35 +00:00
|
|
|
self.data['watching'].append({
|
|
|
|
'url': url,
|
|
|
|
'tag': tag,
|
2021-01-27 14:12:31 +00:00
|
|
|
'last_checked':0,
|
|
|
|
'last_changed': 0,
|
2021-01-27 12:17:35 +00:00
|
|
|
'uuid': str(uuid.uuid4())
|
|
|
|
})
|
|
|
|
self.sync_to_json()
|
|
|
|
# @todo throw custom exception
|
2021-01-27 11:39:46 +00:00
|
|
|
|
|
|
|
def sync_to_json(self):
|
|
|
|
with open('/datastore/url-watches.json', 'w') as json_file:
|
2021-01-27 16:35:32 +00:00
|
|
|
json.dump(self.data, json_file, indent=4)
|
2021-01-27 11:39:46 +00:00
|
|
|
|
|
|
|
# body of the constructor
|