ZHIWEI666 commited on
Commit
c00dff0
·
verified ·
1 Parent(s): 3c4577f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # ⚙️ 后端逻辑/核心服务端.py
2
  from fastapi import FastAPI, HTTPException, File, UploadFile, Form
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from pydantic import BaseModel
@@ -7,6 +7,7 @@ import time
7
  import uuid
8
  import hashlib
9
 
 
10
  import 数据库连接 as db
11
 
12
  app = FastAPI(title="ComfyUI Ranking Community API")
@@ -37,7 +38,10 @@ async def upload_file(file: UploadFile = File(...), file_type: str = Form(...)):
37
  db.save_file(full_path_in_repo, content)
38
  url = f"https://huggingface.co/datasets/{db.DATASET_REPO_ID}/resolve/main/{full_path_in_repo}"
39
 
40
- return {"status": "success", "url": url, "display_name": original_name, "hashed_name": new_filename}
 
 
 
41
 
42
  # --- 数据模型 ---
43
  class UserRegister(BaseModel):
@@ -135,9 +139,17 @@ def add_notification(target_account: str, notif_data: dict):
135
  @app.post("/api/users/register")
136
  async def register_user(user: UserRegister):
137
  users_db = db.load_data("users.json", default_data={})
138
- if user.account in users_db: raise HTTPException(status_code=400, detail="该账号已被注册")
 
 
 
 
139
  new_user = user.dict()
140
- new_user.update({"created_at": int(time.time()), "followers": [], "following": [], "privacy": {"follows": False, "likes": False, "favorites": False, "downloads": False}})
 
 
 
 
141
  users_db[user.account] = new_user
142
  db.save_data("users.json", users_db)
143
  return {"status": "success", "message": "注册成功", "data": {k: v for k, v in new_user.items() if k != "password"}}
@@ -155,6 +167,7 @@ async def get_user_profile(account: str):
155
  users_db = db.load_data("users.json", default_data={})
156
  if account not in users_db: raise HTTPException(status_code=404, detail="用户不存在")
157
  user_data = users_db[account]
 
158
  items_db = db.load_data("items.json", default_data=[])
159
  user_items = [item for item in items_db if item.get("author") == account]
160
  user_data["receivedLikes"] = sum(item.get("likes", 0) for item in user_items)
@@ -193,7 +206,6 @@ async def toggle_follow(follow: FollowToggle):
193
  if follow.is_active:
194
  if follow.user_id not in target_followers:
195
  target_followers.append(follow.user_id)
196
- # 派发关注通知
197
  add_notification(follow.target_account, {"type": "follow", "from_user": follow.user_id})
198
  if follow.target_account not in current_following: current_following.append(follow.target_account)
199
  else:
@@ -202,14 +214,14 @@ async def toggle_follow(follow: FollowToggle):
202
  db.save_data("users.json", users_db)
203
  return {"status": "success"}
204
 
205
- # --- 2. 排行榜与发布 ---
206
  @app.get("/api/items")
207
  async def get_items(type: str = "tool", sort: str = "time", limit: int = 20):
208
  items_db = db.load_data("items.json", default_data=[])
209
  comments_db = db.load_data("comments.json", default_data={})
 
210
  filtered_items = [item for item in items_db if item.get("type") == type]
211
  for item in filtered_items:
212
- # 动态挂载评论区数据
213
  item["commentsData"] = comments_db.get(item["id"], [])
214
  item["comments"] = len(item["commentsData"])
215
 
@@ -239,7 +251,7 @@ async def get_creators(sort: str = "downloads", limit: int = 20):
239
  "shortDesc": u.get("intro", "这个人很懒,什么都没写..."), "fullDesc": u.get("intro", "这个人很懒,什么都没写..."),
240
  "likes": likes, "favorites": favorites, "downloads": uses, "toolsCount": tools_count, "appsCount": apps_count,
241
  "followers": len(u.get("followers", [])), "created_at": u.get("created_at", 0),
242
- "commentsData": comments_db.get(account, []), # 挂载留言板数据
243
  "trendData": {"tools": [0,0,0,0,0, tools_count], "apps": [0,0,0,0,0, apps_count]}
244
  })
245
 
@@ -279,18 +291,19 @@ async def toggle_interaction(interaction: InteractionToggle):
279
  if interaction.user_id not in user_list:
280
  user_list.append(interaction.user_id)
281
  target_item[count_key] += 1
282
- # 派发点赞/收藏通知给作者
283
  add_notification(target_item.get("author", ""), {
284
  "type": interaction.action_type, "from_user": interaction.user_id,
285
  "target_item_id": target_item["id"], "target_item_title": target_item["title"]
286
  })
287
  else:
288
- if interaction.user_id in user_list: user_list.remove(interaction.user_id); target_item[count_key] = max(0, target_item[count_key] - 1)
 
 
289
 
290
  db.save_data("items.json", items_db)
291
  return {"status": "success", "new_count": target_item[count_key]}
292
 
293
- # --- 4. 评论系统与消息 ---
294
  @app.post("/api/comments")
295
  async def post_comment(comment: CommentCreate):
296
  comments_db = db.load_data("comments.json", default_data={})
@@ -311,13 +324,11 @@ async def post_comment(comment: CommentCreate):
311
  parent = next((c for c in item_comments if c["id"] == comment.parent_id), None)
312
  if parent:
313
  parent["replies"].append(new_comment)
314
- # 通知被回复者
315
  if comment.reply_to_user:
316
  add_notification(comment.reply_to_user, {"type": "reply", "from_user": comment.author, "target_item_id": comment.item_id, "target_item_title": "收到新的回复", "content": comment.content})
317
- else: raise HTTPException(status_code=404, detail="找不到要回复的评论")
318
  else:
