File size: 10,374 Bytes
09ebcd0
 
 
84aff1b
09ebcd0
1b298f9
464c233
 
1b298f9
8a9ead8
 
1b298f9
8a9ead8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b298f9
 
28c3288
1b298f9
 
 
 
 
 
 
 
 
 
 
 
28c3288
1b298f9
 
 
28c3288
1b298f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f166d5c
1b298f9
68a8b0c
1b298f9
68a8b0c
28c3288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b298f9
ab30506
28c3288
ab30506
1b298f9
28c3288
 
ad0f104
b659602
1b298f9
28c3288
ad0f104
 
84aff1b
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import gradio as gr
import requests
from datetime import datetime, timedelta
import concurrent.futures

# Google Custom Search API ํ‚ค์™€ ๊ฒ€์ƒ‰ ์—”์ง„ ID
API_KEY = "AIzaSyDUz3wkGal0ewRtPlzeMit88bV4hS4ZIVY"
SEARCH_ENGINE_ID = "56b34994f47704ddd"


# ์ง€์›๋˜๋Š” ๊ตญ๊ฐ€ ๋ฆฌ์ŠคํŠธ (Google Custom Search API์—์„œ ์ง€์›ํ•˜๋Š” ๋ชจ๋“  ๊ตญ๊ฐ€)
COUNTRIES = {
    'Afghanistan': 'countryAF', 'Albania': 'countryAL', 'Algeria': 'countryDZ', 'American Samoa': 'countryAS', 
    'Andorra': 'countryAD', 'Angola': 'countryAO', 'Anguilla': 'countryAI', 'Antarctica': 'countryAQ', 
    'Antigua and Barbuda': 'countryAG', 'Argentina': 'countryAR', 'Armenia': 'countryAM', 'Aruba': 'countryAW', 
    'Australia': 'countryAU', 'Austria': 'countryAT', 'Azerbaijan': 'countryAZ', 'Bahamas': 'countryBS', 
    'Bahrain': 'countryBH', 'Bangladesh': 'countryBD', 'Barbados': 'countryBB', 'Belarus': 'countryBY', 
    'Belgium': 'countryBE', 'Belize': 'countryBZ', 'Benin': 'countryBJ', 'Bermuda': 'countryBM', 
    'Bhutan': 'countryBT', 'Bolivia': 'countryBO', 'Bosnia and Herzegovina': 'countryBA', 'Botswana': 'countryBW', 
    'Bouvet Island': 'countryBV', 'Brazil': 'countryBR', 'British Indian Ocean Territory': 'countryIO', 
    'Brunei Darussalam': 'countryBN', 'Bulgaria': 'countryBG', 'Burkina Faso': 'countryBF', 'Burundi': 'countryBI', 
    'Cambodia': 'countryKH', 'Cameroon': 'countryCM', 'Canada': 'countryCA', 'Cape Verde': 'countryCV', 
    'Cayman Islands': 'countryKY', 'Central African Republic': 'countryCF', 'Chad': 'countryTD', 'Chile': 'countryCL', 
    'China': 'countryCN', 'Christmas Island': 'countryCX', 'Cocos (Keeling) Islands': 'countryCC', 'Colombia': 'countryCO', 
    'Comoros': 'countryKM', 'Congo': 'countryCG', 'Congo, Democratic Republic': 'countryCD', 'Cook Islands': 'countryCK', 
    'Costa Rica': 'countryCR', "Cote D'Ivoire": 'countryCI', 'Croatia': 'countryHR', 'Cuba': 'countryCU', 'Cyprus': 'countryCY', 
    'Czech Republic': 'countryCZ', 'Denmark': 'countryDK', 'Djibouti': 'countryDJ', 'Dominica': 'countryDM', 
    'Dominican Republic': 'countryDO', 'Ecuador': 'countryEC', 'Egypt': 'countryEG', 'El Salvador': 'countrySV', 
    'Equatorial Guinea': 'countryGQ', 'Eritrea': 'countryER', 'Estonia': 'countryEE', 'Ethiopia': 'countryET', 
    'Falkland Islands (Malvinas)': 'countryFK', 'Faroe Islands': 'countryFO', 'Fiji': 'countryFJ', 'Finland': 'countryFI', 
    'France': 'countryFR', 'French Guiana': 'countryGF', 'French Polynesia': 'countryPF', 'French Southern Territories': 'countryTF', 
    'Gabon': 'countryGA', 'Gambia': 'countryGM', 'Georgia': 'countryGE', 'Germany': 'countryDE', 'Ghana': 'countryGH', 
    'Gibraltar': 'countryGI', 'Greece': 'countryGR', 'Greenland': 'countryGL', 'Grenada': 'countryGD', 'Guadeloupe': 'countryGP', 
    'Guam': 'countryGU', 'Guatemala': 'countryGT', 'Guinea': 'countryGN', 'Guinea-Bissau': 'countryGW', 'Guyana': 'countryGY', 
    'Haiti': 'countryHT', 'Heard Island and Mcdonald Islands': 'countryHM', 'Holy See (Vatican City State)': 'countryVA', 
    'Honduras': 'countryHN', 'Hong Kong': 'countryHK', 'Hungary': 'countryHU', 'Iceland': 'countryIS', 'India': 'countryIN', 
    'Indonesia': 'countryID', 'Iran, Islamic Republic of': 'countryIR', 'Iraq': 'countryIQ', 'Ireland': 'countryIE', 
    'Israel': 'countryIL', 'Italy': 'countryIT', 'Jamaica': 'countryJM', 'Japan': 'countryJP', 'Jordan': 'countryJO', 
    'Kazakhstan': 'countryKZ', 'Kenya': 'countryKE', 'Kiribati': 'countryKI', "Korea, Democratic People's Republic of": 'countryKP', 
    'Korea, Republic of': 'countryKR', 'Kuwait': 'countryKW', 'Kyrgyzstan': 'countryKG', "Lao People's Democratic Republic": 'countryLA', 
    'Latvia': 'countryLV', 'Lebanon': 'countryLB', 'Lesotho': 'countryLS', 'Liberia': 'countryLR', 'Libyan Arab Jamahiriya': 'countryLY', 
    'Liechtenstein': 'countryLI', 'Lithuania': 'countryLT', 'Luxembourg': 'countryLU', 'Macao': 'countryMO', 
    'Macedonia, The Former Yugoslav Republic of': 'countryMK', 'Madagascar': 'countryMG', 'Malawi': 'countryMW', 
    'Malaysia': 'countryMY', 'Maldives': 'countryMV', 'Mali': 'countryML', 'Malta': 'countryMT', 'Marshall Islands': 'countryMH', 
    'Martinique': 'countryMQ', 'Mauritania': 'countryMR', 'Mauritius': 'countryMU', 'Mayotte': 'countryYT', 'Mexico': 'countryMX', 
    'Micronesia, Federated States of': 'countryFM', 'Moldova, Republic of': 'countryMD', 'Monaco': 'countryMC', 'Mongolia': 'countryMN', 
    'Montserrat': 'countryMS', 'Morocco': 'countryMA', 'Mozambique': 'countryMZ', 'Myanmar': 'countryMM', 'Namibia': 'countryNA', 
    'Nauru': 'countryNR', 'Nepal': 'countryNP', 'Netherlands': 'countryNL', 'Netherlands Antilles': 'countryAN', 'New Caledonia': 'countryNC', 
    'New Zealand': 'countryNZ', 'Nicaragua': 'countryNI', 'Niger': 'countryNE', 'Nigeria': 'countryNG', 'Niue': 'countryNU', 
    'Norfolk Island': 'countryNF', 'Northern Mariana Islands': 'countryMP', 'Norway': 'countryNO', 'Oman': 'countryOM', 
    'Pakistan': 'countryPK', 'Palau': 'countryPW', 'Palestinian Territory, Occupied': 'countryPS', 'Panama': 'countryPA', 
    'Papua New Guinea': 'countryPG', 'Paraguay': 'countryPY', 'Peru': 'countryPE', 'Philippines': 'countryPH', 'Pitcairn': 'countryPN', 
    'Poland': 'countryPL', 'Portugal': 'countryPT', 'Puerto Rico': 'countryPR', 'Qatar': 'countryQA', 'Reunion': 'countryRE', 
    'Romania': 'countryRO', 'Russian Federation': 'countryRU', 'Rwanda': 'countryRW', 'Saint Helena': 'countrySH', 
    'Saint Kitts and Nevis': 'countryKN', 'Saint Lucia': 'countryLC', 'Saint Pierre and Miquelon': 'countryPM', 
    'Saint Vincent and the Grenadines': 'countryVC', 'Samoa': 'countryWS', 'San Marino': 'countrySM', 'Sao Tome and Principe': 'countryST', 
    'Saudi Arabia': 'countrySA', 'Senegal': 'countrySN', 'Serbia and Montenegro': 'countryCS', 'Seychelles': 'countrySC', 
    'Sierra Leone': 'countrySL', 'Singapore': 'countrySG', 'Slovakia': 'countrySK', 'Slovenia': 'countrySI', 'Solomon Islands': 'countrySB', 
    'Somalia': 'countrySO', 'South Africa': 'countryZA', 'South Georgia and the South Sandwich Islands': 'countryGS', 'Spain': 'countryES', 
    'Sri Lanka': 'countryLK', 'Sudan': 'countrySD', 'Suriname': 'countrySR', 'Svalbard and Jan Mayen': 'countrySJ', 'Swaziland': 'countrySZ', 
    'Sweden': 'countrySE', 'Switzerland': 'countryCH', 'Syrian Arab Republic': 'countrySY', 'Taiwan, Province of China': 'countryTW', 
    'Tajikistan': 'countryTJ', 'Tanzania, United Republic of': 'countryTZ', 'Thailand': 'countryTH', 'Timor-Leste': 'countryTL', 
    'Togo': 'countryTG', 'Tokelau': 'countryTK', 'Tonga': 'countryTO', 'Trinidad and Tobago': 'countryTT', 'Tunisia': 'countryTN', 
    'Turkey': 'countryTR', 'Turkmenistan': 'countryTM', 'Turks and Caicos Islands': 'countryTC', 'Tuvalu': 'countryTV', 'Uganda': 'countryUG', 
    'Ukraine': 'countryUA', 'United Arab Emirates': 'countryAE', 'United Kingdom': 'countryGB', 'United States': 'countryUS', 
    'United States Minor Outlying Islands': 'countryUM', 'Uruguay': 'countryUY', 'Uzbekistan': 'countryUZ', 'Vanuatu': 'countryVU', 
    'Venezuela': 'countryVE', 'Viet Nam': 'countryVN', 'Virgin Islands, British': 'countryVG', 'Virgin Islands, U.S.': 'countryVI', 
    'Wallis and Futuna': 'countryWF', 'Western Sahara': 'countryEH', 'Yemen': 'countryYE', 'Zambia': 'countryZM', 'Zimbabwe': 'countryZW'
}

