seawolf2357 commited on
Commit
1b298f9
ยท
verified ยท
1 Parent(s): 16e9e43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -107
app.py CHANGED
@@ -1,121 +1,70 @@
1
  import gradio as gr
2
  import requests
3
  from datetime import datetime, timedelta
4
- import pycountry
5
- import json
6
 
7
- # NewsAPI key
8
- API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
9
-
10
-
11
- COUNTRY_CODES = "ae ar at au be bg br ca ch cn co cu cz de eg fr gb gr hk hu id ie il in it jp kr lt lv ma mx my ng nl no nz ph pl pt ro rs ru sa se sg si sk th tr tw ua us ve za".split()
12
-
13
- COUNTRIES = {'all': 'All Countries'}
14
- COUNTRIES.update({code: pycountry.countries.get(alpha_2=code.upper()).name for code in COUNTRY_CODES if pycountry.countries.get(alpha_2=code.upper())})
15
-
16
- def get_related_keywords(keyword):
17
- return [f"{keyword} news", f"{keyword} technology", f"{keyword} company", f"latest {keyword}"]
18
-
19
- def get_news(keyword, article_count, country):
20
- country_code = next((code for code, name in COUNTRIES.items() if name == country), 'all')
21
-
22
- if country_code == 'all':
23
- base_url = "https://newsapi.org/v2/everything"
24
- params = {
25
- 'apiKey': API_KEY,
26
- 'q': keyword,
27
- 'language': 'en',
28
- 'sortBy': 'publishedAt',
29
- 'pageSize': min(article_count * 2, 100) # ์š”์ฒญ ๊ธฐ์‚ฌ ์ˆ˜๋ฅผ 2๋ฐฐ๋กœ ๋Š˜๋ฆผ (์ตœ๋Œ€ 100)
30
- }
31
- else:
32
- base_url = "https://newsapi.org/v2/top-headlines"
33
- params = {
34
- 'apiKey': API_KEY,
35
- 'q': keyword,
36
- 'country': country_code,
37
- 'pageSize': min(article_count * 2, 100) # ์š”์ฒญ ๊ธฐ์‚ฌ ์ˆ˜๋ฅผ 2๋ฐฐ๋กœ ๋Š˜๋ฆผ (์ตœ๋Œ€ 100)
38
- }
39
-
40
- two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
41
- params['from'] = two_days_ago
42
-
43
- debug_info = f"API Request URL: {base_url}\n"
44
- debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"
45
-
46
- try:
47
- response = requests.get(base_url, params=params, timeout=10)
48
- response.raise_for_status()
49
- news_data = response.json()
50
-
51
- debug_info += f"API Response Status: {response.status_code}\n"
52
- debug_info += f"API Response Headers: {json.dumps(dict(response.headers), indent=2)}\n\n"
53
- debug_info += f"API Response Body: {json.dumps(news_data, indent=2)}\n\n"
54
-
55
- except requests.RequestException as e:
56
- return f"<p style='color: red;'>Error fetching news: {str(e)}</p><pre>{debug_info}</pre>"
57
-
58
- if news_data['status'] != 'ok':
59
- return f"<p style='color: red;'>API Error: {news_data.get('message', 'Unknown error occurred')}</p><pre>{debug_info}</pre>"
60
-
61
- articles = news_data['articles']
62
-
63
- # removed ๊ธฐ์‚ฌ ํ•„ํ„ฐ๋ง
64
- filtered_articles = [article for article in articles if article.get('title') != '[Removed]' and article.get('description') != '[Removed]']
65
-
66
- if not filtered_articles:
67
- if country_code != 'all':
68
- # ํŠน์ • ๊ตญ๊ฐ€์—์„œ ๊ฒฐ๊ณผ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ, ์ „์ฒด ๊ตญ๊ฐ€์—์„œ ๊ฒ€์ƒ‰
69
- return get_news(keyword, article_count, 'All Countries')
70
- else:
71
- related_keywords = get_related_keywords(keyword)
72
- suggestions = (f"<p>No news found for the keyword '<strong>{keyword}</strong>' in {country}.</p>"
73
- f"<p>Suggestions:</p>"
74
- f"<ul>"
75
- f"<li>Try one of these related keywords: {', '.join(related_keywords)}</li>"
76
- f"<li>Increase the number of articles</li>"
77
- f"<li>Check for any spelling errors in your keyword</li>"
78
- f"</ul>")
79
- return suggestions + f"<pre>{debug_info}</pre>"
80
-
81
- html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
82
- if country_code == 'all':
83
- html_output += "<p><em>Showing results from all countries</em></p>"
84
  else:
85
- html_output += f"<p><em>Showing results specifically for {country}</em></p>"
86
-
87
- html_output += f"<p>Found {len(filtered_articles)} relevant articles (after removing any '[Removed]' articles)</p>"
88
-
89
- for article in filtered_articles[:article_count]: # ์š”์ฒญํ•œ ๊ธฐ์‚ฌ ์ˆ˜๋งŒํผ๋งŒ ํ‘œ์‹œ
90
- title = article['title']
91
- link = article['url']
92
- pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
93
- source = article.get('source', {}).get('name', 'Unknown Source')
94
- description = article.get('description', 'No description available')
95
-
96
- html_output += f"""
97
- <div style='margin-bottom: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 5px;'>
98
- <h3><a href='{link}' target='_blank' style='text-decoration: none; color: #1a0dab;'>{title}</a></h3>
99
- <p style='color: #006621;'>{source}</p>
100
- <p style='color: #545454;'>{pub_date.strftime('%Y-%m-%d %H:%M:%S')}</p>
101
- <p>{description}</p>
102
- </div>
103
- """
104
 
