export_titles.py
2.91 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
import requests
import csv
import time
from urllib.parse import urlencode
# ========== 配置区域 ==========
# 从你提供的请求中提取的 Bearer Token
BEARER_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiZTI1ZDFlZDEtMTQyNy00MTk4LTlmZjMtYTg2MDY3OTc2OWNkIiwiZXhwIjoxNzgwNDgwNTI4LCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.yQMDS6fMPnSjmm1qVbqkDdfSitzwPhslomIri0na4-M"
# API 基础 URL(去掉查询参数)
BASE_URL = "https://aiassistant.glsx.com.cn/console/api/apps/c080837e-cda0-4428-a2f0-9513845d3da6/chat-conversations"
# 可选:如果你的网络需要代理,取消注释并填写代理地址
# PROXY = {
# "http": "http://127.0.0.1:7890",
# "https": "http://127.0.0.1:7890"
# }
# 请求头(只需要 Authorization + 基础 User-Agent)
HEADERS = {
"Authorization": f"Bearer {BEARER_TOKEN}",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Content-Type": "application/json"
}
# 查询参数(可根据需要修改,例如调整时间范围)
PARAMS = {
"limit": 50, # 每页条数,最大100
"start": "2025-06-03 00:00",
"end": "2026-06-03 23:59",
"sort_by": "-created_at",
"annotation_status": "all"
}
# ============================
def get_conversations(page=1):
"""获取单页会话数据"""
params = PARAMS.copy()
params["page"] = page
url = f"{BASE_URL}?{urlencode(params)}"
try:
# 如果设置了代理,加上 proxies=PROXY
resp = requests.get(url, headers=HEADERS, timeout=30) # , proxies=PROXY
if resp.status_code != 200:
print(f"请求失败: {resp.status_code}, {resp.text[:200]}")
return None
return resp.json()
except Exception as e:
print(f"请求异常: {e}")
return None
def main():
all_titles = []
page = 1
while True:
print(f"正在获取第 {page} 页...")
data = get_conversations(page)
if not data:
break
conversations = data.get("data", [])
if not conversations:
print("没有更多数据,结束")
break
for conv in conversations:
title = conv.get("name", "").strip()
if title:
all_titles.append(title)
# 检查是否还有下一页
if not data.get("has_more", False):
break
page += 1
time.sleep(0.5) # 避免请求过快
# 去重并保存
unique_titles = list(dict.fromkeys(all_titles))
print(f"共获取 {len(unique_titles)} 条标题")
# 保存为 CSV 文件
output_file = "dify_titles.csv"
with open(output_file, "w", newline="", encoding="utf-8-sig") as f:
writer = csv.writer(f)
writer.writerow(["标题"])
for title in unique_titles:
writer.writerow([title])
print(f"已保存到 {output_file}")
if __name__ == "__main__":
main()