From 450ec331014f7a7cf9a3850629779ea220f0d043 Mon Sep 17 00:00:00 2001 From: Tour Date: Sun, 7 Dec 2025 10:21:47 +0100 Subject: [PATCH] fix-os-error-after-completion --- src/scraper.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/scraper.py b/src/scraper.py index 97641ea..6008980 100644 --- a/src/scraper.py +++ b/src/scraper.py @@ -2,6 +2,7 @@ """ Core scaev module for Scaev Auctions """ +import os import sqlite3 import asyncio import time @@ -470,4 +471,63 @@ class TroostwijkScraper: results.append(page_data) await browser.close() - return results \ No newline at end of file + return results + + def export_to_files(self) -> Dict[str, str]: + """Export database to CSV/JSON files""" + import sqlite3 + import json + import csv + from datetime import datetime + + timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') + output_dir = os.path.dirname(self.cache.db_path) + + conn = sqlite3.connect(self.cache.db_path) + conn.row_factory = sqlite3.Row + cursor = conn.cursor() + + files = {} + + # Export auctions + cursor.execute("SELECT * FROM auctions") + auctions = [dict(row) for row in cursor.fetchall()] + + auctions_csv = os.path.join(output_dir, f'auctions_{timestamp}.csv') + auctions_json = os.path.join(output_dir, f'auctions_{timestamp}.json') + + if auctions: + with open(auctions_csv, 'w', newline='', encoding='utf-8') as f: + writer = csv.DictWriter(f, fieldnames=auctions[0].keys()) + writer.writeheader() + writer.writerows(auctions) + + with open(auctions_json, 'w', encoding='utf-8') as f: + json.dump(auctions, f, indent=2, ensure_ascii=False) + + files['auctions_csv'] = auctions_csv + files['auctions_json'] = auctions_json + print(f" Exported {len(auctions)} auctions") + + # Export lots + cursor.execute("SELECT * FROM lots") + lots = [dict(row) for row in cursor.fetchall()] + + lots_csv = os.path.join(output_dir, f'lots_{timestamp}.csv') + lots_json = os.path.join(output_dir, f'lots_{timestamp}.json') + + if lots: + with open(lots_csv, 'w', newline='', encoding='utf-8') as f: + writer = csv.DictWriter(f, fieldnames=lots[0].keys()) + writer.writeheader() + writer.writerows(lots) + + with open(lots_json, 'w', encoding='utf-8') as f: + json.dump(lots, f, indent=2, ensure_ascii=False) + + files['lots_csv'] = lots_csv + files['lots_json'] = lots_json + print(f" Exported {len(lots)} lots") + + conn.close() + return files \ No newline at end of file