105
- html_output += f"<details><summary>Debug Info</summary><pre>{debug_info}</pre></details>"
106
- return html_output
107
 
 
108
  iface = gr.Interface(
109
- fn=get_news,
110
  inputs=[
111
- gr.Textbox(label="Enter keyword"),
112
- gr.Slider(minimum=10, maximum=100, step=10, value=10, label="Number of Articles"),
113
- gr.Dropdown(choices=list(COUNTRIES.values()), value="All Countries", label="Select Country")
114
  ],
115
  outputs=gr.HTML(),
116
- title="Advanced Visual News Search",
117
- description="Search for news articles using NewsAPI. Results will be specific to the selected country when applicable.",
118
- theme=gr.themes.Soft()
119
  )
120
 
 
121
  iface.launch()
 
1
  import gradio as gr
2
  import requests
3
  from datetime import datetime, timedelta
 
 
4
 
5
+ # Google Custom Search API ํ‚ค์™€ ๊ฒ€์ƒ‰ ์—”์ง„ ID
6
+ API_KEY = "YOUR_API_KEY_HERE"
7
+ SEARCH_ENGINE_ID = "YOUR_SEARCH_ENGINE_ID_HERE"
8
+
9
+ # ์ง€์›๋˜๋Š” ๊ตญ๊ฐ€ ๋ฆฌ์ŠคํŠธ
10
+ COUNTRIES = {
11
+ 'United States': 'countryUS', 'United Kingdom': 'countryGB', 'India': 'countryIN',
12
+ 'Australia': 'countryAU', 'Canada': 'countryCA', 'Germany': 'countryDE',
13
+ 'France': 'countryFR', 'Italy': 'countryIT', 'Spain': 'countryES', 'Brazil': 'countryBR',
14
+ 'Mexico': 'countryMX', 'Argentina': 'countryAR', 'Japan': 'countryJP',
15
+ 'South Korea': 'countryKR', 'Russia': 'countryRU', 'China': 'countryCN',
16
+ 'Netherlands': 'countryNL', 'Sweden': 'countrySE', 'Poland': 'countryPL', 'Turkey': 'countryTR'
17
+ }
18
+
19
+ def search_news(keyword, country):
20
+ # 24์‹œ๊ฐ„ ์ „ ๋‚ ์งœ ๊ณ„์‚ฐ
21
+ one_day_ago = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
22
+
23
+ # API ์š”์ฒญ URL ๋ฐ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •
24
+ url = "https://www.googleapis.com/customsearch/v1"
25
+ params = {
26
+ 'key': API_KEY,
27
+ 'cx': SEARCH_ENGINE_ID,
28
+ 'q': keyword,
29
+ 'dateRestrict': 'd1', # ์ตœ๊ทผ 1์ผ ๋‚ด ๊ฒฐ๊ณผ๋งŒ
30
+ 'lr': 'lang_en', # ์˜์–ด ๊ฒฐ๊ณผ๋งŒ
31
+ 'sort': 'date', # ๋‚ ์งœ์ˆœ ์ •๋ ฌ
32
+ 'num': 10, # ์ตœ๋Œ€ 10๊ฐœ ๊ฒฐ๊ณผ
33
+ 'siteSearch': 'news.google.com', # Google News๋กœ ์ œํ•œ
34
+ }
35
+
36
+ if country != 'All Countries':
37
+ params['cr'] = COUNTRIES[country]
38
+
39
+ # API ์š”์ฒญ
40
+ response = requests.get(url, params=params)
41
+ results = response.json()
42
+
43
+ # ๊ฒฐ๊ณผ ํฌ๋งทํŒ…
44
+ formatted_results = ""
45
+ if 'items' in results:
46
+ for item in results['items']:
47
+ title = item['title']
48
+ link = item['link']
49
+ snippet = item['snippet']
50
+ formatted_results += f"<h3><a href='{link}' target='_blank'>{title}</a></h3>"
51
+ formatted_results += f"<p>{snippet}</p><br>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  else:
53
+ formatted_results = f"No news found for '{keyword}' in {country} within the last 24 hours."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ return formatted_results
 
56
 
57
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
58
  iface = gr.Interface(
59
+ fn=search_news,
60
  inputs=[
61
+ gr.Textbox(label="Enter keyword (in English)"),
62
+ gr.Dropdown(choices=['All Countries'] + list(COUNTRIES.keys()), label="Select Country")
 
63
  ],
64
  outputs=gr.HTML(),
65
+ title="Google News Search",
66
+ description="Search for news articles from the last 24 hours using Google Custom Search API."
 
67
  )
68
 
69
+ # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
70
  iface.launch()