wrap in try/except

pull/1931/head
dgtlmoon 1 year ago
parent 73c0bd1839
commit 160901a32c

@ -137,6 +137,7 @@ class import_distill_io_json(Importer):
flash("{} Imported from Distill.io in {:.2f}s, {} Skipped.".format(len(self.new_uuids), time.time() - now, len(self.remaining_data))) flash("{} Imported from Distill.io in {:.2f}s, {} Skipped.".format(len(self.new_uuids), time.time() - now, len(self.remaining_data)))
class import_xlsx_wachete(Importer): class import_xlsx_wachete(Importer):
def run(self, def run(self,
@ -154,58 +155,65 @@ class import_xlsx_wachete(Importer):
try: try:
wb = load_workbook(data) wb = load_workbook(data)
except Exception as e: except Exception as e:
#@todo correct except # @todo correct except
flash("Unable to read export XLSX file, something wrong with the file?", 'error') flash("Unable to read export XLSX file, something wrong with the file?", 'error')
return return
row_id = 2
for row in wb.active.iter_rows(min_row=row_id):
try:
extras = {}
data = {}
for cell in row:
if not cell.value:
continue
column_title = wb.active.cell(row=1, column=cell.column).value.strip().lower()
data[column_title] = cell.value
# Forced switch to webdriver/playwright/etc
dynamic_wachet = str(data.get('dynamic wachet')).strip().lower() # Convert bool to str to cover all cases
# libreoffice and others can have it as =FALSE() =TRUE(), or bool(true)
if 'true' in dynamic_wachet or dynamic_wachet == '1':
extras['fetch_backend'] = 'html_webdriver'
if data.get('xpath'):
# @todo split by || ?
extras['include_filters'] = [data.get('xpath')]
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}
for row in wb.active.iter_rows(min_row=2): # At minimum a URL is required.
extras = {} if data.get('url'):
data = {} try:
for cell in row: validate_url(data.get('url'))
if not cell.value: except ValidationError as e:
continue print(">> import URL error", data.get('url'), str(e))
column_title = wb.active.cell(row=1, column=cell.column).value.strip().lower() # Don't bother processing anything else on this row
data[column_title] = cell.value continue
# Forced switch to webdriver/playwright/etc new_uuid = datastore.add_watch(url=data['url'].strip(),
dynamic_wachet = str(data.get('dynamic wachet')).strip().lower() # Convert bool to str to cover all cases extras=extras,
# libreoffice and others can have it as =FALSE() =TRUE(), or bool(true) tag=data.get('folder'),
if 'true' in dynamic_wachet or dynamic_wachet == '1': write_to_disk_now=False)
extras['fetch_backend'] = 'html_webdriver' if new_uuid:
# Straight into the queue.
if data.get('xpath'): self.new_uuids.append(new_uuid)
#@todo split by || ? good += 1
extras['include_filters'] = [data.get('xpath')] except Exception as e:
if data.get('name'): print(e)
extras['title'] = data.get('name').strip() flash(f"Error processing row number {row_id}, check all cell data types are correct")
if data.get('interval (min)'): else:
minutes = int(data.get('interval (min)')) row_id += 1
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'):
try:
validate_url(data.get('url'))
except ValidationError as e:
print(">> import URL error", data.get('url'), str(e))
# Don't bother processing anything else on this row
continue
new_uuid = datastore.add_watch(url=data['url'].strip(),
extras=extras,
tag=data.get('folder'),
write_to_disk_now=False)
if new_uuid:
# Straight into the queue.
self.new_uuids.append(new_uuid)
good += 1
flash( 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): class import_xlsx_custom(Importer):
def run(self, def run(self,
@ -223,61 +231,67 @@ class import_xlsx_custom(Importer):
try: try:
wb = load_workbook(data) wb = load_workbook(data)
except Exception as e: except Exception as e:
#@todo correct except # @todo correct except
flash("Unable to read export XLSX file, something wrong with the file?", 'error') flash("Unable to read export XLSX file, something wrong with the file?", 'error')
return return
# @todo cehck atleast 2 rows, same in other method # @todo cehck atleast 2 rows, same in other method
from .forms import validate_url from .forms import validate_url
try:
for row in wb.active.iter_rows(): row_i = 1
url = None for row in wb.active.iter_rows():
tags = None url = None
extras = {} tags = None
extras = {}
for cell in row:
if not self.import_profile.get(cell.col_idx): for cell in row:
continue if not self.import_profile.get(cell.col_idx):
if not cell.value: continue
continue if not cell.value:
continue
cell_map = self.import_profile.get(cell.col_idx)
cell_map = self.import_profile.get(cell.col_idx)
cell_val = str(cell.value).strip() # could be bool
cell_val = str(cell.value).strip() # could be bool
if cell_map == 'url':
url = cell.value.strip() if cell_map == 'url':
try: url = cell.value.strip()
validate_url(url) try:
except ValidationError as e: validate_url(url)
print(">> Import URL error", url, str(e)) except ValidationError as e:
# Don't bother processing anything else on this row print(">> Import URL error", url, str(e))
url = None # Don't bother processing anything else on this row
break url = None
elif cell_map == 'tag': break
tags = cell.value.strip() elif cell_map == 'tag':
elif cell_map == 'include_filters': tags = cell.value.strip()
# @todo validate? elif cell_map == 'include_filters':
extras['include_filters'] = [cell.value.strip()] # @todo validate?
elif cell_map == 'interval_minutes': extras['include_filters'] = [cell.value.strip()]
hours, minutes = divmod(int(cell_val), 60) elif cell_map == 'interval_minutes':
days, hours = divmod(hours, 24) hours, minutes = divmod(int(cell_val), 60)
weeks, days = divmod(days, 7) days, hours = divmod(hours, 24)
extras['time_between_check'] = {'weeks': weeks, 'days': days, 'hours': hours, 'minutes': minutes, 'seconds': 0} weeks, days = divmod(days, 7)
else: extras['time_between_check'] = {'weeks': weeks, 'days': days, 'hours': hours, 'minutes': minutes, 'seconds': 0}
extras[cell_map] = cell_val else:
extras[cell_map] = cell_val
# At minimum a URL is required.
if url: # At minimum a URL is required.
new_uuid = datastore.add_watch(url=url, if url:
extras=extras, new_uuid = datastore.add_watch(url=url,
tag=tags, extras=extras,
write_to_disk_now=False) tag=tags,
if new_uuid: write_to_disk_now=False)
# Straight into the queue. if new_uuid:
self.new_uuids.append(new_uuid) # Straight into the queue.
good += 1 self.new_uuids.append(new_uuid)
good += 1
except Exception as e:
print(e)
flash(f"Error processing row number {row_i}, check all cell data types are correct")
else:
row_i += 1
flash( 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))

Loading…
Cancel
Save