ZHIWEI666 commited on
Commit
7b74f04
·
verified ·
1 Parent(s): 13880c6

Upload router_items.py

Browse files
Files changed (1) hide show
  1. router_items.py +20 -15
router_items.py CHANGED
@@ -2,13 +2,12 @@
2
  from fastapi import APIRouter, HTTPException
3
  import time
4
  import uuid
5
- import datetime # 【新增】:处理时间序列
6
  import 数据库连接 as db
7
  from models import ItemCreate, ItemUpdate
8
 
9
  router = APIRouter()
10
 
11
- # 【新增】:获取最近 6 个月的年月字符串列表 (例如: ['2025-10', '2025-11', ..., '2026-03'])
12
  def get_last_6_months():
13
  res = []
14
  today = datetime.date.today()
@@ -25,7 +24,13 @@ def get_last_6_months():
25
  async def get_items(type: str = "tool", sort: str = "time", limit: int = 20):
26
  items_db = db.load_data("items.json", default_data=[])
27
  comments_db = db.load_data("comments.json", default_data={})
28
- filtered_items = [item for item in items_db if item.get("type") == type]
 
 
 
 
 
 
29
  for item in filtered_items:
30
  item["commentsData"] = comments_db.get(item["id"], [])
31
  item["comments"] = len(item["commentsData"])
@@ -47,33 +52,37 @@ async def get_creators(sort: str = "downloads", limit: int = 20):
47
  for account, u in users_db.items():
48
  u_items = [i for i in items_db if i.get("author") == account]
49
 
50
- # 【核心修正】:计算真实的聚合增长曲线
51
  trend_tools = {m: 0 for m in months}
52
  trend_apps = {m: 0 for m in months}
 
53
  tools_count = 0
54
  apps_count = 0
55
 
56
  for i in u_items:
57
- itype = i.get("type")
58
  history = i.get("use_history", {})
59
- if itype == "tool":
60
- tools_count += 1
61
  for m in months: trend_tools[m] += history.get(m, 0)
62
- elif itype == "app":
63
- apps_count += 1
64
  for m in months: trend_apps[m] += history.get(m, 0)
 
 
 
65
 
66
  creators.append({
67
  "account": account, "name": u.get("name", account), "avatar": u.get("avatarDataUrl", "https://via.placeholder.com/150"),
68
  "shortDesc": u.get("intro", "这个人很懒,什么都没写..."), "fullDesc": u.get("intro", "这个人很懒,什么都没写..."),
69
  "likes": sum(i.get("likes", 0) for i in u_items), "favorites": sum(i.get("favorites", 0) for i in u_items),
70
- "downloads": sum(i.get("uses", 0) for i in u_items), # 创作者总下载量
71
  "toolsCount": tools_count, "appsCount": apps_count, "followers": len(u.get("followers", [])), "created_at": u.get("created_at", 0),
72
  "commentsData": comments_db.get(account, []),
73
  "trendData": {
74
  "months": months,
75
  "tools": [trend_tools[m] for m in months],
76
- "apps": [trend_apps[m] for m in months]
 
77
  }
78
  })
79
 
@@ -129,19 +138,15 @@ async def delete_item(item_id: str, author: str):
129
 
130
  @router.post("/api/items/{item_id}/use")
131
  async def record_item_use(item_id: str):
132
- """记录资源被获取/使用的次数,并计入时间序列"""
133
  items_db = db.load_data("items.json", default_data=[])
134
  current_month = datetime.date.today().strftime("%Y-%m")
135
 
136
  for item in items_db:
137
  if item["id"] == item_id:
138
  item["uses"] = item.get("uses", 0) + 1
139
- # 初始化时间序列历史并累加
140
  if "use_history" not in item:
141
  item["use_history"] = {}
142
  item["use_history"][current_month] = item["use_history"].get(current_month, 0) + 1
143
-
144
  db.save_data("items.json", items_db)
