from io import BytesIO from time import sleep import pdfplumber from data.sig_ref import SIGRef from providers.sigrefdata.file_download_sig_ref_data_provider import FileDownloadSIGRefDataProvider class FEA(FileDownloadSIGRefDataProvider): """SIG ref data provider for Diploma Faros de EspaƱa""" POLL_INTERVAL_DAYS = 30 SIG = "FEA" DATA_URL = "http://ea5ol.net/Lista%20Faros.pdf" def __init__(self, provider_config): super().__init__(self.SIG, provider_config, self.DATA_URL, self.POLL_INTERVAL_DAYS) def _http_response_to_data(self, http_response): new_data = [] # Use PDFPlumber to extract the tables in the PDF with pdfplumber.open(BytesIO(http_response.content)) as pdf: all_rows = [] for page_number, page in enumerate(pdf.pages, start=1): tables = page.extract_tables() for table_number, table in enumerate(tables, start=1): if not table: continue rows = [row for row in table if any(cell and cell.strip() for cell in row)] all_rows.extend(rows) for row in all_rows: if not "REF" in row[0] and not "\n" in row[0]: # FEA references are technically [DE]\-\d{4}(\.\d)? but spotters always seem to miss out the D- or E- # prefix and just use FEA 1234, so we treat FEA reference IDs as if they were just the number. ref_id = row[0].strip().replace("D-", "").replace("E-", "") new_data.append(SIGRef(sig=self.SIG, id=ref_id, name=row[1].strip(), ref_type="Lighthouse")) # Bail out if a stop has been requested, i.e. the program is shutting down - no need to parse the rest of # the data in this case if self._stop_event.is_set(): break # Very short pause. This will extend the time to handle sig refs by a few seconds but will ensure some time # is available for other threads e.g. the web server. sleep(0.001) return new_data