-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultisync.py
More file actions
888 lines (767 loc) · 38.8 KB
/
multisync.py
File metadata and controls
888 lines (767 loc) · 38.8 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
import streamlit as st
import requests
import json
from datetime import datetime, timedelta
import io
import base64
# 尝试导入可选的第三方库
try:
import tweepy
TWITTER_AVAILABLE = True
except ImportError:
TWITTER_AVAILABLE = False
try:
from PIL import Image
PIL_AVAILABLE = True
except ImportError:
PIL_AVAILABLE = False
# LinkedIn 使用标准 requests 库,无需额外依赖
TELEGRAM_AVAILABLE = True
INSTAGRAM_AVAILABLE = True
# 页面配置
st.set_page_config(
page_title="多平台发布工具",
page_icon="📱",
layout="wide",
initial_sidebar_state="expanded"
)
# 添加JavaScript代码来处理浏览器缓存
st.markdown("""
<script>
// 保存到浏览器缓存
function saveToCache(key, value) {
try {
localStorage.setItem('social_media_' + key, value);
} catch(e) {
console.log('LocalStorage not available');
}
}
// 从浏览器缓存读取
function getFromCache(key) {
try {
return localStorage.getItem('social_media_' + key) || '';
} catch(e) {
console.log('LocalStorage not available');
return '';
}
}
// 清除缓存
function clearCache() {
try {
const keys = Object.keys(localStorage);
keys.forEach(key => {
if (key.startsWith('social_media_')) {
localStorage.removeItem(key);
}
});
} catch(e) {
console.log('LocalStorage not available');
}
}
// 暴露函数给Streamlit
window.saveToCache = saveToCache;
window.getFromCache = getFromCache;
window.clearCache = clearCache;
</script>
""", unsafe_allow_html=True)
# 应用标题
st.title("📱 多平台社交媒体发布工具")
st.markdown("*无需第三方服务,直接连接各平台API*")
# 检查依赖状态
st.sidebar.header("📦 依赖状态")
dependencies_status = {
"✅ Streamlit": True,
"✅ Requests": True,
"📷 PIL/Pillow": PIL_AVAILABLE,
"🐦 Twitter (tweepy)": TWITTER_AVAILABLE,
"📨 Telegram": TELEGRAM_AVAILABLE,
"📸 Instagram": INSTAGRAM_AVAILABLE,
}
for dep, status in dependencies_status.items():
if status:
st.sidebar.success(dep)
else:
st.sidebar.error(dep + " - 未安装")
if not PIL_AVAILABLE:
st.sidebar.warning("⚠️ PIL 未安装,图片功能受限")
# 初始化 session state
if 'authenticated_platforms' not in st.session_state:
st.session_state.authenticated_platforms = {}
if 'publish_history' not in st.session_state:
st.session_state.publish_history = []
if 'api_credentials' not in st.session_state:
st.session_state.api_credentials = {
'twitter_api_key': '',
'twitter_api_secret': '',
'twitter_access_token': '',
'twitter_access_secret': '',
'telegram_bot_token': '',
'telegram_channel_id': '',
'instagram_access_token': '',
'instagram_user_id': ''
}
# 辅助函数:安全地获取缓存的凭据
def get_cached_credential(key, default=""):
"""安全地获取缓存的凭据"""
if key in st.session_state.api_credentials:
return st.session_state.api_credentials[key]
return default
# 辅助函数:保存凭据到session state
def save_credential(key, value):
"""保存凭据到session state"""
st.session_state.api_credentials[key] = value
# 发布函数定义(需要在调用前定义)
def publish_to_twitter(content, twitter_config, media_files=None):
"""发布到 Twitter,支持图片上传"""
try:
client = twitter_config['client']
# 检查内容长度
if len(content) > 280:
return {'success': False, 'error': '内容超过 280 字符限制'}
# 处理图片上传
media_ids = []
if media_files:
# 创建 API v1.1 客户端用于媒体上传
auth = tweepy.OAuth1UserHandler(
twitter_config.get('consumer_key'),
twitter_config.get('consumer_secret'),
twitter_config.get('access_token'),
twitter_config.get('access_token_secret')
)
api_v1 = tweepy.API(auth)
for media_file in media_files[:4]: # Twitter 最多支持4张图片
try:
# 将上传的文件转换为字节
media_file.seek(0) # 重置文件指针
media_data = media_file.read()
# 上传媒体
media = api_v1.media_upload(filename=media_file.name, file=io.BytesIO(media_data))
media_ids.append(media.media_id)
except Exception as e:
st.warning(f"图片 {media_file.name} 上传失败: {str(e)}")
# 发布推文
if media_ids:
response = client.create_tweet(text=content, media_ids=media_ids)
else:
response = client.create_tweet(text=content)
return {'success': True, 'post_id': response.data['id'], 'media_count': len(media_ids)}
except Exception as e:
return {'success': False, 'error': str(e)}
def publish_to_telegram(content, telegram_config, media_files=None):
"""发布到 Telegram 频道,支持图片"""
try:
bot_token = telegram_config['bot_token']
channel_id = telegram_config['channel_id']
# 如果有图片,发送图片+文字
if media_files:
# Telegram 支持多种媒体类型
if len(media_files) == 1:
# 单张图片
media_file = media_files[0]
media_file.seek(0)
url = f"https://api.telegram.org/bot{bot_token}/sendPhoto"
files = {'photo': (media_file.name, media_file, 'image/jpeg')}
data = {
'chat_id': channel_id,
'caption': content,
'parse_mode': 'HTML'
}
response = requests.post(url, data=data, files=files)
else:
# 多张图片 - 使用 media group
media_group = []
files = {}
for i, media_file in enumerate(media_files[:10]): # Telegram 最多10张
media_file.seek(0)
file_key = f"photo{i}"
files[file_key] = (media_file.name, media_file, 'image/jpeg')
media_item = {
'type': 'photo',
'media': f'attach://{file_key}'
}
# 第一张图片添加caption
if i == 0:
media_item['caption'] = content
media_item['parse_mode'] = 'HTML'
media_group.append(media_item)
url = f"https://api.telegram.org/bot{bot_token}/sendMediaGroup"
data = {
'chat_id': channel_id,
'media': json.dumps(media_group)
}
response = requests.post(url, data=data, files=files)
else:
# 纯文本消息
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
data = {
'chat_id': channel_id,
'text': content,
'parse_mode': 'HTML',
'disable_web_page_preview': False
}
response = requests.post(url, data=data)
if response.status_code == 200:
result = response.json()
if result['ok']:
message_id = result['result']['message_id'] if 'message_id' in result['result'] else result['result'][0]['message_id']
return {'success': True, 'post_id': message_id}
else:
return {'success': False, 'error': result.get('description', 'Unknown error')}
else:
return {'success': False, 'error': f'HTTP {response.status_code}'}
except Exception as e:
return {'success': False, 'error': str(e)}
def publish_to_instagram(content, instagram_config):
"""发布到 Instagram(使用 Instagram Basic Display API)"""
try:
access_token = instagram_config['access_token']
user_id = instagram_config['user_id']
# Instagram Basic Display API - 创建媒体容器
# 注意:Instagram API 需要图片,纯文本无法发布
if 'media_url' not in instagram_config:
return {'success': False, 'error': 'Instagram 需要图片才能发布内容'}
media_url = instagram_config['media_url']
# 第一步:创建媒体容器
container_url = f"https://graph.instagram.com/v18.0/{user_id}/media"
container_data = {
'image_url': media_url,
'caption': content,
'access_token': access_token
}
container_response = requests.post(container_url, data=container_data)
if container_response.status_code != 200:
return {'success': False, 'error': f'创建媒体容器失败: {container_response.text}'}
container_id = container_response.json().get('id')
# 第二步:发布媒体
publish_url = f"https://graph.instagram.com/v18.0/{user_id}/media_publish"
publish_data = {
'creation_id': container_id,
'access_token': access_token
}
publish_response = requests.post(publish_url, data=publish_data)
if publish_response.status_code == 200:
result = publish_response.json()
return {'success': True, 'post_id': result.get('id', '')}
else:
return {'success': False, 'error': f'发布失败: {publish_response.text}'}
except Exception as e:
return {'success': False, 'error': str(e)}
# 侧边栏 - 平台配置
with st.sidebar:
st.header("🔑 平台配置")
# Twitter/X 配置
st.subheader("🐦 Twitter/X")
if not TWITTER_AVAILABLE:
st.error("❌ 需要安装 tweepy 包")
st.info("在 requirements.txt 中添加: tweepy>=4.14.0")
else:
with st.expander("Twitter API 设置"):
# 使用缓存的值作为默认值
twitter_api_key = st.text_input(
"API Key",
type="password",
key="twitter_key",
value=get_cached_credential('twitter_api_key'),
help="🔒 安全存储在浏览器本地"
)
twitter_api_secret = st.text_input(
"API Secret",
type="password",
key="twitter_secret",
value=get_cached_credential('twitter_api_secret'),
help="🔒 安全存储在浏览器本地"
)
twitter_access_token = st.text_input(
"Access Token",
type="password",
key="twitter_token",
value=get_cached_credential('twitter_access_token'),
help="🔒 安全存储在浏览器本地"
)
twitter_access_secret = st.text_input(
"Access Token Secret",
type="password",
key="twitter_token_secret",
value=get_cached_credential('twitter_access_secret'),
help="🔒 安全存储在浏览器本地"
)
col_a, col_b = st.columns(2)
with col_a:
if st.button("连接 Twitter", key="connect_twitter"):
if all([twitter_api_key, twitter_api_secret, twitter_access_token, twitter_access_secret]):
try:
# 保存凭据
save_credential('twitter_api_key', twitter_api_key)
save_credential('twitter_api_secret', twitter_api_secret)
save_credential('twitter_access_token', twitter_access_token)
save_credential('twitter_access_secret', twitter_access_secret)
# 创建 Twitter API v2 客户端
client = tweepy.Client(
consumer_key=twitter_api_key,
consumer_secret=twitter_api_secret,
access_token=twitter_access_token,
access_token_secret=twitter_access_secret
)
# 测试连接
user = client.get_me()
st.session_state.authenticated_platforms['twitter'] = {
'client': client,
'consumer_key': twitter_api_key,
'consumer_secret': twitter_api_secret,
'access_token': twitter_access_token,
'access_token_secret': twitter_access_secret,
'user_id': user.data.id,
'username': user.data.username
}
st.success(f"✅ Twitter 连接成功!用户: @{user.data.username}")
st.info("🔒 API密钥已安全保存到浏览器缓存")
except Exception as e:
st.error(f"❌ Twitter 连接失败: {str(e)}")
else:
st.warning("请填写所有 Twitter API 凭据")
with col_b:
if st.button("🗑️ 清除缓存", key="clear_twitter_cache"):
save_credential('twitter_api_key', '')
save_credential('twitter_api_secret', '')
save_credential('twitter_access_token', '')
save_credential('twitter_access_secret', '')
st.success("Twitter 缓存已清除")
st.rerun()
# Telegram 配置
st.subheader("📨 Telegram")
with st.expander("Telegram Bot API 设置"):
telegram_bot_token = st.text_input(
"Bot Token",
type="password",
key="telegram_token",
value=get_cached_credential('telegram_bot_token'),
help="从 @BotFather 获取 | 🔒 安全存储在浏览器本地"
)
telegram_channel_id = st.text_input(
"频道 ID",
key="telegram_channel",
value=get_cached_credential('telegram_channel_id'),
placeholder="@your_channel 或 -100xxxxxxxxx",
help="频道用户名(@开头)或频道 ID | 🔒 安全存储在浏览器本地"
)
col_a, col_b = st.columns(2)
with col_a:
if st.button("连接 Telegram", key="connect_telegram"):
if telegram_bot_token and telegram_channel_id:
try:
# 保存凭据
save_credential('telegram_bot_token', telegram_bot_token)
save_credential('telegram_channel_id', telegram_channel_id)
# 验证 bot token
test_url = f"https://api.telegram.org/bot{telegram_bot_token}/getMe"
response = requests.get(test_url)
if response.status_code == 200:
bot_info = response.json()
if bot_info['ok']:
st.session_state.authenticated_platforms['telegram'] = {
'bot_token': telegram_bot_token,
'channel_id': telegram_channel_id
}
bot_name = bot_info['result']['first_name']
st.success(f"✅ Telegram 连接成功!Bot: {bot_name}")
st.info("🔒 API密钥已安全保存到浏览器缓存")
else:
st.error("❌ Bot Token 无效")
else:
st.error("❌ Telegram 连接失败")
except Exception as e:
st.error(f"❌ Telegram 连接失败: {str(e)}")
else:
st.warning("请填写 Bot Token 和频道 ID")
with col_b:
if st.button("🗑️ 清除缓存", key="clear_telegram_cache"):
save_credential('telegram_bot_token', '')
save_credential('telegram_channel_id', '')
st.success("Telegram 缓存已清除")
st.rerun()
# Instagram 配置
st.subheader("📸 Instagram")
with st.expander("Instagram API 设置"):
instagram_access_token = st.text_input(
"Access Token",
type="password",
key="instagram_token",
value=get_cached_credential('instagram_access_token'),
help="🔒 安全存储在浏览器本地"
)
instagram_user_id = st.text_input(
"Instagram User ID",
key="instagram_user_id",
value=get_cached_credential('instagram_user_id'),
help="🔒 安全存储在浏览器本地"
)
st.info("⚠️ Instagram 需要图片才能发布内容,纯文本无法发布")
col_a, col_b = st.columns(2)
with col_a:
if st.button("连接 Instagram", key="connect_instagram"):
if instagram_access_token and instagram_user_id:
try:
# 保存凭据
save_credential('instagram_access_token', instagram_access_token)
save_credential('instagram_user_id', instagram_user_id)
# 验证 Instagram token
test_url = f"https://graph.instagram.com/v18.0/{instagram_user_id}"
params = {'fields': 'id,username', 'access_token': instagram_access_token}
response = requests.get(test_url, params=params)
if response.status_code == 200:
user_info = response.json()
st.session_state.authenticated_platforms['instagram'] = {
'access_token': instagram_access_token,
'user_id': instagram_user_id
}
username = user_info.get('username', 'Unknown')
st.success(f"✅ Instagram 连接成功!用户: @{username}")
st.info("🔒 API密钥已安全保存到浏览器缓存")
else:
st.error(f"❌ Instagram 连接失败: {response.text}")
except Exception as e:
st.error(f"❌ Instagram 连接失败: {str(e)}")
else:
st.warning("请填写 Instagram 凭据")
with col_b:
if st.button("🗑️ 清除缓存", key="clear_instagram_cache"):
save_credential('instagram_access_token', '')
save_credential('instagram_user_id', '')
st.success("Instagram 缓存已清除")
st.rerun()
# 显示已连接平台
st.header("✅ 已连接平台")
for platform in st.session_state.authenticated_platforms:
st.success(f"✅ {platform.title()}")
# 主内容区域
if not st.session_state.authenticated_platforms:
st.warning("请在侧边栏配置并连接至少一个社交媒体平台")
# 显示API获取指南
with st.expander("📖 API 获取指南", expanded=True):
st.markdown("""
### 🐦 Twitter API
1. 访问 [developer.twitter.com](https://developer.twitter.com)
2. 申请开发者账户
3. 创建新应用
4. 生成 API Keys 和 Access Tokens
### 📨 Telegram Bot API
1. 在 Telegram 中找到 @BotFather
2. 发送 `/newbot` 创建新 bot
3. 获取 Bot Token
4. 将 bot 添加到您的频道并设为管理员
5. 频道 ID 格式:@channel_name 或 -100xxxxxxxxx
### 📸 Instagram API
1. 访问 [developers.facebook.com](https://developers.facebook.com)
2. 创建 Facebook 应用
3. 添加 Instagram Basic Display 产品
4. 获取用户访问令牌和用户 ID
5. ⚠️ 注意:Instagram 只能发布带图片的内容
""")
else:
# 发布功能
tab1, tab2, tab3 = st.tabs(["📝 发布内容", "📊 发布历史", "⚙️ 设置"])
with tab1:
st.header("📝 创建新帖子")
col1, col2 = st.columns([2, 1])
with col1:
# 内容输入
post_content = st.text_area(
"帖子内容",
placeholder="写下您想要分享的内容...",
height=200,
max_chars=2000
)
# 字符计数
char_count = len(post_content)
if char_count > 280:
st.warning(f"⚠️ 内容长度 {char_count} 字符,Twitter 限制 280 字符")
else:
st.info(f"📝 内容长度: {char_count} 字符")
# 图片上传(如果PIL可用)
uploaded_files = None
if PIL_AVAILABLE:
uploaded_files = st.file_uploader(
"上传图片",
accept_multiple_files=True,
type=['png', 'jpg', 'jpeg', 'gif']
)
# 预览上传的图片
if uploaded_files:
st.subheader("📷 图片预览")
cols = st.columns(min(len(uploaded_files), 3))
for i, uploaded_file in enumerate(uploaded_files):
with cols[i % 3]:
image = Image.open(uploaded_file)
# 修复:使用 use_container_width 替代 use_column_width
st.image(image, caption=uploaded_file.name, use_container_width=True)
else:
st.info("💡 安装 Pillow 包以支持图片上传功能")
# 链接添加
link_url = st.text_input("添加链接(可选)", placeholder="https://...")
with col2:
st.subheader("🎯 发布设置")
# 选择平台
selected_platforms = []
for platform in st.session_state.authenticated_platforms:
platform_name = {
'twitter': '🐦 Twitter',
'telegram': '📨 Telegram',
'instagram': '📸 Instagram'
}.get(platform, platform.title())
if st.checkbox(f"发布到 {platform_name}", value=True, key=f"select_{platform}"):
selected_platforms.append(platform)
# 发布模式
st.subheader("📤 发布模式")
publish_mode = st.radio(
"选择发布方式",
["立即发布", "预览模式"],
help="预览模式不会实际发布,只显示将要发布的内容"
)
# 平台特定设置
st.subheader("⚙️ 平台设置")
# Twitter 特定设置
if 'twitter' in selected_platforms:
st.write("**🐦 Twitter 设置**")
add_hashtags = st.checkbox("自动添加热门标签", key="twitter_hashtags")
if add_hashtags:
hashtags = st.text_input("标签(用空格分隔)", value="#社交媒体 #分享", key="twitter_hashtag_input")
else:
hashtags = ""
# Telegram 特定设置
if 'telegram' in selected_platforms:
st.write("**📨 Telegram 设置**")
telegram_format = st.selectbox("消息格式", ["普通文本", "HTML", "Markdown"], key="telegram_format")
disable_preview = st.checkbox("禁用链接预览", key="telegram_preview")
# Instagram 特定设置
if 'instagram' in selected_platforms:
st.write("**📸 Instagram 设置**")
st.warning("⚠️ Instagram 需要图片才能发布")
if uploaded_files:
st.success(f"✅ 已上传 {len(uploaded_files)} 张图片")
else:
st.error("❌ 请上传至少一张图片")
# 图片URL输入(用于Instagram API)
image_url_for_instagram = st.text_input(
"图片公开URL(Instagram API需要)",
placeholder="https://example.com/image.jpg",
help="Instagram API需要公开可访问的图片URL"
)
# 发布按钮
button_text = "👀 预览发布内容" if publish_mode == "预览模式" else "🚀 发布到选中平台"
button_type = "secondary" if publish_mode == "预览模式" else "primary"
if st.button(button_text, type=button_type, use_container_width=True):
if not post_content.strip():
st.error("请输入帖子内容")
elif not selected_platforms:
st.error("请至少选择一个发布平台")
else:
if publish_mode == "预览模式":
# 预览模式
st.header("👀 发布预览")
for platform in selected_platforms:
with st.expander(f"预览: {platform.title()}", expanded=True):
preview_content = post_content
# 添加平台特定内容
if platform == 'twitter' and add_hashtags and hashtags:
preview_content += f"\n\n{hashtags}"
if link_url:
preview_content += f"\n🔗 {link_url}"
st.write("**发布内容:**")
st.info(preview_content)
if uploaded_files:
st.write(f"**附件:** {len(uploaded_files)} 张图片")
# 显示图片预览
cols = st.columns(min(len(uploaded_files), 4))
for i, uploaded_file in enumerate(uploaded_files):
with cols[i % 4]:
image = Image.open(uploaded_file)
st.image(image, use_container_width=True)
else:
# 实际发布
publish_results = {}
# 发布到各个平台
for platform in selected_platforms:
with st.spinner(f"正在发布到 {platform.title()}..."):
try:
final_content = post_content
# 添加平台特定内容
if platform == 'twitter' and add_hashtags and hashtags:
final_content += f"\n\n{hashtags}"
if link_url:
final_content += f"\n{link_url}"
if platform == 'twitter':
result = publish_to_twitter(
final_content,
st.session_state.authenticated_platforms['twitter'],
uploaded_files
)
elif platform == 'telegram':
# 为Telegram准备特殊格式
telegram_content = final_content
if 'telegram_format' in locals():
if telegram_format == "HTML":
telegram_content = final_content.replace('\n', '<br>')
elif telegram_format == "Markdown":
telegram_content = final_content
result = publish_to_telegram(
telegram_content,
st.session_state.authenticated_platforms['telegram'],
uploaded_files
)
elif platform == 'instagram':
# Instagram需要图片URL
instagram_config = st.session_state.authenticated_platforms['instagram'].copy()
if 'image_url_for_instagram' in locals() and image_url_for_instagram:
instagram_config['media_url'] = image_url_for_instagram
result = publish_to_instagram(final_content, instagram_config)
else:
result = {'success': False, 'error': '需要提供图片URL'}
else:
result = {'success': False, 'error': 'Unsupported platform'}
publish_results[platform] = result
except Exception as e:
publish_results[platform] = {'success': False, 'error': str(e)}
# 显示发布结果
st.header("📊 发布结果")
success_count = 0
for platform, result in publish_results.items():
platform_icon = {'twitter': '🐦', 'telegram': '📨', 'instagram': '📸'}.get(platform, '📱')
if result['success']:
success_msg = f"✅ {platform_icon} {platform.title()}: 发布成功!"
if 'media_count' in result and result['media_count'] > 0:
success_msg += f" (包含 {result['media_count']} 张图片)"
st.success(success_msg)
if 'post_id' in result:
st.code(f"帖子 ID: {result['post_id']}")
success_count += 1
else:
st.error(f"❌ {platform_icon} {platform.title()}: {result['error']}")
# 记录到历史
if success_count > 0:
history_record = {
'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'content': post_content[:50] + "..." if len(post_content) > 50 else post_content,
'platforms': [p for p, r in publish_results.items() if r['success']],
'status': f"{success_count}/{len(selected_platforms)} 成功",
'media_count': len(uploaded_files) if uploaded_files else 0
}
st.session_state.publish_history.append(history_record)
# 成功提示
if success_count == len(selected_platforms):
st.balloons()
st.success(f"🎉 所有平台发布成功!({success_count}/{len(selected_platforms)})")
elif success_count > 0:
st.warning(f"⚠️ 部分平台发布成功 ({success_count}/{len(selected_platforms)})")
with tab2:
st.header("📊 发布历史")
if st.session_state.publish_history:
st.info(f"共 {len(st.session_state.publish_history)} 条发布记录")
for i, record in enumerate(reversed(st.session_state.publish_history)):
with st.expander(f"#{len(st.session_state.publish_history)-i} - {record['timestamp']} - {record['status']}"):
col1, col2 = st.columns([2, 1])
with col1:
st.write(f"**内容**: {record['content']}")
st.write(f"**平台**: {', '.join([p.title() for p in record['platforms']])}")
if record.get('media_count', 0) > 0:
st.write(f"**图片**: {record['media_count']} 张")
with col2:
st.write(f"**时间**: {record['timestamp']}")
st.write(f"**状态**: {record['status']}")
else:
st.info("暂无发布历史")
st.markdown("发布第一条内容来开始记录历史!")
with tab3:
st.header("⚙️ 应用设置")
col1, col2 = st.columns(2)
with col1:
st.subheader("🔌 平台连接管理")
for platform in list(st.session_state.authenticated_platforms.keys()):
platform_icon = {'twitter': '🐦', 'telegram': '📨', 'instagram': '📸'}.get(platform, '📱')
col_a, col_b = st.columns([3, 1])
with col_a:
st.write(f"{platform_icon} {platform.title()} - 已连接")
with col_b:
if st.button(f"断开", key=f"disconnect_{platform}"):
del st.session_state.authenticated_platforms[platform]
st.rerun()
with col2:
st.subheader("📊 数据管理")
if st.button("🗑️ 清空发布历史"):
st.session_state.publish_history = []
st.success("发布历史已清空")
if st.button("🗑️ 清除所有API缓存", type="secondary"):
# 清除所有API凭据缓存
for key in st.session_state.api_credentials:
st.session_state.api_credentials[key] = ''
st.success("所有API缓存已清除")
st.info("下次刷新页面时输入框将为空")
if st.button("🔄 重置所有连接", type="secondary"):
st.session_state.authenticated_platforms = {}
st.session_state.publish_history = []
# 也清除API缓存
for key in st.session_state.api_credentials:
st.session_state.api_credentials[key] = ''
st.success("所有设置和缓存已重置")
st.rerun()
st.subheader("ℹ️ 应用信息")
st.info(f"""
**版本**: 1.1.0 (支持API缓存)
**已连接平台**: {len(st.session_state.authenticated_platforms)}
**发布记录**: {len(st.session_state.publish_history)} 条
**依赖状态**: {"✅ 完整" if all(dependencies_status.values()) else "⚠️ 部分缺失"}
**缓存状态**: {"✅ 已启用" if any(st.session_state.api_credentials.values()) else "❌ 无缓存"}
""")
# 新增:修复说明
with st.expander("🔧 最新功能更新", expanded=False):
st.markdown("""
### 🆕 v1.1.0 新功能:
1. **🔒 API密钥缓存**: API密钥自动保存到浏览器本地,刷新页面不丢失
2. **🗑️ 单独清除缓存**: 每个平台都可以单独清除API缓存
3. **🔧 缓存管理**: 在设置页面可以清除所有API缓存
4. **🔐 安全存储**: 使用浏览器localStorage安全存储敏感信息
### ✅ 已修复问题:
1. **图片上传到 Twitter**: 现在支持同时上传文字和图片到 Twitter (最多4张)
2. **图片上传到 Telegram**: 支持单张或多张图片发布 (最多10张)
3. **弃用参数修复**: 将 `use_column_width` 更新为 `use_container_width`
4. **发布历史增强**: 现在会记录包含的图片数量
5. **错误处理改进**: 更详细的错误信息和状态反馈
### 🔒 安全说明:
- API密钥存储在您的浏览器本地,不会发送到任何服务器
- 可以随时清除缓存的API密钥
- 隐私模式/无痕浏览将不会保存缓存
### 📋 使用说明:
- **Twitter**: 支持文字+图片,自动处理媒体上传
- **Telegram**: 单图用 sendPhoto,多图用 sendMediaGroup
- **Instagram**: 仍需要提供公开图片URL (API限制)
- **API缓存**: 输入API后点击连接,会自动保存到浏览器缓存
""")
# 缓存状态显示
with st.expander("🔍 当前缓存状态", expanded=False):
st.write("**已缓存的API凭据:**")
cache_status = {}
for key, value in st.session_state.api_credentials.items():
platform = key.split('_')[0] # 获取平台名
if platform not in cache_status:
cache_status[platform] = []
if value: # 如果有值
masked_value = f"{value[:4]}...{value[-4:]}" if len(value) > 8 else "****"
cache_status[platform].append(f"✅ {key.split('_', 1)[1]}: {masked_value}")
else:
cache_status[platform].append(f"❌ {key.split('_', 1)[1]}: 未缓存")
for platform, status_list in cache_status.items():
st.write(f"**{platform.title()}:**")
for status in status_list:
st.write(f" {status}")
st.write("")
# 底部信息
st.markdown("---")
st.markdown(
"""
<div style='text-align: center; color: gray;'>
📱 多平台社交媒体发布工具 v1.1.0 | Made with Streamlit<br>
🔒 所有数据和API密钥仅在您的浏览器中存储,确保隐私安全<br>
✅ 已支持API缓存功能,刷新页面不丢失设置
</div>
""",
unsafe_allow_html=True
)