diff --git a/changedetectionio/importer.py b/changedetectionio/importer.py index 565c23ad1..963482de1 100644 --- a/changedetectionio/importer.py +++ b/changedetectionio/importer.py @@ -174,8 +174,15 @@ class import_xlsx_wachete(Importer): if data.get('xpath'): #@todo split by || ? extras['include_filters'] = [data.get('xpath')] - if data.get('title'): - extras['title'] = [data.get('title').strip()] + if data.get('name'): + extras['title'] = [data.get('name').strip()] + if data.get('interval (min)'): + minutes = int(data.get('interval (min)')) + hours, minutes = divmod(minutes, 60) + days, hours = divmod(hours, 24) + weeks, days = divmod(days, 7) + extras['time_between_check'] = {'weeks': weeks, 'days': days, 'hours': hours, 'minutes': minutes, 'seconds': 0} + # At minimum a URL is required. if data.get('url'): @@ -200,7 +207,7 @@ class import_xlsx_wachete(Importer): flash( - "{} Imported from Wachete .xlsx in {:.2f}s".format(len(self.new_uuids), time.time() - now)) + "{} imported from Wachete .xlsx in {:.2f}s".format(len(self.new_uuids), time.time() - now)) class import_xlsx_custom(Importer): @@ -270,4 +277,4 @@ class import_xlsx_custom(Importer): row += 1 flash( - "{} Imported from custom .xlsx in {:.2f}s".format(len(self.new_uuids), time.time() - now)) + "{} imported from custom .xlsx in {:.2f}s".format(len(self.new_uuids), time.time() - now)) diff --git a/changedetectionio/store.py b/changedetectionio/store.py index e476afae5..6306a3913 100644 --- a/changedetectionio/store.py +++ b/changedetectionio/store.py @@ -360,6 +360,8 @@ class ChangeDetectionStore: if write_to_disk_now: self.sync_to_json() + print("added ", url) + return new_uuid def visualselector_data_is_ready(self, watch_uuid): diff --git a/changedetectionio/tests/import/spreadsheet.xlsx b/changedetectionio/tests/import/spreadsheet.xlsx new file mode 100644 index 000000000..600847b89 Binary files /dev/null and b/changedetectionio/tests/import/spreadsheet.xlsx differ diff --git a/changedetectionio/tests/test_import.py b/changedetectionio/tests/test_import.py index a044ff793..4772226c7 100644 --- a/changedetectionio/tests/test_import.py +++ b/changedetectionio/tests/test_import.py @@ -1,16 +1,19 @@ #!/usr/bin/python3 - +import io +import os import time from flask import url_for -from .util import live_server_setup +from .util import live_server_setup, wait_for_all_checks + + def test_setup(client, live_server): live_server_setup(live_server) def test_import(client, live_server): # Give the endpoint time to spin up - time.sleep(1) + wait_for_all_checks(client) res = client.post( url_for("import_page"), @@ -119,3 +122,82 @@ def test_import_distillio(client, live_server): res = client.get(url_for("form_delete", uuid="all"), follow_redirects=True) # Clear flask alerts res = client.get(url_for("index")) + +def test_import_custom_xlsx(client, live_server): + """Test can upload a excel spreadsheet and the watches are created correctly""" + + #live_server_setup(live_server) + dirname = os.path.dirname(__file__) + filename = os.path.join(dirname, 'import/spreadsheet.xlsx') + with open(filename, 'rb') as f: + + data= { + 'file_mapping': 'custom', + 'custom_xlsx[col_0]': '1', + 'custom_xlsx[col_1]': '3', + 'custom_xlsx[col_2]': '5', + 'custom_xlsx[col_3]': '4', + 'custom_xlsx[col_type_0]': 'title', + 'custom_xlsx[col_type_1]': 'url', + 'custom_xlsx[col_type_2]': 'include_filters', + 'custom_xlsx[col_type_3]': 'interval_minutes', + 'xlsx_file': (io.BytesIO(f.read()), 'spreadsheet.xlsx') + } + + res = client.post( + url_for("import_page"), + data=data, + follow_redirects=True, + ) + + assert b'2 imported from custom .xlsx' in res.data + + res = client.get( + url_for("index") + ) + + + assert b'Somesite results ABC' in res.data + assert b'City news results' in res.data + + # Just find one to check over + for uuid, watch in live_server.app.config['DATASTORE'].data['watching'].items(): + if watch.get('title') == 'Somesite results ABC': + filters = watch.get('include_filters') + assert filters[0] == '/html[1]/body[1]/div[4]/div[1]/div[1]/div[1]||//*[@id=\'content\']/div[3]/div[1]/div[1]||//*[@id=\'content\']/div[1]' + assert watch.get('time_between_check') == {'weeks': 0, 'days': 1, 'hours': 6, 'minutes': 24, 'seconds': 0} + +def test_import_watchete_xlsx(client, live_server): + """Test can upload a excel spreadsheet and the watches are created correctly""" + + #live_server_setup(live_server) + dirname = os.path.dirname(__file__) + filename = os.path.join(dirname, 'import/spreadsheet.xlsx') + with open(filename, 'rb') as f: + + data= { + 'file_mapping': 'wachete', + 'xlsx_file': (io.BytesIO(f.read()), 'spreadsheet.xlsx') + } + + res = client.post( + url_for("import_page"), + data=data, + follow_redirects=True, + ) + + assert b'2 imported from Wachete .xlsx' in res.data + + res = client.get( + url_for("index") + ) + + assert b'Somesite results ABC' in res.data + assert b'City news results' in res.data + + # Just find one to check over + for uuid, watch in live_server.app.config['DATASTORE'].data['watching'].items(): + if watch.get('title') == 'Somesite results ABC': + filters = watch.get('include_filters') + assert filters[0] == '/html[1]/body[1]/div[4]/div[1]/div[1]/div[1]||//*[@id=\'content\']/div[3]/div[1]/div[1]||//*[@id=\'content\']/div[1]' + assert watch.get('time_between_check') == {'weeks': 0, 'days': 1, 'hours': 6, 'minutes': 24, 'seconds': 0}