145
  return {"status": "success", "uses": item["uses"]}
146
-
147
  raise HTTPException(status_code=404, detail="找不到该内容记录")
 
2
  from fastapi import APIRouter, HTTPException
3
  import time
4
  import uuid
5
+ import datetime
6
  import 数据库连接 as db
7
  from models import ItemCreate, ItemUpdate
8
 
9
  router = APIRouter()
10
 
 
11
  def get_last_6_months():
12
  res = []
13
  today = datetime.date.today()
 
24
  async def get_items(type: str = "tool", sort: str = "time", limit: int = 20):
25
  items_db = db.load_data("items.json", default_data=[])
26
  comments_db = db.load_data("comments.json", default_data={})
27
+
28
+ # 【核心修改】:如果是推荐榜,匹配所有 recommend 开头的子类型
29
+ if type == "recommend":
30
+ filtered_items = [item for item in items_db if item.get("type", "").startswith("recommend")]
31
+ else:
32
+ filtered_items = [item for item in items_db if item.get("type") == type]
33
+
34
  for item in filtered_items:
35
  item["commentsData"] = comments_db.get(item["id"], [])
36
  item["comments"] = len(item["commentsData"])
 
52
  for account, u in users_db.items():
53
  u_items = [i for i in items_db if i.get("author") == account]
54
 
 
55
  trend_tools = {m: 0 for m in months}
56
  trend_apps = {m: 0 for m in months}
57
+ trend_recommends = {m: 0 for m in months} # 新增:推荐增长序列
58
  tools_count = 0
59
  apps_count = 0
60
 
61
  for i in u_items:
62
+ itype = i.get("type", "")
63
  history = i.get("use_history", {})
64
+ if itype == "tool" or itype == "recommend_tool":
65
+ if itype == "tool": tools_count += 1
66
  for m in months: trend_tools[m] += history.get(m, 0)
67
+ elif itype == "app" or itype == "recommend_app":
68
+ if itype == "app": apps_count += 1
69
  for m in months: trend_apps[m] += history.get(m, 0)
70
+ elif itype.startswith("recommend"):
71
+ # 纯链接推荐
72
+ for m in months: trend_recommends[m] += history.get(m, 0)
73
 
74
  creators.append({
75
  "account": account, "name": u.get("name", account), "avatar": u.get("avatarDataUrl", "https://via.placeholder.com/150"),
76
  "shortDesc": u.get("intro", "这个人很懒,什么都没写..."), "fullDesc": u.get("intro", "这个人很懒,什么都没写..."),
77
  "likes": sum(i.get("likes", 0) for i in u_items), "favorites": sum(i.get("favorites", 0) for i in u_items),
78
+ "downloads": sum(i.get("uses", 0) for i in u_items),
79
  "toolsCount": tools_count, "appsCount": apps_count, "followers": len(u.get("followers", [])), "created_at": u.get("created_at", 0),
80
  "commentsData": comments_db.get(account, []),
81
  "trendData": {
82
  "months": months,
83
  "tools": [trend_tools[m] for m in months],
84
+ "apps": [trend_apps[m] for m in months],
85
+ "recommends": [trend_recommends[m] for m in months] # 注入前端
86
  }
87
  })
88
 
 
138
 
139
  @router.post("/api/items/{item_id}/use")
140
  async def record_item_use(item_id: str):
 
141
  items_db = db.load_data("items.json", default_data=[])
142
  current_month = datetime.date.today().strftime("%Y-%m")
143
 
144
  for item in items_db:
145
  if item["id"] == item_id:
146
  item["uses"] = item.get("uses", 0) + 1
 
147
  if "use_history" not in item:
148
  item["use_history"] = {}
149
  item["use_history"][current_month] = item["use_history"].get(current_month, 0) + 1
 
150
  db.save_data("items.json", items_db)
151
  return {"status": "success", "uses": item["uses"]}
 
152
  raise HTTPException(status_code=404, detail="找不到该内容记录")