def search_news(keyword, country, search_all_countries=False):
    # 24์‹œ๊ฐ„ ์ „ ๋‚ ์งœ ๊ณ„์‚ฐ
    one_day_ago = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")

    # API ์š”์ฒญ URL ๋ฐ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •
    url = "https://www.googleapis.com/customsearch/v1"
    params = {
        'key': API_KEY,
        'cx': SEARCH_ENGINE_ID,
        'q': keyword,
        'dateRestrict': 'd1',  # ์ตœ๊ทผ 1์ผ ๋‚ด ๊ฒฐ๊ณผ๋งŒ
        'lr': 'lang_en',  # ์˜์–ด ๊ฒฐ๊ณผ๋งŒ
        'sort': 'date',  # ๋‚ ์งœ์ˆœ ์ •๋ ฌ
        'num': 100,  # ์ตœ๋Œ€ 100๊ฐœ ๊ฒฐ๊ณผ
        'siteSearch': 'news.google.com',  # Google News๋กœ ์ œํ•œ
    }

    if not search_all_countries and country != 'All Countries':
        params['cr'] = COUNTRIES[country]

    # API ์š”์ฒญ
    response = requests.get(url, params=params)
    results = response.json()

    # ๊ฒฐ๊ณผ ํฌ๋งทํŒ…
    formatted_results = ""
    if 'items' in results:
        for item in results['items']:
            title = item['title']
            link = item['link']
            snippet = item['snippet']
            formatted_results += f"<h3><a href='{link}' target='_blank'>{title}</a></h3>"
            formatted_results += f"<p>{snippet}</p><br>"
    else:
        formatted_results = f"No news found for '{keyword}' in {country} within the last 24 hours."

    return formatted_results