319
  item_comments.append(new_comment)
320
- # 通知作品作者 或 留言板主人
321
  items_db = db.load_data("items.json", default_data=[])
322
  target_item = next((item for item in items_db if item["id"] == comment.item_id), None)
323
  if target_item:
 
1
+ # ⚙️ 后端逻辑/核心服务端.py (Hugging Face Spaces app.py)
2
  from fastapi import FastAPI, HTTPException, File, UploadFile, Form
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from pydantic import BaseModel
 
7
  import uuid
8
  import hashlib
9
 
10
+ # 导入数据库操作模块
11
  import 数据库连接 as db
12
 
13
  app = FastAPI(title="ComfyUI Ranking Community API")
 
38
  db.save_file(full_path_in_repo, content)
39
  url = f"https://huggingface.co/datasets/{db.DATASET_REPO_ID}/resolve/main/{full_path_in_repo}"
40
 
41
+ return {
42
+ "status": "success", "url": url,
43
+ "display_name": original_name, "hashed_name": new_filename
44
+ }
45
 
46
  # --- 数据模型 ---
47
  class UserRegister(BaseModel):
 
139
  @app.post("/api/users/register")
140
  async def register_user(user: UserRegister):
141
  users_db = db.load_data("users.json", default_data={})
142
+ if user.account in users_db: raise HTTPException(status_code=400, detail="该账号已被注册,请尝试其他账号名")
143
+ for existing_user in users_db.values():
144
+ if existing_user.get("email") == user.email: raise HTTPException(status_code=400, detail="该邮箱已被绑定")
145
+ if existing_user.get("phone") == user.phone: raise HTTPException(status_code=400, detail="该手机号已被绑定")
146
+
147
  new_user = user.dict()
148
+ new_user.update({
149
+ "receivedLikes": 0, "receivedFollows": 0, "created_at": int(time.time()), "followers": [], "following": [],
150
+ "privacy": {"follows": False, "likes": False, "favorites": False, "downloads": False}
151
+ })
152
+
153
  users_db[user.account] = new_user
154
  db.save_data("users.json", users_db)
155
  return {"status": "success", "message": "注册成功", "data": {k: v for k, v in new_user.items() if k != "password"}}
 
167
  users_db = db.load_data("users.json", default_data={})
168
  if account not in users_db: raise HTTPException(status_code=404, detail="用户不存在")
169
  user_data = users_db[account]
170
+
171
  items_db = db.load_data("items.json", default_data=[])
172
  user_items = [item for item in items_db if item.get("author") == account]
173
  user_data["receivedLikes"] = sum(item.get("likes", 0) for item in user_items)
 
206
  if follow.is_active:
207
  if follow.user_id not in target_followers:
208
  target_followers.append(follow.user_id)
 
209
  add_notification(follow.target_account, {"type": "follow", "from_user": follow.user_id})
210
  if follow.target_account not in current_following: current_following.append(follow.target_account)
211
  else:
 
214
  db.save_data("users.json", users_db)
215
  return {"status": "success"}
216
 
217
+ # --- 2. 排行榜核心数据流 ---
218
  @app.get("/api/items")
219
  async def get_items(type: str = "tool", sort: str = "time", limit: int = 20):
220
  items_db = db.load_data("items.json", default_data=[])
221
  comments_db = db.load_data("comments.json", default_data={})
222
+
223
  filtered_items = [item for item in items_db if item.get("type") == type]
224
  for item in filtered_items:
 
225
  item["commentsData"] = comments_db.get(item["id"], [])
226
  item["comments"] = len(item["commentsData"])
227
 
 
251
  "shortDesc": u.get("intro", "这个人很懒,什么都没写..."), "fullDesc": u.get("intro", "这个人很懒,什么都没写..."),
252
  "likes": likes, "favorites": favorites, "downloads": uses, "toolsCount": tools_count, "appsCount": apps_count,
253
  "followers": len(u.get("followers", [])), "created_at": u.get("created_at", 0),
254
+ "commentsData": comments_db.get(account, []),
255
  "trendData": {"tools": [0,0,0,0,0, tools_count], "apps": [0,0,0,0,0, apps_count]}
256
  })
257
 
 
291
  if interaction.user_id not in user_list:
292
  user_list.append(interaction.user_id)
293
  target_item[count_key] += 1
 
294
  add_notification(target_item.get("author", ""), {
295
  "type": interaction.action_type, "from_user": interaction.user_id,
296
  "target_item_id": target_item["id"], "target_item_title": target_item["title"]
297
  })
298
  else:
299
+ if interaction.user_id in user_list:
300
+ user_list.remove(interaction.user_id)
301
+ target_item[count_key] = max(0, target_item[count_key] - 1)
302
 
303
  db.save_data("items.json", items_db)
304
  return {"status": "success", "new_count": target_item[count_key]}
305
 
306
+ # --- 4. 评论系统与通知 ---
307
  @app.post("/api/comments")
308
  async def post_comment(comment: CommentCreate):
309
  comments_db = db.load_data("comments.json", default_data={})
 
324
  parent = next((c for c in item_comments if c["id"] == comment.parent_id), None)
325
  if parent:
326
  parent["replies"].append(new_comment)
 
327
  if comment.reply_to_user:
328
  add_notification(comment.reply_to_user, {"type": "reply", "from_user": comment.author, "target_item_id": comment.item_id, "target_item_title": "收到新的回复", "content": comment.content})
329
+ else: raise HTTPException(status_code=404, detail="找不到要回复的父级评论")
330
  else:
331
  item_comments.append(new_comment)
 
332
  items_db = db.load_data("items.json", default_data=[])
333
  target_item = next((item for item in items_db if item["id"] == comment.item_id), None)
334
  if target_item: