From ff874fe0d3f16bcbcd8fe4f62b79958882cdce04 Mon Sep 17 00:00:00 2001 From: msramalho <19508417+msramalho@users.noreply.github.com> Date: Wed, 9 Mar 2022 12:17:51 +0100 Subject: [PATCH] simplifies access to google sheets, single get_values --- auto_archive.py | 10 ++++------ utils/gworksheet.py | 9 +++++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/auto_archive.py b/auto_archive.py index b5dae88..2891ea0 100644 --- a/auto_archive.py +++ b/auto_archive.py @@ -7,6 +7,7 @@ import gspread from loguru import logger from dotenv import load_dotenv from selenium import webdriver +import traceback import archivers from storages import S3Storage, S3Config @@ -104,12 +105,10 @@ def process_sheet(sheet, header=1): archivers.WaybackArchiver(s3_client, driver) ] - values = gw.get_values() # loop through rows in worksheet for row in range(1 + header, gw.count_rows() + 1): - row_values = values[row-1] - url = gw.get_cell(row_values, 'url') - status = gw.get_cell(row_values, 'status') + url = gw.get_cell(row, 'url') + status = gw.get_cell(row, 'status') if url != '' and status in ['', None]: gw.set_cell(row, 'status', 'Archive in progress') @@ -122,8 +121,7 @@ def process_sheet(sheet, header=1): result = archiver.download(url, check_if_exists=True) except Exception as e: result = False - logger.error( - f'Got unexpected error in row {row} with archiver {archiver} for url {url}: {e}') + logger.error(f'Got unexpected error in row {row} with archiver {archiver} for url {url}: {e}\n{traceback.format_exc()}') if result: if result.status in ['success', 'already archived']: diff --git a/utils/gworksheet.py b/utils/gworksheet.py index f7f0549..e10df10 100644 --- a/utils/gworksheet.py +++ b/utils/gworksheet.py @@ -18,7 +18,8 @@ class GWorksheet: def __init__(self, worksheet, columns=COLUMN_NAMES, header_row=1): self.wks = worksheet - self.headers = [v.lower() for v in self.wks.row_values(header_row)] + self.values = self.wks.get_values() + self.headers = [v.lower() for v in self.values[header_row - 1]] self.columns = columns def _check_col_exists(self, col: str): @@ -34,14 +35,14 @@ class GWorksheet: return self.columns[col] in self.headers def count_rows(self): - return len(self.wks.get_values()) + return len(self.values) def get_row(self, row: int): # row is 1-based - return self.wks.row_values(row) + return self.values[row - 1] def get_values(self): - return self.wks.get_values() + return self.values def get_cell(self, row, col: str): """