You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
changedetection.io/backend/store.py

92 lines
2.9 KiB

4 years ago
import json
import uuid
4 years ago
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
4 years ago
# 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 :)
4 years ago
# https://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change
4 years ago
class ChangeDetectionStore:
def __init__(self):
try:
with open('/datastore/url-watches.json') as json_file:
self.data = json.load(json_file)
4 years ago
self.data['watching'].reverse()
4 years ago
for p in self.data['watching']:
4 years ago
print("Watching:", p['url'])
4 years ago
# 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',
4 years ago
'last_checked': 0,
4 years ago
'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,
4 years ago
'uuid': str(uuid.uuid4())
})
4 years ago
4 years ago
with open('/datastore/url-watches.json', 'w') as json_file:
json.dump(self.data, json_file)
4 years ago
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()
4 years ago
def url_exists(self, url):
# Probably their should be dict...
for watch in self.data['watching']:
if watch['url'] == url:
return True
return False
4 years ago
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
4 years ago
4 years ago
def add_watch(self, url, tag):
validators.url(url)
4 years ago
# @todo use a common generic version of this
4 years ago
self.data['watching'].append({
'url': url,
'tag': tag,
4 years ago
'last_checked':0,
'last_changed': 0,
4 years ago
'uuid': str(uuid.uuid4())
})
self.sync_to_json()
# @todo throw custom exception
4 years ago
def sync_to_json(self):
with open('/datastore/url-watches.json', 'w') as json_file:
4 years ago
json.dump(self.data, json_file, indent=4)
4 years ago
# body of the constructor