seawolf2357 commited on
Commit
18c9d60
ยท
verified ยท
1 Parent(s): 30b6d7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -20
app.py CHANGED
@@ -1,31 +1,37 @@
1
  import gradio as gr
2
- from gnewsclient import gnewsclient
 
3
  from datetime import datetime, timedelta
4
 
5
- # Google ๋‰ด์Šค ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
6
- client = gnewsclient.NewsClient(language='en', location='', topic='', max_results=10)
7
-
8
- # ์ง€์›๋˜๋Š” ๊ตญ๊ฐ€ ๋ชฉ๋ก
9
- supported_countries = client.locations
 
 
 
10
 
11
  def get_news(country, keyword):
12
- # ๊ตญ๊ฐ€ ๋ฐ ํ‚ค์›Œ๋“œ ์„ค์ •
13
- client.location = country
14
- client.query = keyword
 
 
 
 
 
15
 
16
  # ํ˜„์žฌ ์‹œ๊ฐ„ ๊ธฐ์ค€ 24์‹œ๊ฐ„ ์ „ ์‹œ๊ฐ„ ๊ณ„์‚ฐ
17
  time_threshold = datetime.now() - timedelta(hours=24)
18
 
19
- # ๋‰ด์Šค ๊ฒ€์ƒ‰
20
- news_items = client.get_news()
21
-
22
- # 24์‹œ๊ฐ„ ์ด๋‚ด์˜ ๋‰ด์Šค๋งŒ ํ•„ํ„ฐ๋งํ•˜๊ณ  ์ œ๋ชฉ๊ณผ ๋งํฌ ์ถ”์ถœ
23
  filtered_news = []
24
- for item in news_items:
25
- if 'published' in item:
26
- news_date = datetime.strptime(item['published'], "%a, %d %b %Y %H:%M:%S %Z")
27
- if news_date > time_threshold:
28
- filtered_news.append(f"Title: {item['title']}\nLink: {item['link']}\n")
29
 
30
  return "\n".join(filtered_news) if filtered_news else "No recent news found for the given keyword."
31
 
@@ -33,12 +39,12 @@ def get_news(country, keyword):
33
  iface = gr.Interface(
34
  fn=get_news,
35
  inputs=[
36
- gr.Dropdown(choices=supported_countries, label="Select Country"),
37
  gr.Textbox(label="Enter keyword")
38
  ],
39
  outputs="text",
40
  title="Google News Search",
41
- description="Search for news articles from the last 24 hours using Google News."
42
  )
43
 
44
  # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
 
1
  import gradio as gr
2
+ import requests
3
+ import feedparser
4
  from datetime import datetime, timedelta
5
 
6
+ # ์ง€์›๋˜๋Š” ๊ตญ๊ฐ€ ๋ฐ ํ•ด๋‹น Google News URL
7
+ SUPPORTED_COUNTRIES = {
8
+ 'United States': 'https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en',
9
+ 'United Kingdom': 'https://news.google.com/rss?hl=en-GB&gl=GB&ceid=GB:en',
10
+ 'Australia': 'https://news.google.com/rss?hl=en-AU&gl=AU&ceid=AU:en',
11
+ 'India': 'https://news.google.com/rss?hl=en-IN&gl=IN&ceid=IN:en',
12
+ 'Canada': 'https://news.google.com/rss?hl=en-CA&gl=CA&ceid=CA:en'
13
+ }
14
 
15
  def get_news(country, keyword):
16
+ if country not in SUPPORTED_COUNTRIES:
17
+ return "Selected country is not supported."
18
+
19
+ url = SUPPORTED_COUNTRIES[country]
20
+
21
+ # RSS ํ”ผ๋“œ ๊ฐ€์ ธ์˜ค๊ธฐ
22
+ response = requests.get(url)
23
+ feed = feedparser.parse(response.content)
24
 
25
  # ํ˜„์žฌ ์‹œ๊ฐ„ ๊ธฐ์ค€ 24์‹œ๊ฐ„ ์ „ ์‹œ๊ฐ„ ๊ณ„์‚ฐ
26
  time_threshold = datetime.now() - timedelta(hours=24)
27
 
28
+ # ํ‚ค์›Œ๋“œ์™€ ์ผ์น˜ํ•˜๊ณ  24์‹œ๊ฐ„ ์ด๋‚ด์˜ ๋‰ด์Šค๋งŒ ํ•„ํ„ฐ๋ง
 
 
 
29
  filtered_news = []
30
+ for entry in feed.entries:
31
+ if keyword.lower() in entry.title.lower():
32
+ pub_date = datetime(*entry.published_parsed[:6])
33
+ if pub_date > time_threshold:
34
+ filtered_news.append(f"Title: {entry.title}\nLink: {entry.link}\n")
35
 
36
  return "\n".join(filtered_news) if filtered_news else "No recent news found for the given keyword."
37
 
 
39
  iface = gr.Interface(
40
  fn=get_news,
41
  inputs=[
42
+ gr.Dropdown(choices=list(SUPPORTED_COUNTRIES.keys()), label="Select Country"),
43
  gr.Textbox(label="Enter keyword")
44
  ],
45
  outputs="text",
46
  title="Google News Search",
47
+ description="Search for news articles from the last 24 hours using Google News RSS feeds."
48
  )
49
 
50
  # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