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.
127 lines
3.3 KiB
Go
127 lines
3.3 KiB
Go
// Package service — Bangumi metadata provider.
|
|
//
|
|
// Bangumi (https://bgm.tv) is the Chinese anime / manga / game database
|
|
// most users in mainland China prefer. Its public REST API is documented
|
|
// at https://bangumi.github.io/api/.
|
|
//
|
|
// We implement the minimal subset needed to enrich anime libraries:
|
|
//
|
|
// GET /search/subject/{keywords}?type=2&responseGroup=small
|
|
// GET /v0/subjects/{id} (cover)
|
|
//
|
|
// The provider gracefully no-ops when bangumi_access_token is empty.
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/config"
|
|
)
|
|
|
|
// BangumiProvider talks to https://api.bgm.tv.
|
|
type BangumiProvider struct {
|
|
cfg *config.Config
|
|
log *zap.Logger
|
|
client *http.Client
|
|
base string
|
|
}
|
|
|
|
// NewBangumiProvider is the constructor.
|
|
func NewBangumiProvider(cfg *config.Config, log *zap.Logger) *BangumiProvider {
|
|
return &BangumiProvider{
|
|
cfg: cfg,
|
|
log: log,
|
|
base: "https://api.bgm.tv",
|
|
client: &http.Client{Timeout: 15 * time.Second},
|
|
}
|
|
}
|
|
|
|
// Enabled reports whether a token is configured. Bangumi works without
|
|
// auth for read-only endpoints, but configuring a token raises the rate
|
|
// limit so we treat presence as a soft "enabled" flag.
|
|
func (b *BangumiProvider) Enabled() bool {
|
|
// Bangumi search works without auth — keep the provider enabled
|
|
// unconditionally, but pass the token when we have one.
|
|
return true
|
|
}
|
|
|
|
// Search runs a Bangumi keyword search and returns the top match. Type 2
|
|
// = anime; pass 1 (book) / 6 (real) when extending later.
|
|
func (b *BangumiProvider) Search(ctx context.Context, query string) (*Match, error) {
|
|
if query == "" {
|
|
return nil, nil
|
|
}
|
|
u := fmt.Sprintf("%s/search/subject/%s?type=2&responseGroup=small",
|
|
b.base, url.PathEscape(query))
|
|
|
|
type subject struct {
|
|
ID int `json:"id"`
|
|
Name string `json:"name"`
|
|
NameCN string `json:"name_cn"`
|
|
Summary string `json:"summary"`
|
|
Air string `json:"air_date"`
|
|
Rating struct {
|
|
Score float32 `json:"score"`
|
|
} `json:"rating"`
|
|
Images struct {
|
|
Large string `json:"large"`
|
|
} `json:"images"`
|
|
}
|
|
type page struct {
|
|
Results int `json:"results"`
|
|
List []subject `json:"list"`
|
|
}
|
|
|
|
var p page
|
|
if err := b.getJSON(ctx, u, &p); err != nil {
|
|
return nil, err
|
|
}
|
|
if p.Results == 0 || len(p.List) == 0 {
|
|
return nil, nil
|
|
}
|
|
r := p.List[0]
|
|
title := r.NameCN
|
|
if title == "" {
|
|
title = r.Name
|
|
}
|
|
m := &Match{
|
|
BangumiID: r.ID,
|
|
Title: title,
|
|
Overview: r.Summary,
|
|
PosterURL: r.Images.Large,
|
|
Rating: r.Rating.Score,
|
|
}
|
|
if len(r.Air) >= 4 {
|
|
fmt.Sscanf(r.Air[:4], "%d", &m.Year)
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func (b *BangumiProvider) getJSON(ctx context.Context, u string, out any) error {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("User-Agent", "MediaStationGo/0.1 (https://github.com/ShukeBta/MediaStationGo)")
|
|
if t := strings.TrimSpace(b.cfg.Secrets.BangumiToken); t != "" {
|
|
req.Header.Set("Authorization", "Bearer "+t)
|
|
}
|
|
resp, err := b.client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode >= 400 {
|
|
return fmt.Errorf("bangumi %s: %d", u, resp.StatusCode)
|
|
}
|
|
return json.NewDecoder(resp.Body).Decode(out)
|
|
}
|