-
Notifications
You must be signed in to change notification settings - Fork 3
/
send_weibo
46 lines (38 loc) · 1.53 KB
/
send_weibo
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
import os
from urllib.parse import urlparse, parse_qs
import requests
def send_weibo_post():
url = "https://api.weibo.cn/2/statuses/send"
headers = {
"User-Agent": "Weibo/88047 (iPhone; iOS 17.3.1; Scale/3.00)",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
}
# 从环境变量中获取 status_taobudiao
status_taobudiao = os.getenv('status_taobudiao')
if not status_taobudiao:
print("环境变量 'status_taobudiao' 未设置")
return
# 解析 URL 并提取参数
parsed_url = urlparse(status_taobudiao)
params = parse_qs(parsed_url.query)
params_dict = {k: v[0] for k, v in params.items()}
# 调试信息
print("Parsed URL:", parsed_url)
print("Parsed Params:", params_dict)
# 从环境变量中获取微博内容
weibo_content = os.getenv('WEIBO_CONTENT', '测试微博内容')
data = {
"act": "add",
"content": weibo_content,
}
response = requests.post(url, headers=headers, data=data, params=params_dict)
if response.status_code == 200:
response_json = response.json()
username = response_json.get("user", {}).get("screen_name", "Unknown")
if username != "Unknown":
print(f"用户名:{username} 发送微博'{weibo_content}'成功")
else:
print(f"发送微博'{weibo_content}'失败: 无法获取用户名")
else:
print(f"发送微博'{weibo_content}'失败:", response.status_code, response.text)
send_weibo_post()