-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
134 lines (111 loc) · 3.93 KB
/
Copy pathstreamlit_app.py
File metadata and controls
134 lines (111 loc) · 3.93 KB
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
import base64
import threading
from pathlib import Path
import streamlit as st
_logo_path = Path(__file__).parent / "xpose-logo.jpg"
if _logo_path.exists():
_jpg_b64 = base64.b64encode(_logo_path.read_bytes()).decode()
_icon = f"data:image/jpeg;base64,{_jpg_b64}"
else:
_icon = "🛠️"
st.set_page_config(
page_title="X-POSE | 悟空X照妖镜",
page_icon=_icon,
layout="wide",
)
st.markdown(
"""
<style>
#MainMenu, .stDeployButton, [data-testid="stStatusWidget"], [data-testid="stSidebarNav"] { visibility: hidden; display: none; }
div[data-testid="stTextInput"] { min-width: 380px; }
</style>
""",
unsafe_allow_html=True,
)
from i18n import _
@st.cache_resource
def get_global_limiter():
return threading.BoundedSemaphore(value=1)
def _init():
defaults = {
"lang": "zh",
"scraped_user": None,
"scraped_tweets": None,
"analysis_result": None,
"scrape_username": "",
}
for k, v in defaults.items():
if k not in st.session_state:
st.session_state[k] = v
_init()
from lib.sidebar import render_sidebar
render_sidebar()
title_col, title_col3 = st.columns([10, 5])
with title_col:
logo_html = ""
logo_path = Path(__file__).parent / "xpose-logo.jpg"
if logo_path.exists():
b64 = base64.b64encode(logo_path.read_bytes()).decode()
logo_html = f'<img src="data:image/jpeg;base64,{b64}" style="width:42px;height:42px;vertical-align:middle;margin-right:10px;">'
st.markdown(
f"<h1 style='margin:0;display:flex;align-items:center;'>{logo_html}{_('title')}</h1>",
unsafe_allow_html=True,
)
with title_col3:
lang_opts = {"简体中文": "zh", "English": "en"}
idx = list(lang_opts.values()).index(st.session_state.lang)
sel = st.radio(
"Lang",
options=list(lang_opts.keys()),
index=idx,
horizontal=True,
label_visibility="collapsed",
)
if lang_opts[sel] != st.session_state.lang:
st.session_state.lang = lang_opts[sel]
st.rerun()
st.divider()
st.markdown(f"### {_('home_heading')}")
st.caption(_("home_desc"))
col_in, _col_spacer = st.columns([3, 1])
with col_in:
username = st.text_input(
_("input_username_label"),
placeholder="@username 或 https://x.com/username",
key="home_username",
label_visibility="collapsed",
).strip().removeprefix("https://x.com/").removeprefix("https://twitter.com/").lstrip("@")
col_btn, _btn_spacer = st.columns([1, 1])
with col_btn:
if st.button(_("btn_analyze"), type="primary", use_container_width=True):
if not username:
st.warning(_("warn_empty_username"))
else:
sem = get_global_limiter()
can_run = sem.acquire(blocking=False)
if not can_run:
st.warning(_("warn_concurrency"))
else:
try:
from scraper import fetch_all
with st.spinner(_("spinner_scraping")):
st.info(_("scrape_user") + f" @{username}...")
user, tweets = fetch_all(username)
st.session_state.scraped_user = user
st.session_state.scraped_tweets = tweets
st.session_state.analysis_result = None
st.session_state.scrape_username = username
except Exception as e:
st.error(f"❌ {e}")
finally:
sem.release()
# 爬取成功后在顶层显示跳转按钮(避免嵌套 button 导致无法点击)
if st.session_state.get("scraped_user") is not None:
uname = st.session_state.scrape_username
st.success(
f"✅ @{uname} — {len(st.session_state.scraped_tweets)} tweets scraped"
)
if st.button(_("goto_analysis"), type="primary", use_container_width=True):
st.switch_page("pages/1_Analysis.py")
st.divider()
st.caption(_("footer"))