Spaces:
Runtime error
Runtime error
File size: 20,020 Bytes
1bf21f5 3098121 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# noaa_incidents.py
import os
import hashlib
import json
import pickle
import threading
import time
from pathlib import Path
from datetime import datetime
from typing import Dict, List, Optional, Tuple
import logging
import pandas as pd
import requests
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
import chromadb
from chromadb.utils import embedding_functions
from chromadb.config import Settings
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Constants
BASE_URL = "https://incidentnews.noaa.gov"
BROWSE_URL = f"{BASE_URL}/browse/date"
OUTPUT_DIR = Path("output")
CACHE_DIR = Path("cache")
MAX_RETRIES = 3
BATCH_SIZE = 500
class NOAAIncidentScraper:
"""
Scrapes NOAA Incident News data with caching and multi-threading support.
Optimized for Hugging Face Spaces environment.
"""
def __init__(self, max_workers: int = 5, cache_dir: str = 'cache'):
"""
Initialize the scraper with custom configuration.
Args:
max_workers (int): Maximum number of concurrent threads
cache_dir (str): Directory for caching web responses
"""
self.base_url = BASE_URL
self.browse_url = BROWSE_URL
self.headers = {
'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36')
}
self.incidents_data = []
self.data_lock = threading.Lock()
self.max_workers = max_workers
self.cache_dir = Path(cache_dir)
self.cache_dir.mkdir(exist_ok=True)
self.session = requests.Session()
self.session.headers.update(self.headers)
def get_cache_path(self, url: str) -> Path:
"""Generate a cache file path for a given URL."""
url_hash = hashlib.md5(url.encode()).hexdigest()
return self.cache_dir / f"{url_hash}.cache"
def get_cached_response(self, url: str) -> Optional[str]:
"""Retrieve cached response for a URL."""
cache_path = self.get_cache_path(url)
if cache_path.exists():
try:
with cache_path.open('rb') as f:
return pickle.load(f)
except Exception as e:
logger.warning(f"Error loading cache for {url}: {e}")
return None
return None
def cache_response(self, url: str, content: str):
"""Cache response content for a URL."""
cache_path = self.get_cache_path(url)
try:
with cache_path.open('wb') as f:
pickle.dump(content, f)
except Exception as e:
logger.warning(f"Error caching response for {url}: {e}")
def fetch_url(self, url: str, use_cache: bool = True) -> Optional[str]:
"""
Fetch URL content with caching and retry mechanism.
Args:
url (str): URL to fetch
use_cache (bool): Whether to use cached response
Returns:
Optional[str]: Response content or None if failed
"""
if use_cache:
cached = self.get_cached_response(url)
if cached:
return cached
for attempt in range(MAX_RETRIES):
try:
response = self.session.get(url, timeout=10)
response.raise_for_status()
content = response.text
if use_cache:
self.cache_response(url, content)
return content
except requests.RequestException as e:
wait_time = min(2 ** attempt, 10) # Exponential backoff with max 10s
logger.warning(f"Attempt {attempt + 1}/{MAX_RETRIES} failed for {url}: {e}")
if attempt < MAX_RETRIES - 1:
logger.info(f"Retrying in {wait_time} seconds...")
time.sleep(wait_time)
else:
logger.error(f"Failed to fetch {url} after {MAX_RETRIES} attempts")
return None
def validate_incident_data(self, incident_data: Dict) -> bool:
"""
Validate scraped incident data.
Args:
incident_data (Dict): Incident data to validate
Returns:
bool: True if data is valid
"""
required_fields = ['title', 'date', 'location']
return all(incident_data.get(field) for field in required_fields)
def get_total_pages(self) -> int:
"""Get the total number of pages to scrape."""
content = self.fetch_url(self.browse_url)
if not content:
return 0
try:
soup = BeautifulSoup(content, 'html.parser')
pagination = soup.find('ul', class_='pagination')
if pagination:
last_page = int(pagination.find_all('li')[-2].text)
return last_page
except Exception as e:
logger.error(f"Error determining total pages: {e}")
return 1
def scrape_incident_page(self, incident_url: str, use_cache: bool = True) -> Optional[Dict]:
"""
Scrape detailed information from an individual incident page.
Args:
incident_url (str): URL of the incident page
use_cache (bool): Whether to use cached response
Returns:
Optional[Dict]: Incident data or None if failed
"""
try:
full_url = f"{self.base_url}{incident_url}"
content = self.fetch_url(full_url, use_cache)
if not content:
return None
soup = BeautifulSoup(content, 'html.parser')
incident_details = {}
# Extract Title
title = soup.find('h1')
incident_details['title'] = title.text.strip() if title else None
# Extract Initial Notification
initial_notification = soup.find('p', class_='sub')
incident_details['initial_notification'] = (
initial_notification.text.strip() if initial_notification else None
)
# Extract Location and Date
location_date = soup.find('p', class_='')
if location_date:
location = location_date.find('span', class_='glyphicon-map-marker')
if location and location.next_sibling:
incident_details['location'] = location.next_sibling.strip()
date = location_date.find('span', class_='incident-date')
if date:
incident_details['date'] = date.text.strip()
# Extract Additional Details from Table
details_table = soup.find('table', class_='table')
if details_table:
for row in details_table.find_all('tr'):
cols = row.find_all('td')
if len(cols) == 2:
key = cols[0].text.strip().rstrip(':').lower().replace(' ', '_')
value = cols[1].text.strip()
incident_details[key] = value
if not self.validate_incident_data(incident_details):
logger.warning(f"Invalid incident data for {incident_url}")
return None
return incident_details
except Exception as e:
logger.error(f"Error scraping incident page {incident_url}: {e}")
return None
def scrape_listing_page(self, page_number: int, use_cache: bool = True) -> List[str]:
"""
Scrape a single listing page of incidents.
Args:
page_number (int): Page number to scrape
use_cache (bool): Whether to use cached response
Returns:
List[str]: List of incident URLs
"""
url = f"{self.browse_url}?page={page_number}"
try:
content = self.fetch_url(url, use_cache)
if not content:
return []
soup = BeautifulSoup(content, 'html.parser')
incidents_table = soup.find('table', class_='incident-links')
if not incidents_table:
logger.warning(f"No incidents table found on page {page_number}")
return []
incident_urls = []
for row in incidents_table.find_all('tr')[1:]: # Skip header row
cols = row.find_all('td')
if len(cols) >= 3:
incident_link = cols[0].find('a')
if incident_link and 'href' in incident_link.attrs:
incident_urls.append(incident_link['href'])
return incident_urls
except Exception as e:
logger.error(f"Error scraping listing page {page_number}: {e}")
return []
def process_incident(self, incident_url: str, use_cache: bool = True) -> Optional[str]:
"""Process a single incident URL with thread safety."""
incident_data = self.scrape_incident_page(incident_url, use_cache)
if incident_data:
with self.data_lock:
self.incidents_data.append(incident_data)
return incident_url
return None
def save_data(self) -> Tuple[str, str]:
"""
Save the scraped data to CSV and JSON files.
Returns:
Tuple[str, str]: Paths to saved CSV and JSON files
"""
OUTPUT_DIR.mkdir(exist_ok=True)
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
try:
# Save to CSV
csv_filename = OUTPUT_DIR / f'noaa_incidents_{timestamp}.csv'
df = pd.DataFrame(self.incidents_data)
df.to_csv(csv_filename, index=False)
# Save to JSON
json_filename = OUTPUT_DIR / f'noaa_incidents_{timestamp}.json'
with open(json_filename, 'w', encoding='utf-8') as f:
json.dump(self.incidents_data, f, indent=4)
logger.info(f"Data saved to {csv_filename} and {json_filename}")
return str(csv_filename), str(json_filename)
except Exception as e:
logger.error(f"Error saving data: {e}")
return None, None
def run(self, validate_first: bool = True) -> Tuple[Optional[str], Optional[str]]:
"""
Run the complete scraping process.
Args:
validate_first (bool): Whether to validate first page before full scrape
Returns:
Tuple[Optional[str], Optional[str]]: Paths to saved CSV and JSON files
"""
logger.info("Starting NOAA IncidentNews scraper...")
if validate_first:
logger.info("Performing initial validation run...")
test_page = self.scrape_listing_page(1, use_cache=False)
if not test_page:
logger.error("Unable to scrape the first page. Aborting.")
return None, None
test_incident = self.scrape_incident_page(test_page[0], use_cache=False)
if not test_incident:
logger.error("Unable to scrape test incident. Aborting.")
return None, None
logger.info("Validation successful, proceeding with full scrape...")
total_pages = self.get_total_pages()
logger.info(f"Found {total_pages} pages to scrape")
# Collect incident URLs
all_incident_urls = []
logger.info("Collecting incident URLs...")
with tqdm(total=total_pages, desc="Collecting URLs") as pbar:
for page in range(1, total_pages + 1):
urls = self.scrape_listing_page(page)
all_incident_urls.extend(urls)
pbar.update(1)
total_incidents = len(all_incident_urls)
logger.info(f"Found {total_incidents} incidents to process")
if total_incidents == 0:
logger.error("No incidents found to process")
return None, None
# Process incidents
logger.info("Processing incidents...")
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
futures = [
executor.submit(self.process_incident, url)
for url in all_incident_urls
]
with tqdm(total=total_incidents, desc="Scraping incidents") as pbar:
for future in as_completed(futures):
try:
future.result()
pbar.update(1)
except Exception as e:
logger.error(f"Error processing incident: {e}")
logger.info(f"Scraped {len(self.incidents_data)} incidents")
if self.incidents_data:
return self.save_data()
else:
logger.error("No incident data scraped")
return None, None
class NOAAIncidentDB:
"""
Manages NOAA incident data using ChromaDB for vector storage and search.
"""
def __init__(self,
persist_directory: str = "noaa_db",
embedding_model: str = "all-MiniLM-L6-v2"):
"""
Initialize the database with ChromaDB settings.
Args:
persist_directory (str): Directory for ChromaDB storage
embedding_model (str): Model name for embeddings
"""
self.persist_directory = persist_directory
os.makedirs(self.persist_directory, exist_ok=True)
# Initialize ChromaDB client
self.client = chromadb.Client(Settings(
persist_directory=self.persist_directory,
anonymized_telemetry=False
))
# Setup embedding function
self.embedding_function = embedding_functions.SentenceTransformerEmbeddingFunction(
model_name=embedding_model
)
# Get or create collection
self.collection = self.client.get_or_create_collection(
name="noaa_incidents",
embedding_function=self.embedding_function,
metadata={"description": "NOAA incident reports database"}
)
def load_incidents(self, csv_path: str) -> int:
"""
Load incidents from CSV into ChromaDB.
Args:
csv_path (str): Path to CSV file
Returns:
int: Number of incidents loaded
"""
logger.info(f"Loading incidents from {csv_path}")
try:
df = pd.read_csv(csv_path, dtype=str)
df = df.where(pd.notna(df), None)
documents = []
metadatas = []
ids = []
for idx, row in df.iterrows():
# Generate unique ID
# Continue from the previous code...
# Generate unique ID using title, date, location and index
unique_string = str(row.get('title', '')) + '_' + str(row.get('date', '')) + '_' + str(row.get('location', '')) + '_' + str(idx)
incident_id = "incident_" + hashlib.md5(unique_string.encode()).hexdigest()[:8]
# Create searchable document content
doc_content = "\n".join([
"Incident: " + str(row.get('title', 'N/A')),
"Location: " + str(row.get('location', 'N/A')),
"Date: " + str(row.get('date', 'N/A')),
"Details: " + str(row.get('initial_notification', ''))
])
# Create metadata
metadata = {
'title': str(row.get('title', 'N/A')),
'date': str(row.get('date', 'N/A')),
'location': str(row.get('location', 'N/A'))
}
# Add any additional fields present
for col in df.columns:
if col not in ['title', 'date', 'location'] and pd.notna(row[col]):
metadata[col.lower().replace(' ', '_')] = str(row[col])
documents.append(doc_content.strip())
metadatas.append(metadata)
ids.append(incident_id)
# Add to database in batches
total_documents = len(documents)
for i in range(0, total_documents, BATCH_SIZE):
batch_end = min(i + BATCH_SIZE, total_documents)
self.collection.add(
documents=documents[i:batch_end],
metadatas=metadatas[i:batch_end],
ids=ids[i:batch_end]
)
logger.info(f"Added batch {i // BATCH_SIZE + 1} with {batch_end - i} incidents")
logger.info(f"Successfully loaded {total_documents} incidents into ChromaDB")
return total_documents
except Exception as e:
logger.error(f"Error loading incidents from CSV: {e}")
return 0
def search(self, query: str, n_results: int = 5) -> List[Dict]:
"""
Search for incidents matching the query.
Args:
query (str): Search query
n_results (int): Number of results to return
Returns:
List[Dict]: List of matching incidents
"""
try:
results = self.collection.query(
query_texts=[query],
n_results=n_results,
include=['metadatas', 'documents', 'ids']
)
formatted_results = []
for doc, metadata, incident_id in zip(
results['documents'][0],
results['metadatas'][0],
results['ids'][0]
):
result = {
'id': incident_id,
'title': metadata.get('title', 'N/A'),
'date': metadata.get('date', 'N/A'),
'location': metadata.get('location', 'N/A'),
'details': doc,
'metadata': metadata
}
formatted_results.append(result)
return formatted_results
except Exception as e:
logger.error(f"Error during search: {e}")
return []
def delete_collection(self):
"""Delete the current collection."""
try:
self.client.delete_collection("noaa_incidents")
logger.info("Collection deleted successfully")
except Exception as e:
logger.error(f"Error deleting collection: {e}")
def get_collection_stats(self) -> Dict:
"""
Get statistics about the current collection.
Returns:
Dict: Collection statistics
"""
try:
count = self.collection.count()
return {
"total_documents": count,
"collection_name": "noaa_incidents",
"embedding_model": self.embedding_function.model_name
}
except Exception as e:
logger.error(f"Error getting collection stats: {e}")
return {}
if __name__ == "__main__":
# Example usage
scraper = NOAAIncidentScraper(max_workers=5)
csv_file, json_file = scraper.run(validate_first=True)
if csv_file:
db = NOAAIncidentDB()
num_loaded = db.load_incidents(csv_file)
logger.info(f"Loaded {num_loaded} incidents into database")
# Example search
results = db.search("oil spill near coral reefs", n_results=5)
for i, result in enumerate(results, 1):
print(f"\nResult {i}:")
print(f"Title: {result['title']}")
print(f"Date: {result['date']}")
print(f"Location: {result['location']}")
print(f"Details: {result['details']}\n") |