Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,13 +23,13 @@ requests.packages.urllib3.disable_warnings()
|
|
| 23 |
|
| 24 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 25 |
HF_DATASET = "dxedline/historical_research_data"
|
| 26 |
-
SCRAPE_WORKERS =
|
| 27 |
IMAGE_WORKERS = 10
|
| 28 |
MAX_CELL_CHARS = 49000
|
| 29 |
MAX_WORD_LIMIT = 150000
|
| 30 |
MIN_IMAGE_SIZE = 400
|
| 31 |
IMAGE_SIMILARITY_THRESHOLD = 8
|
| 32 |
-
MIN_RELEVANCE_SCORE =
|
| 33 |
|
| 34 |
if HF_TOKEN:
|
| 35 |
login(token=HF_TOKEN)
|
|
@@ -355,50 +355,114 @@ def scrape_url(url, event_id, incident_date, query, title):
|
|
| 355 |
except:
|
| 356 |
return None
|
| 357 |
|
| 358 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
links = []
|
| 360 |
try:
|
| 361 |
from duckduckgo_search import DDGS
|
| 362 |
-
|
| 363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
for result in results:
|
| 365 |
link = result.get('href') or result.get('link')
|
| 366 |
if link and 'youtube.com' not in link.lower():
|
| 367 |
links.append(link)
|
| 368 |
-
|
| 369 |
-
|
|
|
|
|
|
|
|
|
|
| 370 |
return links
|
| 371 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
def search_and_scrape_event(event_id, query, title, incident_date):
|
| 373 |
all_links = []
|
| 374 |
|
| 375 |
-
yield f"Searching
|
| 376 |
-
|
| 377 |
-
all_links.extend(
|
| 378 |
-
yield f"Found {len(
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
all_links = list(dict.fromkeys(all_links))
|
| 401 |
-
yield f"\nTotal unique links: {len(all_links)}\n\n"
|
| 402 |
|
| 403 |
scraped_articles = []
|
| 404 |
all_images = []
|
|
|
|
| 23 |
|
| 24 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 25 |
HF_DATASET = "dxedline/historical_research_data"
|
| 26 |
+
SCRAPE_WORKERS = 15
|
| 27 |
IMAGE_WORKERS = 10
|
| 28 |
MAX_CELL_CHARS = 49000
|
| 29 |
MAX_WORD_LIMIT = 150000
|
| 30 |
MIN_IMAGE_SIZE = 400
|
| 31 |
IMAGE_SIMILARITY_THRESHOLD = 8
|
| 32 |
+
MIN_RELEVANCE_SCORE = 25
|
| 33 |
|
| 34 |
if HF_TOKEN:
|
| 35 |
login(token=HF_TOKEN)
|
|
|
|
| 355 |
except:
|
| 356 |
return None
|
| 357 |
|
| 358 |
+
def search_duckduckgo_html(query, max_results=100):
|
| 359 |
+
links = []
|
| 360 |
+
try:
|
| 361 |
+
headers = {
|
| 362 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 363 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
| 364 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
| 365 |
+
'Accept-Encoding': 'gzip, deflate',
|
| 366 |
+
'DNT': '1',
|
| 367 |
+
'Connection': 'keep-alive',
|
| 368 |
+
'Upgrade-Insecure-Requests': '1'
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
encoded_query = quote_plus(query)
|
| 372 |
+
search_url = f"https://html.duckduckgo.com/html/?q={encoded_query}"
|
| 373 |
+
|
| 374 |
+
response = requests.get(search_url, headers=headers, timeout=15)
|
| 375 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 376 |
+
|
| 377 |
+
for result in soup.find_all('a', class_='result__a'):
|
| 378 |
+
href = result.get('href')
|
| 379 |
+
if href:
|
| 380 |
+
if href.startswith('//duckduckgo.com/l/?'):
|
| 381 |
+
actual_url = href.split('uddg=')[1].split('&')[0] if 'uddg=' in href else None
|
| 382 |
+
if actual_url:
|
| 383 |
+
from urllib.parse import unquote
|
| 384 |
+
decoded_url = unquote(actual_url)
|
| 385 |
+
if decoded_url.startswith('http') and 'youtube.com' not in decoded_url.lower():
|
| 386 |
+
links.append(decoded_url)
|
| 387 |
+
if len(links) >= max_results:
|
| 388 |
+
break
|
| 389 |
+
|
| 390 |
+
time.sleep(2)
|
| 391 |
+
except Exception as e:
|
| 392 |
+
print(f"HTML search error: {e}")
|
| 393 |
+
|
| 394 |
+
return links
|
| 395 |
+
|
| 396 |
+
def search_duckduckgo_api(query, timelimit=None, max_results=100):
|
| 397 |
links = []
|
| 398 |
try:
|
| 399 |
from duckduckgo_search import DDGS
|
| 400 |
+
|
| 401 |
+
ddgs = DDGS(timeout=20)
|
| 402 |
+
|
| 403 |
+
if timelimit:
|
| 404 |
+
results = ddgs.text(query, timelimit=timelimit, max_results=max_results)
|
| 405 |
+
else:
|
| 406 |
+
results = ddgs.text(query, max_results=max_results)
|
| 407 |
+
|
| 408 |
for result in results:
|
| 409 |
link = result.get('href') or result.get('link')
|
| 410 |
if link and 'youtube.com' not in link.lower():
|
| 411 |
links.append(link)
|
| 412 |
+
|
| 413 |
+
time.sleep(2)
|
| 414 |
+
except Exception as e:
|
| 415 |
+
print(f"API search error: {e}")
|
| 416 |
+
|
| 417 |
return links
|
| 418 |
|
| 419 |
+
def search_combined(query, timelimit=None, max_results=100):
|
| 420 |
+
all_links = []
|
| 421 |
+
|
| 422 |
+
api_links = search_duckduckgo_api(query, timelimit, max_results)
|
| 423 |
+
all_links.extend(api_links)
|
| 424 |
+
|
| 425 |
+
if len(api_links) < 10:
|
| 426 |
+
html_links = search_duckduckgo_html(query, max_results)
|
| 427 |
+
all_links.extend(html_links)
|
| 428 |
+
|
| 429 |
+
return list(dict.fromkeys(all_links))
|
| 430 |
+
|
| 431 |
def search_and_scrape_event(event_id, query, title, incident_date):
|
| 432 |
all_links = []
|
| 433 |
|
| 434 |
+
yield f"Searching all time for query: {query}...\n"
|
| 435 |
+
query_all = search_combined(query, timelimit=None, max_results=100)
|
| 436 |
+
all_links.extend(query_all)
|
| 437 |
+
yield f"Found {len(query_all)} results\n"
|
| 438 |
+
|
| 439 |
+
yield f"Searching all time for title: {title}...\n"
|
| 440 |
+
title_all = search_combined(title, timelimit=None, max_results=100)
|
| 441 |
+
all_links.extend(title_all)
|
| 442 |
+
yield f"Found {len(title_all)} results\n"
|
| 443 |
+
|
| 444 |
+
yield f"Searching past year: {query}...\n"
|
| 445 |
+
year_links = search_combined(query, timelimit='y', max_results=80)
|
| 446 |
+
all_links.extend(year_links)
|
| 447 |
+
yield f"Found {len(year_links)} results from past year\n"
|
| 448 |
+
|
| 449 |
+
yield f"Searching past year: {title}...\n"
|
| 450 |
+
year_title = search_combined(title, timelimit='y', max_results=80)
|
| 451 |
+
all_links.extend(year_title)
|
| 452 |
+
yield f"Found {len(year_title)} results\n"
|
| 453 |
+
|
| 454 |
+
yield f"Searching past month: {query}...\n"
|
| 455 |
+
month_links = search_combined(query, timelimit='m', max_results=60)
|
| 456 |
+
all_links.extend(month_links)
|
| 457 |
+
yield f"Found {len(month_links)} results from past month\n"
|
| 458 |
+
|
| 459 |
+
yield f"Searching past week: {query}...\n"
|
| 460 |
+
week_links = search_combined(query, timelimit='w', max_results=40)
|
| 461 |
+
all_links.extend(week_links)
|
| 462 |
+
yield f"Found {len(week_links)} results from past week\n"
|
| 463 |
|
| 464 |
all_links = list(dict.fromkeys(all_links))
|
| 465 |
+
yield f"\nTotal unique links collected: {len(all_links)}\n\n"
|
| 466 |
|
| 467 |
scraped_articles = []
|
| 468 |
all_images = []
|