mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-01 20:16:36 +08:00
7bd6bcfb1b
Backend
- service/bangumi.go: Bangumi (bgm.tv) scraper for anime libraries.
- service/episode_parser.go: SxxExx / NxE / EPxx / 第NN集 parser with
unit tests; consumed by the scanner for tv/anime libraries.
- service/scraper.go: provider chain orchestrator picks Bangumi for
anime libraries (TMDb fallback), TMDb for everything else; emits
'no_match' rows so we don't retry forever; AnyEnabled() for the
scanner kick.
- service/scanner.go: writes season/episode numbers for tv/anime libs
and reports a 'probed' counter alongside 'added'.
- service/transcoder.go (existing): unchanged, stays per-media.
- service/subtitle.go: discovers external subtitles next to the source
file (.srt/.vtt/.ass/.ssa) and converts SRT/ASS to WebVTT on the fly.
- service/qbittorrent.go: thread-safe qBittorrent v2 Web UI client
(login / add / list / delete) with cookie-jar reuse.
- service/downloads.go: persists download tasks, reads runtime
qbittorrent.* settings, polls /torrents/info every 5 s and pushes
the result to WS subscribers ('download' topic).
- service/subscription.go: 10-minute RSS poller with regex filter,
GUID dedup persisted in the settings table, and 'subscription' WS
events on enqueue.
- service/watcher.go: fsnotify watcher with 5 s coalescing debouncer
that triggers per-library rescans on create/rename/remove.
- service/stats.go: dashboard snapshot — totals, recently added,
gopsutil-driven CPU/mem/disk readings.
- service/profile.go: non-credential profile patch + admin role mutator.
- service/audit.go: best-effort writer for the access_logs table.
- service/service.go: container wires every new service; Boot() spins
up watcher / downloads poller / subscription scheduler; Close()
tears them down on graceful shutdown.
Handlers
- new files: downloads.go, subscriptions.go, subtitles.go, series.go,
stats.go, profile.go, util.go.
- handler.go: registers PATCH /me, /libraries/:id/seasons,
/media/:id/subtitles, /subtitles/:id, /downloads*, /subscriptions*,
/stats, /admin/users/:id/role.
- auth.go / media.go: write audit rows for login + library CRUD and
refresh the watcher when libraries change.
Frontend
- api: new helpers for downloads, subscriptions, profile, series, stats,
subtitles; library helper gained scrape().
- hooks/useWebSocket.ts: shared connection with 3 s reconnect.
- components/GlobalEvents.tsx: surfaces scan / scrape / subscription
completion as toasts (mounted at app root).
- pages: Library now switches to a season-grouped layout for tv/anime
libraries; Player attaches WebVTT <track> elements; new pages for
Downloads (live torrent table), Subscriptions, Profile, Stats.
- components/Layout.tsx + App.tsx: sidebar groups (媒体库 / 自动化 /
账号 / 管理) and routes for the new pages; /stats and /admin remain
admin-only.
- types/index.ts: new types — Subscription, DownloadTask, QBitTorrent,
Hardware, StatsSnapshot.
Verified: go build, go vet, go test (incl. ParseEpisode + srtToVTT +
stripASSTags) all pass; tsc -b && vite build emits 17 route chunks plus
the deferred hls chunk; main bundle 247 KB / 83 KB gzipped.
225 lines
6.3 KiB
Go
225 lines
6.3 KiB
Go
// Package service — scraper orchestrator.
|
|
//
|
|
// ScraperService takes a Media row and tries to enrich it with metadata
|
|
// from one or more providers. Selection is driven by the library type:
|
|
//
|
|
// library.type == "anime" -> Bangumi (fallback: TMDb)
|
|
// library.type == "tv" -> TMDb (movies) — TV episodes inherit
|
|
// series metadata; episode-level scraping
|
|
// is left as a future step
|
|
// default -> TMDb
|
|
//
|
|
// The orchestrator publishes scrape progress events on the WS hub.
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/config"
|
|
"github.com/ShukeBta/MediaStationGo/internal/model"
|
|
"github.com/ShukeBta/MediaStationGo/internal/repository"
|
|
)
|
|
|
|
// ScraperService coordinates metadata enrichment across providers.
|
|
type ScraperService struct {
|
|
cfg *config.Config
|
|
log *zap.Logger
|
|
repo *repository.Container
|
|
tmdb *TMDbProvider
|
|
bangumi *BangumiProvider
|
|
hub *Hub
|
|
}
|
|
|
|
// NewScraperService is the constructor.
|
|
func NewScraperService(
|
|
cfg *config.Config,
|
|
log *zap.Logger,
|
|
repo *repository.Container,
|
|
tmdb *TMDbProvider,
|
|
bangumi *BangumiProvider,
|
|
hub *Hub,
|
|
) *ScraperService {
|
|
return &ScraperService{
|
|
cfg: cfg, log: log, repo: repo,
|
|
tmdb: tmdb, bangumi: bangumi, hub: hub,
|
|
}
|
|
}
|
|
|
|
// yearPattern extracts a 4-digit year (1900-2099).
|
|
var yearPattern = regexp.MustCompile(`(?:^|[^\d])(19\d{2}|20\d{2})(?:[^\d]|$)`)
|
|
|
|
// noiseTokens are stripped before search.
|
|
var noiseTokens = []string{
|
|
"1080p", "2160p", "4k", "720p", "480p",
|
|
"hdrip", "bluray", "blu-ray", "webrip", "web-dl", "web",
|
|
"x264", "x265", "h264", "h265", "hevc", "avc",
|
|
"hdr", "sdr", "dts", "ddp", "atmos", "aac", "ac3", "flac",
|
|
"remux", "extended", "uncut", "directors-cut", "directors_cut",
|
|
"hkfree", "yify", "rarbg", "ettv", "fgt",
|
|
}
|
|
|
|
// bracketedTag matches "[anything]" or "(anything)" segments.
|
|
var bracketedTag = regexp.MustCompile(`[\[\(][^\]\)]*[\]\)]`)
|
|
|
|
// CleanQuery converts a filename like "Inception.2010.1080p.BluRay.x264.mkv"
|
|
// into a TMDb-friendly title plus an optional year hint.
|
|
func CleanQuery(raw string) (title string, year int) {
|
|
name := strings.TrimSuffix(filepath.Base(raw), filepath.Ext(raw))
|
|
lower := strings.ToLower(name)
|
|
|
|
// 1. Year first — bracketed years (1999) must survive the next step.
|
|
if m := yearPattern.FindStringSubmatch(lower); len(m) >= 2 {
|
|
if v, err := strconv.Atoi(m[1]); err == nil {
|
|
year = v
|
|
lower = strings.ReplaceAll(lower, m[1], " ")
|
|
}
|
|
}
|
|
|
|
// 2. Drop everything inside brackets.
|
|
lower = bracketedTag.ReplaceAllString(lower, " ")
|
|
|
|
// 3. Drop episode markers (S01E02 / 1x02 / EP05 / 第03集).
|
|
lower = patSEnE.ReplaceAllString(lower, " ")
|
|
lower = patNxE.ReplaceAllString(lower, " ")
|
|
lower = patEP.ReplaceAllString(lower, " ")
|
|
lower = patCN.ReplaceAllString(lower, " ")
|
|
|
|
for _, t := range noiseTokens {
|
|
lower = strings.ReplaceAll(lower, t, " ")
|
|
}
|
|
for _, sep := range []string{".", "_", "-", "[", "]", "(", ")"} {
|
|
lower = strings.ReplaceAll(lower, sep, " ")
|
|
}
|
|
fields := strings.Fields(lower)
|
|
title = strings.Join(fields, " ")
|
|
return strings.TrimSpace(title), year
|
|
}
|
|
|
|
// EnrichOne runs the provider chain for a single media row. The library's
|
|
// type decides which provider goes first; a fallback runs when the primary
|
|
// returns nothing.
|
|
func (s *ScraperService) EnrichOne(ctx context.Context, m *model.Media) error {
|
|
lib, err := s.repo.Library.FindByID(ctx, m.LibraryID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
query := m.Title
|
|
if query == "" {
|
|
query, _ = CleanQuery(m.Path)
|
|
} else {
|
|
query, _ = CleanQuery(query)
|
|
}
|
|
year := m.Year
|
|
if year == 0 {
|
|
_, year = CleanQuery(filepath.Base(m.Path))
|
|
}
|
|
|
|
match := s.lookup(ctx, lib, query, year)
|
|
if match == nil {
|
|
// Mark explicitly so we don't retry forever.
|
|
_ = s.repo.DB.Model(&model.Media{}).Where("id = ?", m.ID).
|
|
Update("scrape_status", "no_match").Error
|
|
return nil
|
|
}
|
|
|
|
updates := map[string]any{
|
|
"title": match.Title,
|
|
"overview": match.Overview,
|
|
"poster_url": match.PosterURL,
|
|
"backdrop_url": match.BackdropURL,
|
|
"rating": match.Rating,
|
|
"year": match.Year,
|
|
"scrape_status": "matched",
|
|
}
|
|
if match.TMDbID > 0 {
|
|
updates["tmdb_id"] = match.TMDbID
|
|
}
|
|
if match.BangumiID > 0 {
|
|
updates["bangumi_id"] = match.BangumiID
|
|
}
|
|
if err := s.repo.DB.Model(&model.Media{}).Where("id = ?", m.ID).
|
|
Updates(updates).Error; err != nil {
|
|
return err
|
|
}
|
|
s.hub.Publish("scrape", map[string]any{
|
|
"media_id": m.ID,
|
|
"title": match.Title,
|
|
"tmdb_id": match.TMDbID,
|
|
"bangumi_id": match.BangumiID,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// lookup runs the provider chain. When the library is missing we fall
|
|
// back to TMDb only.
|
|
func (s *ScraperService) lookup(ctx context.Context, lib *model.Library, query string, year int) *Match {
|
|
kind := ""
|
|
if lib != nil {
|
|
kind = lib.Type
|
|
}
|
|
if kind == "anime" && s.bangumi != nil {
|
|
if m, err := s.bangumi.Search(ctx, query); err == nil && m != nil {
|
|
return m
|
|
}
|
|
s.log.Debug("bangumi miss, falling back to tmdb", zap.String("query", query))
|
|
}
|
|
if s.tmdb != nil && s.tmdb.Enabled() {
|
|
if m, err := s.tmdb.SearchMovie(ctx, query, year); err == nil && m != nil {
|
|
return m
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EnrichLibrary runs the provider chain for every "pending" media in a
|
|
// library. It throttles to 4 RPS and publishes a summary event when done.
|
|
func (s *ScraperService) EnrichLibrary(ctx context.Context, libraryID string) (int, error) {
|
|
var rows []model.Media
|
|
q := s.repo.DB.Where("scrape_status = ?", "pending")
|
|
if libraryID != "" {
|
|
q = q.Where("library_id = ?", libraryID)
|
|
}
|
|
if err := q.Find(&rows).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
matched := 0
|
|
for i := range rows {
|
|
select {
|
|
case <-ctx.Done():
|
|
return matched, ctx.Err()
|
|
default:
|
|
}
|
|
if err := s.EnrichOne(ctx, &rows[i]); err != nil {
|
|
s.log.Warn("enrich failed", zap.String("media", rows[i].ID), zap.Error(err))
|
|
continue
|
|
}
|
|
matched++
|
|
time.Sleep(250 * time.Millisecond) // ~4 RPS
|
|
}
|
|
s.hub.Publish("scrape", map[string]any{
|
|
"library_id": libraryID,
|
|
"finished": true,
|
|
"matched": matched,
|
|
})
|
|
return matched, nil
|
|
}
|
|
|
|
// AnyEnabled reports whether at least one provider can run.
|
|
func (s *ScraperService) AnyEnabled() bool {
|
|
if s.tmdb != nil && s.tmdb.Enabled() {
|
|
return true
|
|
}
|
|
if s.bangumi != nil && s.bangumi.Enabled() {
|
|
return true
|
|
}
|
|
return false
|
|
}
|