mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-08 22:36:37 +08:00
refactor: split modules and harden scraping workflows
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/model"
|
||||
)
|
||||
|
||||
func (d *DownloadService) notifyDownloadComplete(ctx context.Context, torrent QBitTorrent, task *model.DownloadTask) {
|
||||
if d == nil || d.notify == nil {
|
||||
return
|
||||
}
|
||||
if d.completedTorrentNotified(ctx, torrent) {
|
||||
return
|
||||
}
|
||||
d.markCompletedTorrentNotified(ctx, torrent)
|
||||
body, data := downloadCompleteNotificationPayload(torrent, task)
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
d.notify.BroadcastEvent(ctx, NotifyEvent{
|
||||
Type: EventDownloadComplete,
|
||||
Title: "MediaStationGo 下载完成",
|
||||
Message: body,
|
||||
Data: data,
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
func downloadCompleteNotificationPayload(torrent QBitTorrent, task *model.DownloadTask) (string, map[string]interface{}) {
|
||||
name := downloadCompleteNotificationName(torrent, task)
|
||||
body := fmt.Sprintf("任务:%s\n保存路径:%s\nHash:%s", name, firstNonEmpty(torrent.ContentPath, torrent.SavePath), torrent.Hash)
|
||||
data := downloadCompleteNotificationData(torrent, task)
|
||||
return body, data
|
||||
}
|
||||
|
||||
func downloadCompleteNotificationName(torrent QBitTorrent, task *model.DownloadTask) string {
|
||||
name := strings.TrimSpace(torrent.Name)
|
||||
if name == "" {
|
||||
name = strings.TrimSpace(filepath.Base(torrent.ContentPath))
|
||||
}
|
||||
if task != nil && strings.TrimSpace(task.Title) != "" {
|
||||
name = strings.TrimSpace(task.Title)
|
||||
}
|
||||
if name == "" {
|
||||
name = "下载任务"
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func downloadCompleteNotificationData(torrent QBitTorrent, task *model.DownloadTask) map[string]interface{} {
|
||||
data := map[string]interface{}{}
|
||||
if rt := strings.TrimSpace(torrent.Name); rt != "" {
|
||||
data["resource_title"] = rt
|
||||
}
|
||||
if task == nil {
|
||||
return data
|
||||
}
|
||||
addTrimmedString(data, "poster_url", task.PosterURL)
|
||||
addTrimmedString(data, "backdrop_url", task.BackdropURL)
|
||||
addTrimmedString(data, "media_type", task.MediaType)
|
||||
addTrimmedString(data, "media_category", task.MediaCategory)
|
||||
addTrimmedString(data, "title", task.Title)
|
||||
addTrimmedString(data, "overview", task.Overview)
|
||||
addTrimmedString(data, "original_title", task.OriginalName)
|
||||
addTrimmedString(data, "original_language", task.OriginalLanguage)
|
||||
if task.Year > 0 {
|
||||
data["year"] = task.Year
|
||||
}
|
||||
if task.Rating > 0 {
|
||||
data["rating"] = task.Rating
|
||||
}
|
||||
addTrimmedString(data, "genres", task.Genres)
|
||||
return data
|
||||
}
|
||||
|
||||
func addTrimmedString(data map[string]interface{}, key, value string) {
|
||||
if strings.TrimSpace(value) != "" {
|
||||
data[key] = value
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user