Skip to content

Commit a745932

Browse files
Copilotlurenss
andcommitted
Address code review feedback: move imports to top and improve API key handling
Co-authored-by: lurenss <[email protected]>
1 parent 1f838d1 commit a745932

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

amazon_keyboard_scraper.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import sys
2424
import os
2525
import time
26+
import re
27+
import hashlib
28+
import traceback
2629
from typing import List, Dict, Any, Optional
2730
from datetime import datetime
2831

@@ -38,14 +41,17 @@ class AmazonKeyboardScraper:
3841

3942
# Amazon Italy base URL for keyboard search
4043
AMAZON_BASE_URL = "https://www.amazon.it/s?k=keyboard"
41-
API_KEY = "sgai-763dcc80-3a64-417f-b9bf-b98c8f50cc4b"
44+
# Default API key (can be overridden with SGAI_API_KEY environment variable)
45+
DEFAULT_API_KEY = "sgai-763dcc80-3a64-417f-b9bf-b98c8f50cc4b"
4246
MARKETPLACE = "Amazon IT"
4347
TOTAL_PAGES = 20
4448

4549
def __init__(self):
4650
"""Initialize the scraper with custom API key"""
47-
# Set the API key in environment
48-
os.environ['SGAI_API_KEY'] = self.API_KEY
51+
# Get API key from environment or use default
52+
# Users can set SGAI_API_KEY environment variable to override the default
53+
api_key = os.environ.get('SGAI_API_KEY', self.DEFAULT_API_KEY)
54+
os.environ['SGAI_API_KEY'] = api_key
4955

5056
# Load configuration
5157
self.config = Config.from_env()
@@ -56,7 +62,7 @@ def __init__(self):
5662
# Initialize ScrapeGraph client
5763
try:
5864
from scrapegraph_py import Client
59-
self.sg_client = Client(api_key=self.API_KEY)
65+
self.sg_client = Client(api_key=api_key)
6066
self.use_api = True
6167
print("✓ ScrapeGraph API client initialized")
6268
except Exception as e:
@@ -169,7 +175,6 @@ def _scrape_with_api(self, page_url: str, page_num: int) -> List[Product]:
169175
asin = product_data.get('asin', '')
170176
if not asin and product_url:
171177
# Try to extract ASIN from URL
172-
import re
173178
match = re.search(r'/dp/([A-Z0-9]{10})', product_url)
174179
if match:
175180
asin = match.group(1)
@@ -209,8 +214,6 @@ def _scrape_with_api(self, page_url: str, page_num: int) -> List[Product]:
209214

210215
def _scrape_with_mock(self, page_url: str, page_num: int) -> List[Product]:
211216
"""Generate mock product data for demonstration"""
212-
import hashlib
213-
214217
products = []
215218
# Generate 10-15 products per page
216219
num_products = 12 + (page_num % 4)
@@ -744,7 +747,6 @@ def main():
744747

745748
except Exception as e:
746749
print(f"\n✗ Fatal error: {e}")
747-
import traceback
748750
traceback.print_exc()
749751

750752
finally:

0 commit comments

Comments
 (0)