ZHIWEI666 commited on
Commit
d1e778f
·
verified ·
1 Parent(s): 61e67a1

Upload api_playlist.py

Browse files
Files changed (1) hide show
  1. api_playlist.py +51 -14
api_playlist.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import uuid
 
3
  from fastapi import APIRouter, UploadFile, File, Form
4
  from pydantic import BaseModel
5
  from huggingface_hub import upload_file
@@ -7,16 +8,11 @@ from core import get_db, save_db, REPO_ID, TOKEN
7
 
8
  router = APIRouter()
9
 
10
- class PlaylistAddReq(BaseModel):
11
- username: str
12
- type: str
13
- playlist_name: str
14
- filename: str
15
-
16
- class PlaylistDeleteReq(BaseModel):
17
- username: str
18
- type: str
19
- name: str
20
 
21
  @router.get("/api/cloud_pl/get")
22
  def get_cloud_playlists():
@@ -43,7 +39,8 @@ async def create_cloud_playlist(username: str = Form(...), type: str = Form(...)
43
  upload_file(path_or_fileobj=temp_path, path_in_repo=f"community_images/{cover_name}", repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
44
  os.remove(temp_path)
45
 
46
- target_dict[name] = {"description": description[:200], "cover": cover_name, "songs": []}
 
47
  save_db(db, "cloud_playlists.json")
48
  return {"status": "success"}
49
  except Exception as e: return {"status": "error", "message": str(e)}
@@ -58,8 +55,9 @@ async def edit_cloud_playlist(username: str = Form(...), type: str = Form(...),
58
  if old_name != name and name in target_dict: return {"status": "error", "message": "新名称已存在"}
59
 
60
  pl_data = target_dict.pop(old_name)
61
- # 兼容旧版本纯数组数据
62
  if isinstance(pl_data, list): pl_data = {"description": "", "cover": "", "songs": pl_data}
 
 
63
 
64
  pl_data["description"] = description[:200]
65
  if image:
@@ -96,13 +94,52 @@ def add_cloud_playlist(req: PlaylistAddReq):
96
  target_dict = db.get("admin", {}) if req.type == "admin" else db.get("users", {}).get(req.username, {})
97
  pl = target_dict.get(req.playlist_name)
98
  if pl is None: return {"status": "error", "message": "歌单不存在"}
99
-
100
- # 兼容旧版本列表
101
  if isinstance(pl, list):
102
  if req.filename not in pl: pl.append(req.filename)
103
  else:
104
  if req.filename not in pl["songs"]: pl["songs"].append(req.filename)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  save_db(db, "cloud_playlists.json")
107
  return {"status": "success"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  except Exception as e: return {"status": "error", "message": str(e)}
 
1
  import os
2
  import uuid
3
+ import time
4
  from fastapi import APIRouter, UploadFile, File, Form
5
  from pydantic import BaseModel
6
  from huggingface_hub import upload_file
 
8
 
9
  router = APIRouter()
10
 
11
+ class PlaylistAddReq(BaseModel): username: str; type: str; playlist_name: str; filename: str
12
+ class PlaylistDeleteReq(BaseModel): username: str; type: str; name: str
13
+ # 🌟 新增请求模型
14
+ class PlaylistLikeReq(BaseModel): username: str; type: str; author: str; name: str
15
+ class PlaylistCommentReq(BaseModel): username: str; type: str; author: str; name: str; content: str
 
 
 
 
 
16
 
17
  @router.get("/api/cloud_pl/get")
18
  def get_cloud_playlists():
 
39
  upload_file(path_or_fileobj=temp_path, path_in_repo=f"community_images/{cover_name}", repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
40
  os.remove(temp_path)
41
 
42
+ # 🌟 初始化点赞与评论空数据
43
+ target_dict[name] = {"description": description[:200], "cover": cover_name, "songs": [], "likes": 0, "liked_by": [], "comments": []}
44
  save_db(db, "cloud_playlists.json")
45
  return {"status": "success"}
46
  except Exception as e: return {"status": "error", "message": str(e)}
 
55
  if old_name != name and name in target_dict: return {"status": "error", "message": "新名称已存在"}
56
 
57
  pl_data = target_dict.pop(old_name)
 
58
  if isinstance(pl_data, list): pl_data = {"description": "", "cover": "", "songs": pl_data}
59
+ # 兼容补充互动字段
60
+ if "likes" not in pl_data: pl_data.update({"likes": 0, "liked_by": [], "comments": []})
61
 
62
  pl_data["description"] = description[:200]
63
  if image:
 
94
  target_dict = db.get("admin", {}) if req.type == "admin" else db.get("users", {}).get(req.username, {})
95
  pl = target_dict.get(req.playlist_name)
96
  if pl is None: return {"status": "error", "message": "歌单不存在"}
 
 
97
  if isinstance(pl, list):
98
  if req.filename not in pl: pl.append(req.filename)
99
  else:
100
  if req.filename not in pl["songs"]: pl["songs"].append(req.filename)
101
+ save_db(db, "cloud_playlists.json")
102
+ return {"status": "success"}
103
+ except Exception as e: return {"status": "error", "message": str(e)}
104
+
105
+ # 🌟 新增:歌单点赞
106
+ @router.post("/api/cloud_pl/like")
107
+ def like_cloud_playlist(req: PlaylistLikeReq):
108
+ try:
109
+ db = get_db("cloud_playlists.json")
110
+ target_dict = db.get("admin", {}) if req.type == "admin" else db.get("users", {}).get(req.author, {})
111
+ pl = target_dict.get(req.name)
112
+ if not pl or isinstance(pl, list): return {"status": "error", "message": "格式异常或不存在"}
113
+ if "liked_by" not in pl: pl["liked_by"] = []; pl["likes"] = 0
114
+
115
+ if req.username in pl["liked_by"]:
116
+ pl["liked_by"].remove(req.username)
117
+ pl["likes"] = max(0, pl.get("likes", 1) - 1)
118
+ else:
119
+ pl["liked_by"].append(req.username)
120
+ pl["likes"] = pl.get("likes", 0) + 1
121
 
122
  save_db(db, "cloud_playlists.json")
123
  return {"status": "success"}
124
+ except Exception as e: return {"status": "error", "message": str(e)}
125
+
126
+ # 🌟 新增:歌单评论
127
+ @router.post("/api/cloud_pl/comment")
128
+ def comment_cloud_playlist(req: PlaylistCommentReq):
129
+ try:
130
+ if len(req.content) > 99: return {"status": "error", "message": "评论过长"}
131
+ db = get_db("cloud_playlists.json")
132
+ target_dict = db.get("admin", {}) if req.type == "admin" else db.get("users", {}).get(req.author, {})
133
+ pl = target_dict.get(req.name)
134
+ if not pl or isinstance(pl, list): return {"status": "error", "message": "格式异常或不存在"}
135
+ if "comments" not in pl: pl["comments"] = []
136
+
137
+ pl["comments"].append({
138
+ "id": uuid.uuid4().hex[:8],
139
+ "user": req.username,
140
+ "text": req.content,
141
+ "time": int(time.time() * 1000)
142
+ })
143
+ save_db(db, "cloud_playlists.json")
144
+ return {"status": "success"}
145
  except Exception as e: return {"status": "error", "message": str(e)}