def search_all_countries(keyword):
    def search_country(country_name):
        result = search_news(keyword, country_name)
        if "No news found" not in result:
            return country_name, result
        return None

    with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
        future_to_country = {executor.submit(search_country, country): country for country in COUNTRIES.keys()}
        matching_countries = []
        for future in concurrent.futures.as_completed(future_to_country):
            result = future.result()
            if result:
                matching_countries.append(result)

    if matching_countries:
        output = "<h2>Countries with matching news:</h2>"
        for country, news in matching_countries:
            output += f"<h3>{country}</h3>{news}<hr>"
        return output
    else:
        return f"No news found for '{keyword}' in any country within the last 24 hours."

def news_search(keyword, country, search_all):
    if search_all:
        return search_all_countries(keyword)
    else:
        return search_news(keyword, country)

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
iface = gr.Interface(
    fn=news_search,
    inputs=[
        gr.Textbox(label="Enter keyword (in English)"),
        gr.Dropdown(choices=['All Countries'] + list(COUNTRIES.keys()), label="Select Country"),
        gr.Checkbox(label="Search all countries")
    ],
    outputs=gr.HTML(),
    title="Google News Search",
    description="Search for news articles from the last 24 hours using Google Custom Search API. You can search in a specific country or across all countries."
)

# ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ (๊ณต๊ฐœ ๋งํฌ ์ƒ์„ฑ)
iface.launch(share=True)