Skip to content

Commit de5b261

Browse files
committed
feat: only retrieve 50 download records
1 parent 24d0776 commit de5b261

File tree

9 files changed

+259
-87
lines changed

9 files changed

+259
-87
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 SDKROOT=$(xcrun --sdk macosx --show-sdk-p
6969
### Linux
7070

7171
```bash
72-
# CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o wx_video_download
72+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o wx_video_download
7373

7474
$env:CGO_ENABLED=0; $env:GOOS="linux"; $env:GOARCH="amd64"; go build -trimpath -ldflags="-s -w" -o wx_video_download
7575

cmd/server.go

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func init() {
5252
root_cmd.AddCommand(serve_cmd)
5353
serve_cmd.AddCommand(server_status_cmd)
5454
serve_cmd.AddCommand(mp_stop_cmd)
55+
serve_cmd.AddCommand(mp_restart_cmd)
5556
}
5657

5758
func serve_command() {
@@ -207,34 +208,47 @@ var mp_stop_cmd = &cobra.Command{
207208
Use: "stop",
208209
Short: "停止公众号服务",
209210
Run: func(cmd *cobra.Command, args []string) {
210-
pid, err := read_wx_pidfile()
211-
if err != nil || pid == 0 {
212-
color.Red("未发现守护进程")
213-
return
214-
}
215-
if !system.IsProcessRunning(pid) {
216-
color.Green("进程已停止")
217-
_ = remove_wx_pidfile()
218-
return
219-
}
220-
_ = system.TerminateProcess(pid)
221-
expire := time.After(8 * time.Second)
222-
tick := time.NewTicker(200 * time.Millisecond)
223-
defer tick.Stop()
224-
for {
225-
select {
226-
case <-tick.C:
227-
if !system.IsProcessRunning(pid) {
228-
_ = remove_wx_pidfile()
229-
color.Green("服务已关闭")
230-
return
231-
}
232-
case <-expire:
233-
color.Red("关闭超时")
211+
stop_daemon()
212+
},
213+
}
214+
215+
var mp_restart_cmd = &cobra.Command{
216+
Use: "restart",
217+
Short: "重启公众号服务",
218+
Run: func(cmd *cobra.Command, args []string) {
219+
stop_daemon()
220+
start_daemon()
221+
},
222+
}
223+
224+
func stop_daemon() {
225+
pid, err := read_wx_pidfile()
226+
if err != nil || pid == 0 {
227+
color.Red("未发现守护进程")
228+
return
229+
}
230+
if !system.IsProcessRunning(pid) {
231+
color.Green("进程已停止")
232+
_ = remove_wx_pidfile()
233+
return
234+
}
235+
_ = system.TerminateProcess(pid)
236+
expire := time.After(8 * time.Second)
237+
tick := time.NewTicker(200 * time.Millisecond)
238+
defer tick.Stop()
239+
for {
240+
select {
241+
case <-tick.C:
242+
if !system.IsProcessRunning(pid) {
243+
_ = remove_wx_pidfile()
244+
color.Green("服务已关闭")
234245
return
235246
}
247+
case <-expire:
248+
color.Red("关闭超时")
249+
return
236250
}
237-
},
251+
}
238252
}
239253

240254
func port_listening(addr string) bool {

internal/api/client.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"net/http"
66
"os"
7+
"sort"
78
"strconv"
89
"time"
910

@@ -41,13 +42,69 @@ func NewAPIClient(cfg *APIConfig, parent_logger *zerolog.Logger) *APIClient {
4142
official_cfg := officialaccount.NewOfficialAccountConfig(cfg.Original, cfg.RemoteServerMode)
4243
officialaccount_client := officialaccount.NewOfficialAccountClient(official_cfg, parent_logger)
4344
channels_client = channels.NewChannelsClient()
45+
46+
get_sorted_tasks := func() []*downloadpkg.Task {
47+
tasks := downloader.GetTasks()
48+
sort.Slice(tasks, func(i, j int) bool {
49+
return tasks[i].CreatedAt.After(tasks[j].CreatedAt)
50+
})
51+
return tasks
52+
}
53+
4454
channels_client.OnConnected = func(client *channels.Client) {
4555
// Initial tasks
46-
tasks := downloader.GetTasks()
47-
if data, err := json.Marshal(APIClientWSMessage{Type: "tasks", Data: tasks}); err == nil {
56+
all_tasks := get_sorted_tasks()
57+
limit := 50
58+
if limit > len(all_tasks) {
59+
limit = len(all_tasks)
60+
}
61+
tasks := all_tasks[:limit]
62+
if data, err := json.Marshal(APIClientWSMessage{Type: "tasks", Data: map[string]interface{}{
63+
"list": tasks,
64+
"total": len(all_tasks),
65+
}}); err == nil {
4866
client.Send <- data
4967
}
5068
}
69+
70+
channels_client.OnMessage = func(client *channels.Client, message []byte) {
71+
var req struct {
72+
Type string `json:"type"`
73+
Page int `json:"page"`
74+
Limit int `json:"limit"`
75+
}
76+
if err := json.Unmarshal(message, &req); err == nil && req.Type == "fetch_tasks" {
77+
allTasks := get_sorted_tasks()
78+
start := (req.Page - 1) * req.Limit
79+
if start < 0 {
80+
start = 0
81+
}
82+
if req.Limit <= 0 {
83+
req.Limit = 50
84+
}
85+
86+
if start >= len(allTasks) {
87+
if data, err := json.Marshal(APIClientWSMessage{Type: "tasks", Data: map[string]interface{}{
88+
"list": []*downloadpkg.Task{},
89+
"total": len(allTasks),
90+
}}); err == nil {
91+
client.Send <- data
92+
}
93+
return
94+
}
95+
end := start + req.Limit
96+
if end > len(allTasks) {
97+
end = len(allTasks)
98+
}
99+
tasks := allTasks[start:end]
100+
if data, err := json.Marshal(APIClientWSMessage{Type: "tasks", Data: map[string]interface{}{
101+
"list": tasks,
102+
"total": len(allTasks),
103+
}}); err == nil {
104+
client.Send <- data
105+
}
106+
}
107+
}
51108
logger := parent_logger.With().Str("Client", "api_client").Logger()
52109
client := &APIClient{
53110
downloader: downloader,

internal/api/handler.go

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/url"
1212
"os"
1313
"path/filepath"
14+
"sort"
1415
"strconv"
1516
"strings"
1617
"time"
@@ -269,10 +270,15 @@ func (c *APIClient) handleCreateFeedDownloadTask(ctx *gin.Context) {
269270
result.Err(ctx, 500, "创建任务失败:"+err.Error())
270271
return
271272
}
272-
c.channels.Broadcast(APIClientWSMessage{
273-
Type: "tasks",
274-
Data: c.downloader.GetTasks(),
275-
})
273+
task := c.downloader.GetTask(id)
274+
if task != nil {
275+
c.channels.Broadcast(APIClientWSMessage{
276+
Type: "event",
277+
Data: map[string]interface{}{
278+
"task": task,
279+
},
280+
})
281+
}
276282
result.Ok(ctx, gin.H{"id": id})
277283
}
278284

@@ -328,6 +334,9 @@ func (c *APIClient) handleFetchTaskList(ctx *gin.Context) {
328334
filter.Statuses = []base.Status{base.Status(status)}
329335
}
330336
list := c.downloader.GetTasksByFilter(filter)
337+
sort.Slice(list, func(i, j int) bool {
338+
return list[i].CreatedAt.After(list[j].CreatedAt)
339+
})
331340
total := len(list)
332341
page_num, err := strconv.Atoi(page_str)
333342
if err != nil {
@@ -429,10 +438,15 @@ func (c *APIClient) handleCreateLiveTask(ctx *gin.Context) {
429438
result.Err(ctx, 500, "创建任务失败: "+err.Error())
430439
return
431440
}
432-
c.channels.Broadcast(APIClientWSMessage{
433-
Type: "tasks",
434-
Data: c.downloader.GetTasks(),
435-
})
441+
task := c.downloader.GetTask(id)
442+
if task != nil {
443+
c.channels.Broadcast(APIClientWSMessage{
444+
Type: "event",
445+
Data: map[string]interface{}{
446+
"task": task,
447+
},
448+
})
449+
}
436450
result.Ok(ctx, gin.H{"id": id})
437451
}
438452

@@ -474,10 +488,17 @@ func (c *APIClient) handleBatchCreateTask(ctx *gin.Context) {
474488
// Int("count", len(task.Reqs)).
475489
// Dur("cost", time.Since(start)).
476490
// Msg("批量创建任务完成")
477-
c.channels.Broadcast(APIClientWSMessage{
478-
Type: "tasks",
479-
Data: c.downloader.GetTasks(),
480-
})
491+
for _, id := range ids {
492+
task := c.downloader.GetTask(id)
493+
if task != nil {
494+
c.channels.Broadcast(APIClientWSMessage{
495+
Type: "event",
496+
Data: map[string]interface{}{
497+
"task": task,
498+
},
499+
})
500+
}
501+
}
481502
result.Ok(ctx, gin.H{"ids": ids})
482503
}
483504

@@ -682,10 +703,15 @@ func (c *APIClient) handleCreateChannelsTask(ctx *gin.Context) {
682703
result.Err(ctx, 500, "下载失败")
683704
return
684705
}
685-
c.channels.Broadcast(APIClientWSMessage{
686-
Type: "tasks",
687-
Data: c.downloader.GetTasks(),
688-
})
706+
task := c.downloader.GetTask(id)
707+
if task != nil {
708+
c.channels.Broadcast(APIClientWSMessage{
709+
Type: "event",
710+
Data: map[string]interface{}{
711+
"task": task,
712+
},
713+
})
714+
}
689715
result.Ok(ctx, gin.H{"id": id})
690716
}
691717

internal/channels/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type ChannelsClient struct {
4242
cache *cache.Cache
4343
req_seq uint64
4444
OnConnected func(client *Client)
45+
OnMessage func(client *Client, message []byte)
4546
}
4647

4748
func NewChannelsClient() *ChannelsClient {
@@ -92,8 +93,13 @@ func (c *ChannelsClient) HandleChannelsWebsocket(ctx *gin.Context) {
9293
c.requests_mu.RUnlock()
9394
if ok {
9495
ch <- resp
96+
continue
9597
}
9698
}
99+
100+
if c.OnMessage != nil {
101+
c.OnMessage(client, message)
102+
}
97103
}
98104
}
99105
func (c *ChannelsClient) Stop() {

internal/interceptor/inject/src/components.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function total_speed(tasks) {
303303
return sum;
304304
}
305305

306-
function __wx_refresh_downloader(selector, tasks) {
306+
function __wx_refresh_downloader(selector, tasks, total) {
307307
const container = document.querySelector(selector);
308308
if (!container) return;
309309
container.innerHTML = "";
@@ -313,7 +313,8 @@ function __wx_refresh_downloader(selector, tasks) {
313313

314314
const countEl = document.getElementById("wx-dl-count");
315315
if (countEl) {
316-
countEl.innerText = list.length > 0 ? `(${list.length})` : "";
316+
const count = total !== undefined ? total : list.length;
317+
countEl.innerText = count > 0 ? `(${count})` : "";
317318
}
318319

319320
if (list.length === 0) {
@@ -470,12 +471,12 @@ function __wx_refresh_downloader(selector, tasks) {
470471
});
471472

472473
if (list.length > 0) {
473-
const footer = document.createElement("div");
474-
footer.className = "weui-loadmore weui-loadmore_line";
475-
footer.style.marginTop = "20px";
476-
footer.innerHTML =
477-
'<span class="weui-loadmore__tips">没有更多内容了</span>';
478-
container.appendChild(footer);
474+
// const footer = document.createElement("div");
475+
// footer.className = "weui-loadmore weui-loadmore_line";
476+
// footer.style.marginTop = "20px";
477+
// footer.innerHTML =
478+
// '<span class="weui-loadmore__tips">没有更多内容了</span>';
479+
// container.appendChild(footer);
479480
}
480481
}
481482

0 commit comments

Comments
 (0)