-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathsau_backend.py
More file actions
717 lines (618 loc) · 23.4 KB
/
sau_backend.py
File metadata and controls
717 lines (618 loc) · 23.4 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
import asyncio
import os
import sqlite3
import threading
import time
import uuid
from pathlib import Path
from queue import Queue
from flask_cors import CORS
from myUtils.auth import check_cookie
from flask import Flask, request, jsonify, Response, render_template, send_from_directory
from conf import BASE_DIR
from myUtils.login import get_tencent_cookie, douyin_cookie_gen, get_ks_cookie, xiaohongshu_cookie_gen
from myUtils.postVideo import post_video_tencent, post_video_DouYin, post_video_ks, post_video_xhs
active_queues = {}
app = Flask(__name__)
#允许所有来源跨域访问
CORS(app)
# 限制上传文件大小为160MB
app.config['MAX_CONTENT_LENGTH'] = 160 * 1024 * 1024
# 获取当前目录(假设 index.html 和 assets 在这里)
current_dir = os.path.dirname(os.path.abspath(__file__))
# 处理所有静态资源请求(未来打包用)
@app.route('/assets/<filename>')
def custom_static(filename):
return send_from_directory(os.path.join(current_dir, 'assets'), filename)
# 处理 favicon.ico 静态资源(未来打包用)
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(current_dir, 'assets'), 'vite.svg')
@app.route('/vite.svg')
def vite_svg():
return send_from_directory(os.path.join(current_dir, 'assets'), 'vite.svg')
# (未来打包用)
@app.route('/')
def index(): # put application's code here
return send_from_directory(current_dir, 'index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({
"code": 400,
"data": None,
"msg": "No file part in the request"
}), 400
file = request.files['file']
if file.filename == '':
return jsonify({
"code": 400,
"data": None,
"msg": "No selected file"
}), 400
try:
# 保存文件到指定位置
uuid_v1 = uuid.uuid1()
print(f"UUID v1: {uuid_v1}")
filepath = Path(BASE_DIR / "videoFile" / f"{uuid_v1}_{file.filename}")
file.save(filepath)
return jsonify({"code":200,"msg": "File uploaded successfully", "data": f"{uuid_v1}_{file.filename}"}), 200
except Exception as e:
return jsonify({"code":500,"msg": str(e),"data":None}), 500
@app.route('/getFile', methods=['GET'])
def get_file():
# 获取 filename 参数
filename = request.args.get('filename')
if not filename:
return jsonify({"code": 400, "msg": "filename is required", "data": None}), 400
# 防止路径穿越攻击
if '..' in filename or filename.startswith('/'):
return jsonify({"code": 400, "msg": "Invalid filename", "data": None}), 400
# 拼接完整路径
file_path = str(Path(BASE_DIR / "videoFile"))
# 返回文件
return send_from_directory(file_path,filename)
@app.route('/uploadSave', methods=['POST'])
def upload_save():
if 'file' not in request.files:
return jsonify({
"code": 400,
"data": None,
"msg": "No file part in the request"
}), 400
file = request.files['file']
if file.filename == '':
return jsonify({
"code": 400,
"data": None,
"msg": "No selected file"
}), 400
# 获取表单中的自定义文件名(可选)
custom_filename = request.form.get('filename', None)
if custom_filename:
filename = custom_filename + "." + file.filename.split('.')[-1]
else:
filename = file.filename
try:
# 生成 UUID v1
uuid_v1 = uuid.uuid1()
print(f"UUID v1: {uuid_v1}")
# 构造文件名和路径
final_filename = f"{uuid_v1}_{filename}"
filepath = Path(BASE_DIR / "videoFile" / f"{uuid_v1}_{filename}")
# 保存文件
file.save(filepath)
with sqlite3.connect(Path(BASE_DIR / "db" / "database.db")) as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO file_records (filename, filesize, file_path)
VALUES (?, ?, ?)
''', (filename, round(float(os.path.getsize(filepath)) / (1024 * 1024),2), final_filename))
conn.commit()
print("✅ 上传文件已记录")
return jsonify({
"code": 200,
"msg": "File uploaded and saved successfully",
"data": {
"filename": filename,
"filepath": final_filename
}
}), 200
except Exception as e:
print(f"Upload failed: {e}")
return jsonify({
"code": 500,
"msg": f"upload failed: {e}",
"data": None
}), 500
@app.route('/getFiles', methods=['GET'])
def get_all_files():
try:
# 使用 with 自动管理数据库连接
with sqlite3.connect(Path(BASE_DIR / "db" / "database.db")) as conn:
conn.row_factory = sqlite3.Row # 允许通过列名访问结果
cursor = conn.cursor()
# 查询所有记录
cursor.execute("SELECT * FROM file_records")
rows = cursor.fetchall()
# 将结果转为字典列表,并提取UUID
data = []
for row in rows:
row_dict = dict(row)
# 从 file_path 中提取 UUID (文件名的第一部分,下划线前)
if row_dict.get('file_path'):
file_path_parts = row_dict['file_path'].split('_', 1) # 只分割第一个下划线
if len(file_path_parts) > 0:
row_dict['uuid'] = file_path_parts[0] # UUID 部分
else:
row_dict['uuid'] = ''
else:
row_dict['uuid'] = ''
data.append(row_dict)
return jsonify({
"code": 200,
"msg": "success",
"data": data
}), 200
except Exception as e:
return jsonify({
"code": 500,
"msg": str("get file failed!"),
"data": None
}), 500
@app.route("/getAccounts", methods=['GET'])
def getAccounts():
"""快速获取所有账号信息,不进行cookie验证"""
try:
with sqlite3.connect(Path(BASE_DIR / "db" / "database.db")) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM user_info''')
rows = cursor.fetchall()
rows_list = [list(row) for row in rows]
print("\n📋 当前数据表内容(快速获取):")
for row in rows:
print(row)
return jsonify(
{
"code": 200,
"msg": None,
"data": rows_list
}), 200
except Exception as e:
print(f"获取账号列表时出错: {str(e)}")
return jsonify({
"code": 500,
"msg": f"获取账号列表失败: {str(e)}",
"data": None
}), 500
@app.route("/getValidAccounts",methods=['GET'])
async def getValidAccounts():
with sqlite3.connect(Path(BASE_DIR / "db" / "database.db")) as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM user_info''')
rows = cursor.fetchall()
rows_list = [list(row) for row in rows]
print("\n📋 当前数据表内容:")
for row in rows:
print(row)
for row in rows_list:
flag = await check_cookie(row[1],row[2])
if not flag:
row[4] = 0
cursor.execute('''
UPDATE user_info
SET status = ?
WHERE id = ?
''', (0,row[0]))
conn.commit()
print("✅ 用户状态已更新")
for row in rows:
print(row)
return jsonify(
{
"code": 200,
"msg": None,
"data": rows_list
}),200
@app.route('/deleteFile', methods=['GET'])
def delete_file():
file_id = request.args.get('id')
if not file_id or not file_id.isdigit():
return jsonify({
"code": 400,
"msg": "Invalid or missing file ID",
"data": None
}), 400
try:
# 获取数据库连接
with sqlite3.connect(Path(BASE_DIR / "db" / "database.db")) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
# 查询要删除的记录
cursor.execute("SELECT * FROM file_records WHERE id = ?", (file_id,))
record = cursor.fetchone()
if not record:
return jsonify({
"code": 404,
"msg": "File not found",
"data": None
}), 404
record = dict(record)
# 获取文件路径并删除实际文件
file_path = Path(BASE_DIR / "videoFile" / record['file_path'])
if file_path.exists():
try:
file_path.unlink() # 删除文件
print(f"✅ 实际文件已删除: {file_path}")
except Exception as e:
print(f"⚠️ 删除实际文件失败: {e}")
# 即使删除文件失败,也要继续删除数据库记录,避免数据不一致
else:
print(f"⚠️ 实际文件不存在: {file_path}")
# 删除数据库记录
cursor.execute("DELETE FROM file_records WHERE id = ?", (file_id,))
conn.commit()
return jsonify({
"code": 200,
"msg": "File deleted successfully",
"data": {
"id": record['id'],
"filename": record['filename']
}
}), 200
except Exception as e:
return jsonify({
"code": 500,
"msg": str("delete failed!"),
"data": None
}), 500
@app.route('/deleteAccount', methods=['GET'])
def delete_account():
account_id = request.args.get('id')
if not account_id or not account_id.isdigit():
return jsonify({
"code": 400,
"msg": "Invalid or missing account ID",
"data": None
}), 400
account_id = int(account_id)
try:
# 获取数据库连接
with sqlite3.connect(Path(BASE_DIR / "db" / "database.db")) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
# 查询要删除的记录
cursor.execute("SELECT * FROM user_info WHERE id = ?", (account_id,))
record = cursor.fetchone()
if not record:
return jsonify({
"code": 404,
"msg": "account not found",
"data": None
}), 404
record = dict(record)
# 删除关联的cookie文件
if record.get('filePath'):
cookie_file_path = Path(BASE_DIR / "cookiesFile" / record['filePath'])
if cookie_file_path.exists():
try:
cookie_file_path.unlink()
print(f"✅ Cookie文件已删除: {cookie_file_path}")
except Exception as e:
print(f"⚠️ 删除Cookie文件失败: {e}")
# 删除数据库记录
cursor.execute("DELETE FROM user_info WHERE id = ?", (account_id,))
conn.commit()
return jsonify({
"code": 200,
"msg": "account deleted successfully",
"data": None
}), 200
except Exception as e:
return jsonify({
"code": 500,
"msg": f"delete failed: {str(e)}",
"data": None
}), 500
# SSE 登录接口
@app.route('/login')
def login():
# 1 小红书 2 视频号 3 抖音 4 快手
type = request.args.get('type')
# 账号名
id = request.args.get('id')
# 模拟一个用于异步通信的队列
status_queue = Queue()
active_queues[id] = status_queue
def on_close():
print(f"清理队列: {id}")
del active_queues[id]
# 启动异步任务线程
thread = threading.Thread(target=run_async_function, args=(type,id,status_queue), daemon=True)
thread.start()
response = Response(sse_stream(status_queue,), mimetype='text/event-stream')
response.headers['Cache-Control'] = 'no-cache'
response.headers['X-Accel-Buffering'] = 'no' # 关键:禁用 Nginx 缓冲
response.headers['Content-Type'] = 'text/event-stream'
response.headers['Connection'] = 'keep-alive'
return response
@app.route('/postVideo', methods=['POST'])
def postVideo():
# 获取JSON数据
data = request.get_json()
if not data:
return jsonify({"code": 400, "msg": "请求数据不能为空", "data": None}), 400
# 从JSON数据中提取fileList和accountList
file_list = data.get('fileList', [])
account_list = data.get('accountList', [])
type = data.get('type')
title = data.get('title')
tags = data.get('tags')
category = data.get('category')
enableTimer = data.get('enableTimer')
if category == 0:
category = None
productLink = data.get('productLink', '')
productTitle = data.get('productTitle', '')
thumbnail_path = data.get('thumbnail', '')
is_draft = data.get('isDraft', False) # 新增参数:是否保存为草稿
videos_per_day = data.get('videosPerDay')
daily_times = data.get('dailyTimes')
start_days = data.get('startDays')
# 参数校验
if not file_list:
return jsonify({"code": 400, "msg": "文件列表不能为空", "data": None}), 400
if not account_list:
return jsonify({"code": 400, "msg": "账号列表不能为空", "data": None}), 400
if not type:
return jsonify({"code": 400, "msg": "平台类型不能为空", "data": None}), 400
if not title:
return jsonify({"code": 400, "msg": "标题不能为空", "data": None}), 400
# 打印获取到的数据(仅作为示例)
print("File List:", file_list)
print("Account List:", account_list)
try:
match type:
case 1:
post_video_xhs(title, file_list, tags, account_list, category, enableTimer, videos_per_day, daily_times,
start_days)
case 2:
post_video_tencent(title, file_list, tags, account_list, category, enableTimer, videos_per_day, daily_times,
start_days, is_draft)
case 3:
post_video_DouYin(title, file_list, tags, account_list, category, enableTimer, videos_per_day, daily_times,
start_days, thumbnail_path, productLink, productTitle)
case 4:
post_video_ks(title, file_list, tags, account_list, category, enableTimer, videos_per_day, daily_times,
start_days)
case _:
return jsonify({"code": 400, "msg": f"不支持的平台类型: {type}", "data": None}), 400
# 返回响应给客户端
return jsonify(
{
"code": 200,
"msg": "发布任务已提交",
"data": None
}), 200
except Exception as e:
print(f"发布视频时出错: {str(e)}")
return jsonify({
"code": 500,
"msg": f"发布失败: {str(e)}",
"data": None
}), 500
@app.route('/updateUserinfo', methods=['POST'])
def updateUserinfo():
# 获取JSON数据
data = request.get_json()
# 从JSON数据中提取 type 和 userName
user_id = data.get('id')
type = data.get('type')
userName = data.get('userName')
try:
# 获取数据库连接
with sqlite3.connect(Path(BASE_DIR / "db" / "database.db")) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
# 更新数据库记录
cursor.execute('''
UPDATE user_info
SET type = ?,
userName = ?
WHERE id = ?;
''', (type, userName, user_id))
conn.commit()
return jsonify({
"code": 200,
"msg": "account update successfully",
"data": None
}), 200
except Exception as e:
return jsonify({
"code": 500,
"msg": str("update failed!"),
"data": None
}), 500
@app.route('/postVideoBatch', methods=['POST'])
def postVideoBatch():
data_list = request.get_json()
if not isinstance(data_list, list):
return jsonify({"code": 400, "msg": "Expected a JSON array", "data": None}), 400
for data in data_list:
# 从JSON数据中提取fileList和accountList
file_list = data.get('fileList', [])
account_list = data.get('accountList', [])
type = data.get('type')
title = data.get('title')
tags = data.get('tags')
category = data.get('category')
enableTimer = data.get('enableTimer')
if category == 0:
category = None
productLink = data.get('productLink', '')
productTitle = data.get('productTitle', '')
is_draft = data.get('isDraft', False)
videos_per_day = data.get('videosPerDay')
daily_times = data.get('dailyTimes')
start_days = data.get('startDays')
# 打印获取到的数据(仅作为示例)
print("File List:", file_list)
print("Account List:", account_list)
match type:
case 1:
post_video_xhs(title, file_list, tags, account_list, category, enableTimer, videos_per_day, daily_times,
start_days)
case 2:
post_video_tencent(title, file_list, tags, account_list, category, enableTimer, videos_per_day, daily_times,
start_days, is_draft)
case 3:
post_video_DouYin(title, file_list, tags, account_list, category, enableTimer, videos_per_day, daily_times,
start_days, productLink, productTitle)
case 4:
post_video_ks(title, file_list, tags, account_list, category, enableTimer, videos_per_day, daily_times,
start_days)
# 返回响应给客户端
return jsonify(
{
"code": 200,
"msg": None,
"data": None
}), 200
# Cookie文件上传API
@app.route('/uploadCookie', methods=['POST'])
def upload_cookie():
try:
if 'file' not in request.files:
return jsonify({
"code": 400,
"msg": "没有找到Cookie文件",
"data": None
}), 400
file = request.files['file']
if file.filename == '':
return jsonify({
"code": 400,
"msg": "Cookie文件名不能为空",
"data": None
}), 400
if not file.filename.endswith('.json'):
return jsonify({
"code": 400,
"msg": "Cookie文件必须是JSON格式",
"data": None
}), 400
# 获取账号信息
account_id = request.form.get('id')
platform = request.form.get('platform')
if not account_id or not platform:
return jsonify({
"code": 400,
"msg": "缺少账号ID或平台信息",
"data": None
}), 400
# 从数据库获取账号的文件路径
with sqlite3.connect(Path(BASE_DIR / "db" / "database.db")) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('SELECT filePath FROM user_info WHERE id = ?', (account_id,))
result = cursor.fetchone()
if not result:
return jsonify({
"code": 500,
"msg": "账号不存在",
"data": None
}), 404
# 保存上传的Cookie文件到对应路径
cookie_file_path = Path(BASE_DIR / "cookiesFile" / result['filePath'])
cookie_file_path.parent.mkdir(parents=True, exist_ok=True)
file.save(str(cookie_file_path))
# 更新数据库中的账号信息(可选,比如更新更新时间)
# 这里可以根据需要添加额外的处理逻辑
return jsonify({
"code": 200,
"msg": "Cookie文件上传成功",
"data": None
}), 200
except Exception as e:
print(f"上传Cookie文件时出错: {str(e)}")
return jsonify({
"code": 500,
"msg": f"上传Cookie文件失败: {str(e)}",
"data": None
}), 500
# Cookie文件下载API
@app.route('/downloadCookie', methods=['GET'])
def download_cookie():
try:
file_path = request.args.get('filePath')
if not file_path:
return jsonify({
"code": 500,
"msg": "缺少文件路径参数",
"data": None
}), 400
# 验证文件路径的安全性,防止路径遍历攻击
cookie_file_path = Path(BASE_DIR / "cookiesFile" / file_path).resolve()
base_path = Path(BASE_DIR / "cookiesFile").resolve()
if not cookie_file_path.is_relative_to(base_path):
return jsonify({
"code": 500,
"msg": "非法文件路径",
"data": None
}), 400
if not cookie_file_path.exists():
return jsonify({
"code": 500,
"msg": "Cookie文件不存在",
"data": None
}), 404
# 返回文件
return send_from_directory(
directory=str(cookie_file_path.parent),
path=cookie_file_path.name,
as_attachment=True
)
except Exception as e:
print(f"下载Cookie文件时出错: {str(e)}")
return jsonify({
"code": 500,
"msg": f"下载Cookie文件失败: {str(e)}",
"data": None
}), 500
# 包装函数:在线程中运行异步函数
def run_async_function(type,id,status_queue):
match type:
case '1':
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(xiaohongshu_cookie_gen(id, status_queue))
loop.close()
case '2':
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(get_tencent_cookie(id,status_queue))
loop.close()
case '3':
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(douyin_cookie_gen(id,status_queue))
loop.close()
case '4':
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(get_ks_cookie(id,status_queue))
loop.close()
# SSE 流生成器函数
def sse_stream(status_queue):
while True:
if not status_queue.empty():
msg = status_queue.get()
yield f"data: {msg}\n\n"
else:
# 避免 CPU 占满
time.sleep(0.1)
if __name__ == '__main__':
app.run(host='0.0.0.0' ,port=5409)