diff --git a/changedetectionio/__init__.py b/changedetectionio/__init__.py index 7f8c1a69..44b8710f 100644 --- a/changedetectionio/__init__.py +++ b/changedetectionio/__init__.py @@ -186,16 +186,17 @@ def changedetection_app(config=None, datastore_o=None): watch_api.add_resource(api_v1.WatchSingleHistory, '/api/v1/watch//history/', - resource_class_kwargs={'datastore': datastore}) + resource_class_kwargs={'datastore': datastore, 'update_q': update_q}) watch_api.add_resource(api_v1.WatchHistory, '/api/v1/watch//history', resource_class_kwargs={'datastore': datastore}) watch_api.add_resource(api_v1.CreateWatch, '/api/v1/watch', - resource_class_kwargs={'datastore': datastore}) + resource_class_kwargs={'datastore': datastore, 'update_q': update_q}) - watch_api.add_resource(api_v1.Watch, '/api/v1/watch/', resource_class_kwargs={'datastore': datastore}) + watch_api.add_resource(api_v1.Watch, '/api/v1/watch/', + resource_class_kwargs={'datastore': datastore, 'update_q': update_q}) @@ -389,6 +390,8 @@ def changedetection_app(config=None, datastore_o=None): if limit_tag != None: # Support for comma separated list of tags. + if watch['tag'] is None: + continue for tag_in_watch in watch['tag'].split(','): tag_in_watch = tag_in_watch.strip() if tag_in_watch == limit_tag: diff --git a/changedetectionio/api_v1.py b/changedetectionio/api_v1.py index 65906545..91a483f8 100644 --- a/changedetectionio/api_v1.py +++ b/changedetectionio/api_v1.py @@ -6,12 +6,24 @@ class Watch(Resource): def __init__(self, **kwargs): # datastore is a black box dependency self.datastore = kwargs['datastore'] + self.update_q = kwargs['update_q'] + # Get information about a single watch, excluding the history list (can be large) + # curl http://localhost:4000/api/v1/watch/ + # ?recheck=true def get(self, uuid): - watch = self.datastore.data['watching'].get(uuid) + from copy import deepcopy + watch = deepcopy(self.datastore.data['watching'].get(uuid)) if not watch: abort(404, message='No watch exists with the UUID of {}'.format(uuid)) + if request.args.get('recheck'): + self.update_q.put(uuid) + return "OK", 200 + + # Return without history, get that via another API call + watch['history_n'] = len(watch['history']) + del (watch['history']) return watch def delete(self, uuid): @@ -29,15 +41,14 @@ class WatchHistory(Resource): # datastore is a black box dependency self.datastore = kwargs['datastore'] - def get(self, uuid, timestamp): + # Get a list of available history for a watch by UUID + # curl http://localhost:4000/api/v1/watch//history + def get(self, uuid): watch = self.datastore.data['watching'].get(uuid) if not watch: abort(404, message='No watch exists with the UUID of {}'.format(uuid)) + return watch['history'], 200 - def delete(self, timestamp): - # Delete all history by timestamp or 'all' - return '', 204 - return watch class WatchSingleHistory(Resource): def __init__(self, **kwargs): @@ -62,11 +73,29 @@ class CreateWatch(Resource): def __init__(self, **kwargs): # datastore is a black box dependency self.datastore = kwargs['datastore'] + self.update_q = kwargs['update_q'] def post(self): - # "Fields" for validation? - tag = request.form.get('tag', '') - new_uuid = self.datastore.add_watch(url=request.form.get('url').strip(), tag=tag) + # curl http://localhost:4000/api/v1/watch -H "Content-Type: application/json" -d '{"url": "https://my-nice.com", "tag": "one, two" }' + json_data = request.get_json() + tag = json_data['tag'].strip() if json_data.get('tag') else '' + new_uuid = self.datastore.add_watch(url=json_data['url'].strip(), tag=tag) return new_uuid, 201 - + # Return concise list of available watches and some very basic info + # curl http://localhost:4000/api/v1/watch|python -mjson.tool + # ?recheck=all to recheck all + def get(self): + list = {} + for k, v in self.datastore.data['watching'].items(): + list[k] = {'url': v['url'], + 'title': v['title'], + 'last_checked': v['last_checked'], + 'last_changed': v['last_changed']} + + if request.args.get('recheck'): + for uuid in self.datastore.data['watching'].items(): + self.update_q.put(uuid) + return "OK", 200 + + return list, 200