seawolf2357 commited on
Commit
ac9df44
·
verified ·
1 Parent(s): d68e8a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -12
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  import requests
3
- from datetime import datetime, timedelta
4
  import json
5
  from functools import lru_cache
6
 
@@ -27,18 +26,10 @@ def search_news(keyword, country):
27
  def search_news_impl(keyword, country):
28
  url = "https://www.googleapis.com/customsearch/v1"
29
 
30
- # 날짜 범위 설정 (최근 24시간)
31
- end_date = datetime.utcnow()
32
- start_date = end_date - timedelta(days=1)
33
-
34
  params = {
35
  'key': API_KEY,
36
  'cx': SEARCH_ENGINE_ID,
37
- 'q': f"{keyword} site:news.google.com",
38
- 'lr': 'lang_en', # 영어 결과만
39
- 'sort': 'date:r:20230101:99999999', # 날짜순 정렬
40
- 'num': 100, # 최대 100개 결과
41
- 'dateRestrict': 'd1', # 최근 1일 내 결과만
42
  }
43
 
44
  if country != 'All Countries':
@@ -51,6 +42,7 @@ def search_news_impl(keyword, country):
51
 
52
  debug_info = f"API Request URL: {response.url}\n"
53
  debug_info += f"API Response Status: {response.status_code}\n"
 
54
  debug_info += f"API Response Body: {json.dumps(results, indent=2)}\n"
55
 
56
  formatted_results = ""
@@ -63,12 +55,13 @@ def search_news_impl(keyword, country):
63
  formatted_results += f"<p>{snippet}</p><br>"
64
  formatted_results += f"<p>Total results: {len(results['items'])}</p>"
65
  else:
66
- formatted_results = f"No news found for '{keyword}' in {country} within the last 24 hours."
67
 
68
  except requests.exceptions.HTTPError as e:
69
  formatted_results = f"An error occurred: {str(e)}"
70
  debug_info = f"Error: {str(e)}\n"
71
  debug_info += f"API Response Status: {e.response.status_code}\n"
 
72
  debug_info += f"API Response Body: {e.response.text}\n"
73
 
74
  except Exception as e:
@@ -88,7 +81,7 @@ iface = gr.Interface(
88
  ],
89
  outputs=gr.HTML(),
90
  title="Google News Search",
91
- description="Search for up to 100 news articles from the last 24 hours using Google Custom Search API."
92
  )
93
 
94
  # 애플리케이션 실행
 
1
  import gradio as gr
2
  import requests
 
3
  import json
4
  from functools import lru_cache
5
 
 
26
  def search_news_impl(keyword, country):
27
  url = "https://www.googleapis.com/customsearch/v1"
28
 
 
 
 
 
29
  params = {
30
  'key': API_KEY,
31
  'cx': SEARCH_ENGINE_ID,
32
+ 'q': keyword,
 
 
 
 
33
  }
34
 
35
  if country != 'All Countries':
 
42
 
43
  debug_info = f"API Request URL: {response.url}\n"
44
  debug_info += f"API Response Status: {response.status_code}\n"
45
+ debug_info += f"API Response Headers: {json.dumps(dict(response.headers), indent=2)}\n"
46
  debug_info += f"API Response Body: {json.dumps(results, indent=2)}\n"
47
 
48
  formatted_results = ""
 
55
  formatted_results += f"<p>{snippet}</p><br>"
56
  formatted_results += f"<p>Total results: {len(results['items'])}</p>"
57
  else:
58
+ formatted_results = f"No results found for '{keyword}' in {country}."
59
 
60
  except requests.exceptions.HTTPError as e:
61
  formatted_results = f"An error occurred: {str(e)}"
62
  debug_info = f"Error: {str(e)}\n"
63
  debug_info += f"API Response Status: {e.response.status_code}\n"
64
+ debug_info += f"API Response Headers: {json.dumps(dict(e.response.headers), indent=2)}\n"
65
  debug_info += f"API Response Body: {e.response.text}\n"
66
 
67
  except Exception as e:
 
81
  ],
82
  outputs=gr.HTML(),
83
  title="Google News Search",
84
+ description="Search for news articles using Google Custom Search API."
85
  )
86
 
87
  # 애플리케이션 실행