Shreyas094 commited on
Commit
c5d68ba
·
verified ·
1 Parent(s): 00ac423

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -167,14 +167,19 @@ def google_search(term, num_results=5, lang="en", timeout=5, safe="active", ssl_
167
  print(f"Total results fetched: {len(all_results)}")
168
  return all_results
169
 
170
- def google_news_search(term, num_results=5, lang="en", timeout=5, safe="active", ssl_verify=None):
171
  print(f"Searching Google News for term: {term}")
172
 
173
- # Get the current year
174
- current_year = datetime.now().year
 
175
 
176
- # Add the current year to the search term
177
- search_term = f"{term} {current_year}"
 
 
 
 
178
 
179
  escaped_term = urllib.parse.quote_plus(search_term)
180
  start = 0
@@ -513,16 +518,20 @@ def save_text_to_pdf(text, output_path):
513
  print("PDF saved successfully.")
514
 
515
  # Integrated function to perform web scraping, formatting, and text generation
516
- def scrape_and_display(query, num_results, instructions, web_search=True, use_news=False, temperature=0.7, repetition_penalty=1.0, top_p=0.9):
517
  print(f"Scraping and displaying results for query: {query} with num_results: {num_results}")
518
  if web_search:
 
 
 
 
519
  if use_news:
520
- search_results = google_news_search(query, num_results)
 
 
521
  else:
522
- # Add the year-based search here
523
- current_year = datetime.now().year
524
- days_in_year = 365 if current_year % 4 != 0 else 366 # Account for leap years
525
- search_results = google_search(query, num_results=num_results, instructions=instructions, days_back=days_in_year)
526
 
527
  # Summarize each result
528
  summarized_results = []
 
167
  print(f"Total results fetched: {len(all_results)}")
168
  return all_results
169
 
170
+ def google_news_search(term, num_results=5, lang="en", timeout=5, safe="active", ssl_verify=None, days_back=30):
171
  print(f"Searching Google News for term: {term}")
172
 
173
+ # Calculate the date range
174
+ end_date = datetime.now()
175
+ start_date = end_date - timedelta(days=days_back)
176
 
177
+ # Format dates as strings
178
+ start_date_str = start_date.strftime("%Y-%m-%d")
179
+ end_date_str = end_date.strftime("%Y-%m-%d")
180
+
181
+ # Add the date range to the search term
182
+ search_term = f"{term} after:{start_date_str} before:{end_date_str}"
183
 
184
  escaped_term = urllib.parse.quote_plus(search_term)
185
  start = 0
 
518
  print("PDF saved successfully.")
519
 
520
  # Integrated function to perform web scraping, formatting, and text generation
521
+ def scrape_and_display(query, num_results, instructions, web_search=True, use_news=False, days_back=None, temperature=0.7, repetition_penalty=1.0, top_p=0.9):
522
  print(f"Scraping and displaying results for query: {query} with num_results: {num_results}")
523
  if web_search:
524
+ if days_back is None:
525
+ current_year = datetime.now().year
526
+ days_back = 365 if current_year % 4 != 0 else 366 # Account for leap years
527
+
528
  if use_news:
529
+ # For news, we might want to use a shorter time frame by default
530
+ news_days_back = min(days_back, 30) # Use at most 30 days for news
531
+ search_results = google_news_search(query, num_results, days_back=news_days_back)
532
  else:
533
+ search_results = google_search(query, num_results=num_results, instructions=instructions, days_back=days_back)
534
+
 
 
535
 
536
  # Summarize each result
537
  summarized_results = []