import gradio as gr import requests import json from datetime import datetime, timedelta API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6" MAJOR_COUNTRIES = [ "United States", "United Kingdom", "Canada", "Australia", "Germany", "France", "Japan", "South Korea", "China", "India", "Brazil", "Mexico", "Russia", "Italy", "Spain", "Netherlands", "Sweden", "Switzerland", "Norway", "Denmark", "Finland", "Belgium", "Austria", "New Zealand", "Ireland", "Singapore", "Hong Kong", "Israel", "United Arab Emirates", "Saudi Arabia", "South Africa", "Turkey", "Egypt", "Poland", "Czech Republic", "Hungary", "Greece", "Portugal", "Argentina", "Chile", "Colombia", "Peru", "Venezuela", "Thailand", "Malaysia", "Indonesia", "Philippines", "Vietnam", "Pakistan", "Bangladesh" ] def search_serphouse(query, country, page, num_result): url = "https://api.serphouse.com/serp/live" now = datetime.utcnow() yesterday = now - timedelta(days=1) date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}" payload = { "data": { "q": query, "domain": "google.com", "loc": country, "lang": "en", "device": "desktop", "serp_type": "news", "page": str(page), "verbatim": "1", "num": str(num_result), "date_range": date_range } } headers = { "accept": "application/json", "content-type": "application/json", "authorization": f"Bearer {API_KEY}" } try: response = requests.post(url, json=payload, headers=headers) response.raise_for_status() return response.json() except requests.RequestException as e: error_msg = f"Error: {str(e)}" if response.text: error_msg += f"\nResponse content: {response.text}" return {"error": error_msg} def format_results(results): all_results = "
오류 발생: {results['error']}
" debug_info += f"{results['error']}" return all_results, debug_info debug_info += f"
{json.dumps(results, indent=2, ensure_ascii=False)}" try: if not isinstance(results, dict): raise ValueError("결과가 사전 형식이 아닙니다.") if "results" not in results: raise ValueError("'results' 키가 응답에 없습니다.") news_results = results["results"].get("news", []) debug_info += f"
뉴스 결과 수: {len(news_results)}
" if not news_results: all_results += "검색 결과가 없습니다.
" else: all_results += "{snippet}
출처: {channel} - {time_str}
{error_message}
" all_results += f"{error_message}
" return all_results, debug_info def serphouse_search(query, country, page, num_result): results = search_serphouse(query, country, page, num_result) all_results, debug_info = format_results(results) return all_results, debug_info css = """ footer { visibility: hidden; } ol { padding-left: 20px; } li { margin-bottom: 20px; } """ iface = gr.Interface( fn=serphouse_search, inputs=[ gr.Textbox(label="검색어"), gr.Dropdown(MAJOR_COUNTRIES, label="국가"), gr.Slider(1, 10, 1, label="페이지"), gr.Slider(1, 100, 10, label="결과 수") ], outputs=[ gr.HTML(label="모든 결과"), gr.HTML(label="디버그 정보") ], title="24시간 이내 뉴스 검색 인터페이스", description="검색어를 입력하고 국가를 선택하여 24시간 이내의 뉴스 결과를 가져옵니다.", theme="Nymbo/Nymbo_Theme", css=css ) iface.launch(auth=("gini","pick"))