seawolf2357 commited on
Commit
2efa0ec
โ€ข
1 Parent(s): ac9df44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -2
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import requests
3
  import json
4
  from functools import lru_cache
 
5
 
6
  # Google Custom Search API ํ‚ค์™€ ๊ฒ€์ƒ‰ ์—”์ง„ ID
7
  API_KEY = "AIzaSyB8wNdEL8-SAvelRq-zenLLU-cUEmsj7uE"
@@ -26,10 +27,17 @@ def search_news(keyword, country):
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':
@@ -51,11 +59,13 @@ def search_news_impl(keyword, country):
51
  title = item['title']
52
  link = item['link']
53
  snippet = item.get('snippet', 'No snippet available')
 
54
  formatted_results += f"<h3><a href='{link}' target='_blank'>{title}</a></h3>"
 
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)}"
@@ -81,7 +91,7 @@ iface = gr.Interface(
81
  ],
82
  outputs=gr.HTML(),
83
  title="Google News Search",
84
- description="Search for news articles using Google Custom Search API."
85
  )
86
 
87
  # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
 
2
  import requests
3
  import json
4
  from functools import lru_cache
5
+ from datetime import datetime, timedelta
6
 
7
  # Google Custom Search API ํ‚ค์™€ ๊ฒ€์ƒ‰ ์—”์ง„ ID
8
  API_KEY = "AIzaSyB8wNdEL8-SAvelRq-zenLLU-cUEmsj7uE"
 
27
  def search_news_impl(keyword, country):
28
  url = "https://www.googleapis.com/customsearch/v1"
29
 
30
+ # 24์‹œ๊ฐ„ ์ „ ๋‚ ์งœ ๊ณ„์‚ฐ
31
+ one_day_ago = (datetime.utcnow() - timedelta(days=1)).strftime("%Y-%m-%d")
32
+
33
  params = {
34
  'key': API_KEY,
35
  'cx': SEARCH_ENGINE_ID,
36
  'q': keyword,
37
+ 'dateRestrict': 'd1', # ์ตœ๊ทผ 1์ผ ๋‚ด ๊ฒฐ๊ณผ๋งŒ
38
+ 'sort': 'date', # ๋‚ ์งœ์ˆœ ์ •๋ ฌ
39
+ 'num': 10, # ๊ฒฐ๊ณผ ์ˆ˜
40
+ 'tbm': 'nws', # ๋‰ด์Šค ๊ฒ€์ƒ‰์œผ๋กœ ์ œํ•œ
41
  }
42
 
43
  if country != 'All Countries':
 
59
  title = item['title']
60
  link = item['link']
61
  snippet = item.get('snippet', 'No snippet available')
62
+ published_date = item.get('pagemap', {}).get('metatags', [{}])[0].get('article:published_time', 'Unknown date')
63
  formatted_results += f"<h3><a href='{link}' target='_blank'>{title}</a></h3>"
64
+ formatted_results += f"<p>Published: {published_date}</p>"
65
  formatted_results += f"<p>{snippet}</p><br>"
66
  formatted_results += f"<p>Total results: {len(results['items'])}</p>"
67
  else:
68
+ formatted_results = f"No news found for '{keyword}' in {country} within the last 24 hours."
69
 
70
  except requests.exceptions.HTTPError as e:
71
  formatted_results = f"An error occurred: {str(e)}"
 
91
  ],
92
  outputs=gr.HTML(),
93
  title="Google News Search",
94
+ description="Search for news articles from the last 24 hours using Google Custom Search API."
95
  )
96
 
97
  # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