Spaces:
Sleeping
Sleeping
import streamlit as st | |
import requests | |
import xml.etree.ElementTree as ET | |
import time | |
def get_news_from_rss(rss_url): | |
"""RSS ํผ๋์์ ๋ด์ค๋ฅผ ๊ฐ์ ธ์ต๋๋ค.""" | |
try: | |
response = requests.get(rss_url) | |
if response.status_code == 200: | |
root = ET.fromstring(response.content) | |
items = root.findall(".//item") | |
news_list = [] | |
for item in items: | |
title = item.find("title").text | |
link = item.find("link").text | |
news_list.append({"title": title, "url": link}) | |
return news_list | |
except Exception as e: | |
print(f"Error fetching RSS feed: {e}") | |
return [] | |
def display_news(news_items): | |
"""๋ด์ค๋ฅผ ์์ฝ ๋ฐ ํ์ํฉ๋๋ค.""" | |
if not news_items: | |
st.warning("๋ด์ค๋ฅผ ๊ฐ์ ธ์ค๋๋ฐ ์คํจํ์ต๋๋ค.") | |
return | |
for item in news_items: | |
original_title = item.get('title', '') | |
if not original_title: | |
continue | |
url = item.get('url', '#') # ๊ธฐ์ฌ URL์ด ์์ผ๋ฉด ๊ธฐ๋ณธ ๋งํฌ๋ '#' | |
st.markdown(f''' | |
<div class="headline-container"> | |
<a href="{url}" target="_blank" style="text-decoration: none; pointer-events: auto;"> | |
<div class="headline-text">{original_title}</div> | |
</a> | |
</div> | |
''', unsafe_allow_html=True) | |
def main(): | |
"""์ฑ ๋ฉ์ธ ํจ์""" | |
st.set_page_config(layout="wide") | |
st.markdown(""" | |
<style> | |
.headline-container { | |
background-color: rgba(255, 255, 255, 0.9); | |
padding: 15px 20px; | |
margin: 15px 0; | |
border-radius: 3px; | |
text-align: center; | |
} | |
.headline-text { | |
color: #000000; | |
font-size: 40px; | |
font-weight: bold; | |
font-family: 'Noto Sans KR', sans-serif; | |
pointer-events: auto; /* ํด๋ฆญ ๊ฐ๋ฅํ๋๋ก ์ค์ */ | |
} | |
.stApp { | |
background-image: url("https://huggingface.co/spaces/JAMESPARK3/headlineai/resolve/main/news.jpg"); | |
background-size: cover; | |
background-repeat: no-repeat; | |
background-position: center; | |
} | |
.stRadio > div > label { | |
font-size: 30px; | |
font-weight: bold; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
# ๋ด์ค ์ ํ ๋ฒํผ ์ถ๊ฐ | |
news_source = st.sidebar.radio("๋ด์ค ์ ํ", ["jtbc ๋ด์ค", "์ฐํฉ๋ด์ค", "SBS ๋ด์ค"]) | |
if news_source == "jtbc ๋ด์ค": | |
rss_url = "https://news-ex.jtbc.co.kr/v1/get/rss/issue" | |
elif news_source == "์ฐํฉ๋ด์ค": | |
rss_url = "http://www.yonhapnewstv.co.kr/category/news/headline/feed/" | |
else: | |
rss_url = "https://news.sbs.co.kr/news/headlineRssFeed.do?plink=RSSREADER" | |
news_items = get_news_from_rss(rss_url) | |
display_news(news_items) | |
# ์๋ก๊ณ ์นจ ํ์ด๋จธ ์ค์ | |
if 'last_run' not in st.session_state: | |
st.session_state.last_run = time.time() | |
current_time = time.time() | |
if current_time - st.session_state.last_run >= 3600: # 1์๊ฐ ๊ฒฝ๊ณผ | |
st.session_state.last_run = current_time | |
st.rerun() # ์๋ก๊ณ ์นจ | |
if __name__ == "__main__": | |
main() | |