mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-29 11:36:36 +08:00
feat(repair): 新增全库修复+重刮入口,剧集类路径正确归类
- 服务层 RepairAndRescrapeAllLibraries:先从路径占位符回填缺失/错误外部ID,再逐库重刮(含 no_match 重试) - 新增 handler 与路由 POST /api/admin/media/repair-rescrape,异步执行经 WS 推送进度 - 前端媒体库页新增「全库修复+重刮」按钮调用该接口 - groupSeries:路径落在剧集类目录(电视剧/动漫/特别篇等)时按剧集卡处理,跳转分类视图
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
// Package handler — batch repair+rescrape endpoint.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/service"
|
||||
)
|
||||
|
||||
// repairAndRescrapeAllHandler 触发"全库修复+重刮"流程:先从媒体路径中的
|
||||
// {tmdb-N}/{bangumi-N} 等占位符回填缺失的外部 ID, 再对所有媒体库重刮一遍。
|
||||
//
|
||||
// 路由: POST /api/admin/media/repair-rescrape (需 admin)
|
||||
// 异步执行, 立即返回 202;通过 WS hub "scrape" topic 推送进度。
|
||||
func repairAndRescrapeAllHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
task := startScrapeHTTPTask(svc, "全库修复并重刮", "", "")
|
||||
go func() {
|
||||
result, err := svc.RepairAndRescrapeAllLibraries(context.Background())
|
||||
metrics := map[string]int64{
|
||||
"repaired": int64(result.Repaired),
|
||||
"libraries": int64(result.Libraries),
|
||||
"matched": int64(result.Matched),
|
||||
}
|
||||
stage := "completed"
|
||||
message := "全库修复并重刮完成"
|
||||
if err != nil {
|
||||
stage = "scrape"
|
||||
message = "全库修复并重刮失败"
|
||||
}
|
||||
finishHTTPTask(task, err, stage, message, metrics, nil)
|
||||
}()
|
||||
c.JSON(http.StatusAccepted, gin.H{"status": "started"})
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,9 @@ func registerAdminRoutes(api *gin.RouterGroup, cfg *config.Config, svc *service.
|
||||
admin.GET("/organize/sources", organizeSourcesHandler(svc))
|
||||
admin.POST("/organize/source", organizeDirectoryHandler(svc))
|
||||
|
||||
// 全库修复+重刮:从路径占位符回填缺失外部 ID,然后批量重刮整库。
|
||||
admin.POST("/media/repair-rescrape", repairAndRescrapeAllHandler(svc))
|
||||
|
||||
// API key management (encrypted at rest).
|
||||
admin.GET("/api-configs", listAPIConfigsHandler(svc))
|
||||
admin.GET("/api-configs/:provider", getAPIConfigHandler(svc))
|
||||
|
||||
@@ -106,3 +106,63 @@ func cloudPathRepairShouldReplaceTitle(current, hinted string) bool {
|
||||
}
|
||||
return len([]rune(current)) > len([]rune(hinted))*2
|
||||
}
|
||||
|
||||
|
||||
// RepairAndRescrapeResult 汇总一次「全库修复+重刮」的结果。
|
||||
type RepairAndRescrapeResult struct {
|
||||
Repaired int `json:"repaired"` // 从路径占位符回填外部 ID 的媒体数
|
||||
Libraries int `json:"libraries"` // 参与重刮的媒体库数
|
||||
Matched int `json:"matched"` // 重刮后成功匹配的媒体数
|
||||
}
|
||||
|
||||
// RepairAndRescrapeAllLibraries 修复并重刮所有媒体库:先从媒体路径中的
|
||||
// {tmdb-123}/{bangumi-456} 等占位符回填缺失或错误的外部 ID(回填后会把相关
|
||||
// 行的 scrape_status 重置为 pending),随后逐个媒体库重刮(含 no_match 重试),
|
||||
// 让此前因空 ID / 脏 ID 无法刮削的媒体重新匹配到正确数据。
|
||||
func (c *Container) RepairAndRescrapeAllLibraries(ctx context.Context) (RepairAndRescrapeResult, error) {
|
||||
var result RepairAndRescrapeResult
|
||||
if c == nil || c.Repo == nil || c.Repo.DB == nil {
|
||||
return result, nil
|
||||
}
|
||||
repaired, err := c.RepairCloudPathMetadata(ctx)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Repaired = repaired
|
||||
|
||||
if c.Scraper == nil || c.Repo.Library == nil {
|
||||
return result, nil
|
||||
}
|
||||
libraries, err := c.Repo.Library.List(ctx)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
for i := range libraries {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return result, ctx.Err()
|
||||
default:
|
||||
}
|
||||
lib := libraries[i]
|
||||
if !lib.Enabled {
|
||||
continue
|
||||
}
|
||||
result.Libraries++
|
||||
// retryNoMatch=true:连之前匹配失败的也再试一次,因为这次可能已回填到正确 ID。
|
||||
matched, err := c.Scraper.EnrichLibrary(ctx, lib.ID, true)
|
||||
if err != nil {
|
||||
if c.Log != nil {
|
||||
c.Log.Warn("repair rescrape library failed", zap.String("library", lib.ID), zap.Error(err))
|
||||
}
|
||||
continue
|
||||
}
|
||||
result.Matched += matched
|
||||
}
|
||||
if c.Log != nil {
|
||||
c.Log.Info("repair and rescrape all libraries done",
|
||||
zap.Int("repaired", result.Repaired),
|
||||
zap.Int("libraries", result.Libraries),
|
||||
zap.Int("matched", result.Matched))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -96,4 +96,12 @@ export const toolsAPI = {
|
||||
api
|
||||
.post<{ message: string }>('/admin/notify/test', { title, body })
|
||||
.then((r) => r.data),
|
||||
|
||||
// repairAndRescrapeAll 触发「全库修复+重刮」:先从媒体路径中的
|
||||
// {tmdb-N}/{bangumi-N} 占位符回填缺失/错误的外部 ID,再批量重刮整库。
|
||||
// 后端异步执行,立即返回;进度通过 WS "scrape" topic 推送。
|
||||
repairAndRescrapeAll: () =>
|
||||
api
|
||||
.post<{ status: string }>('/admin/media/repair-rescrape', {})
|
||||
.then((r) => r.data),
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { useEffect, useMemo, useState, type ReactNode } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { motion } from 'framer-motion'
|
||||
import { ArrowRight, Film, FolderOpen, Library as LibraryIcon, Music, PlayCircle, Tv } from 'lucide-react'
|
||||
import { ArrowRight, Film, FolderOpen, Library as LibraryIcon, Music, PlayCircle, RefreshCw, Tv } from 'lucide-react'
|
||||
|
||||
import { imageURL } from '../api/client'
|
||||
import { libraryAPI } from '../api/library'
|
||||
import { toolsAPI } from '../api/tools'
|
||||
import { MediaCard } from '../components/MediaCard'
|
||||
import type { Library, Media } from '../types'
|
||||
import { artworkScore, groupSeries, seriesCardLink, type SeriesCard } from '../utils/groupSeries'
|
||||
@@ -35,6 +36,22 @@ const TYPE_LABELS: Record<string, string> = {
|
||||
export function LibrariesPage() {
|
||||
const [previews, setPreviews] = useState<LibraryPreview[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [repairing, setRepairing] = useState(false)
|
||||
const [repairMsg, setRepairMsg] = useState('')
|
||||
|
||||
async function handleRepairRescrape() {
|
||||
if (repairing) return
|
||||
setRepairing(true)
|
||||
setRepairMsg('')
|
||||
try {
|
||||
await toolsAPI.repairAndRescrapeAll()
|
||||
setRepairMsg('已开始全库修复+重刮,进度可在任务中查看。')
|
||||
} catch {
|
||||
setRepairMsg('启动失败,请稍后重试。')
|
||||
} finally {
|
||||
setRepairing(false)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
@@ -75,10 +92,23 @@ export function LibrariesPage() {
|
||||
共 {previews.length} 个目录 · {total.toLocaleString()} 个条目。每个目录直接展示最新入库内容。
|
||||
</p>
|
||||
</div>
|
||||
<Link to="/admin" className="btn-outline">
|
||||
管理媒体库
|
||||
<ArrowRight size={14} />
|
||||
</Link>
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
{repairMsg && <span className="text-xs text-ink-50">{repairMsg}</span>}
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleRepairRescrape}
|
||||
disabled={repairing}
|
||||
className="btn-outline disabled:cursor-not-allowed disabled:opacity-60"
|
||||
title="从媒体路径回填缺失/错误的外部 ID,再批量重刮整库"
|
||||
>
|
||||
<RefreshCw size={14} className={repairing ? 'animate-spin' : ''} />
|
||||
{repairing ? '正在启动…' : '全库修复+重刮'}
|
||||
</button>
|
||||
<Link to="/admin" className="btn-outline">
|
||||
管理媒体库
|
||||
<ArrowRight size={14} />
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{previews.length === 0 ? (
|
||||
|
||||
@@ -38,8 +38,24 @@ export function isEpisodeLike(media: Media): boolean {
|
||||
return (media.season_num ?? 0) > 0 || (media.episode_num ?? 0) > 0
|
||||
}
|
||||
|
||||
// 剧集类目录名(电视剧/动漫及其二级分类)。媒体路径落在这些目录下时, 即便
|
||||
// 季集号未识别出来, 也应按剧集对待, 跳转到 /library 分类视图而非 /media 单页。
|
||||
const EPISODIC_PATH_RE =
|
||||
/[\\/](?:电视剧|剧集|国产剧|欧美剧|日韩剧|日剧|韩剧|综艺|纪录片|动漫|番剧|国漫|日番|儿童|tv|series|shows?|season[\s._-]*\d|s\d{1,2}(?:[\s._-]|[\\/])|specials?|sp|ova|oad|特别篇|特別篇|番外|特典)[\\/]/i
|
||||
|
||||
function pathLooksEpisodic(media: Media): boolean {
|
||||
const path = (media.path || media.display_library_path || media.library_path || '')
|
||||
return EPISODIC_PATH_RE.test(path)
|
||||
}
|
||||
|
||||
export function isSeriesCard(card: SeriesCard): boolean {
|
||||
return card.count > 1 || isEpisodeLike(card.rep) || isEpisodeLike(card.linkMedia)
|
||||
return (
|
||||
card.count > 1 ||
|
||||
isEpisodeLike(card.rep) ||
|
||||
isEpisodeLike(card.linkMedia) ||
|
||||
pathLooksEpisodic(card.rep) ||
|
||||
pathLooksEpisodic(card.linkMedia)
|
||||
)
|
||||
}
|
||||
|
||||
export function seriesTitle(media: Media): string {
|
||||
|
||||
Reference in New Issue
Block a user