File size: 5,366 Bytes
09ebcd0
 
 
 
 
 
4409afe
1ac298a
09ebcd0
9d69618
1d7a9d5
 
 
5947621
1d7a9d5
9d69618
 
 
f166d5c
5947621
4409afe
 
 
 
 
 
 
 
16e9e43
4409afe
 
 
 
 
dac6155
4409afe
16e9e43
4409afe
68a8b0c
f166d5c
 
1d7a9d5
09ebcd0
 
 
f42fdaf
68a8b0c
f42fdaf
68a8b0c
09ebcd0
 
 
 
 
f42fdaf
09ebcd0
f42fdaf
68a8b0c
09ebcd0
dac6155
 
ab30506
16e9e43
 
 
 
f166d5c
 
 
ed4ef14
9d69618
 
 
 
 
 
 
 
 
ab30506
4409afe
f166d5c
 
 
 
ed4ef14
16e9e43
 
 
68a8b0c
 
 
b659602
dac6155
b659602
 
 
 
 
 
dac6155
b659602
 
68a8b0c
09ebcd0
b659602
68a8b0c
ab30506
 
 
1d7a9d5
 
 
ad0f104
b659602
9d69618
f166d5c
b659602
ad0f104
 
ab30506
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import gradio as gr
import requests
from datetime import datetime, timedelta
import pycountry
import json

# NewsAPI key
API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"


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()

COUNTRIES = {'all': 'All Countries'}
COUNTRIES.update({code: pycountry.countries.get(alpha_2=code.upper()).name for code in COUNTRY_CODES if pycountry.countries.get(alpha_2=code.upper())})

def get_related_keywords(keyword):
    return [f"{keyword} news", f"{keyword} technology", f"{keyword} company", f"latest {keyword}"]

def get_news(keyword, article_count, country):
    country_code = next((code for code, name in COUNTRIES.items() if name == country), 'all')
    
    if country_code == 'all':
        base_url = "https://newsapi.org/v2/everything"
        params = {
            'apiKey': API_KEY,
            'q': keyword,
            'language': 'en',
            'sortBy': 'publishedAt',
            'pageSize': min(article_count * 2, 100)  # 요청 기사 수를 2배로 늘림 (최대 100)
        }
    else:
        base_url = "https://newsapi.org/v2/top-headlines"
        params = {
            'apiKey': API_KEY,
            'q': keyword,
            'country': country_code,
            'pageSize': min(article_count * 2, 100)  # 요청 기사 수를 2배로 늘림 (최대 100)
        }
    
    two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
    params['from'] = two_days_ago

    debug_info = f"API Request URL: {base_url}\n"
    debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"

    try:
        response = requests.get(base_url, params=params, timeout=10)
        response.raise_for_status()
        news_data = response.json()

        debug_info += f"API Response Status: {response.status_code}\n"
        debug_info += f"API Response Headers: {json.dumps(dict(response.headers), indent=2)}\n\n"
        debug_info += f"API Response Body: {json.dumps(news_data, indent=2)}\n\n"

    except requests.RequestException as e:
        return f"<p style='color: red;'>Error fetching news: {str(e)}</p><pre>{debug_info}</pre>"

    if news_data['status'] != 'ok':
        return f"<p style='color: red;'>API Error: {news_data.get('message', 'Unknown error occurred')}</p><pre>{debug_info}</pre>"

    articles = news_data['articles']
    
    # removed 기사 필터링
    filtered_articles = [article for article in articles if article.get('title') != '[Removed]' and article.get('description') != '[Removed]']
    
    if not filtered_articles:
        if country_code != 'all':
            # 특정 국가에서 결과가 없을 경우, 전체 국가에서 검색
            return get_news(keyword, article_count, 'All Countries')
        else:
            related_keywords = get_related_keywords(keyword)
            suggestions = (f"<p>No news found for the keyword '<strong>{keyword}</strong>' in {country}.</p>"
                           f"<p>Suggestions:</p>"
                           f"<ul>"
                           f"<li>Try one of these related keywords: {', '.join(related_keywords)}</li>"
                           f"<li>Increase the number of articles</li>"
                           f"<li>Check for any spelling errors in your keyword</li>"
                           f"</ul>")
            return suggestions + f"<pre>{debug_info}</pre>"

    html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
    if country_code == 'all':
        html_output += "<p><em>Showing results from all countries</em></p>"
    else:
        html_output += f"<p><em>Showing results specifically for {country}</em></p>"
    
    html_output += f"<p>Found {len(filtered_articles)} relevant articles (after removing any '[Removed]' articles)</p>"
    
    for article in filtered_articles[:article_count]:  # 요청한 기사 수만큼만 표시
        title = article['title']
        link = article['url']
        pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
        source = article.get('source', {}).get('name', 'Unknown Source')
        description = article.get('description', 'No description available')
        
        html_output += f"""
        <div style='margin-bottom: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 5px;'>
            <h3><a href='{link}' target='_blank' style='text-decoration: none; color: #1a0dab;'>{title}</a></h3>
            <p style='color: #006621;'>{source}</p>
            <p style='color: #545454;'>{pub_date.strftime('%Y-%m-%d %H:%M:%S')}</p>
            <p>{description}</p>
        </div>
        """

    html_output += f"<details><summary>Debug Info</summary><pre>{debug_info}</pre></details>"
    return html_output

iface = gr.Interface(
    fn=get_news,
    inputs=[
        gr.Textbox(label="Enter keyword"),
        gr.Slider(minimum=10, maximum=100, step=10, value=10, label="Number of Articles"),
        gr.Dropdown(choices=list(COUNTRIES.values()), value="All Countries", label="Select Country")
    ],
    outputs=gr.HTML(),
    title="Advanced Visual News Search",
    description="Search for news articles using NewsAPI. Results will be specific to the selected country when applicable.",
    theme=gr.themes.Soft()
)

iface.launch()