mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-02 04:16:38 +08:00
181 lines
4.7 KiB
Go
181 lines
4.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/model"
|
|
)
|
|
|
|
type SeriesCard struct {
|
|
Key string `json:"key"`
|
|
Rep model.Media `json:"rep"`
|
|
LinkMedia model.Media `json:"linkMedia"`
|
|
Count int `json:"count"`
|
|
}
|
|
|
|
func (s *MediaService) ListLibrarySeriesCards(ctx context.Context, libraryID string, visibility MediaVisibility) ([]SeriesCard, int64, error) {
|
|
rows, _, err := s.listAllMediaVisible(ctx, libraryID, visibility)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
cards := groupMediaSeriesCards(rows)
|
|
return cards, int64(len(cards)), nil
|
|
}
|
|
|
|
func (s *MediaService) ListLibrarySeriesEpisodes(ctx context.Context, libraryID, key string, visibility MediaVisibility) ([]model.Media, error) {
|
|
rows, _, err := s.listAllMediaVisible(ctx, libraryID, visibility)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]model.Media, 0)
|
|
for _, row := range rows {
|
|
if mediaSeriesKey(row) == key {
|
|
out = append(out, row)
|
|
}
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool {
|
|
if out[i].SeasonNum != out[j].SeasonNum {
|
|
return out[i].SeasonNum < out[j].SeasonNum
|
|
}
|
|
if out[i].EpisodeNum != out[j].EpisodeNum {
|
|
return out[i].EpisodeNum < out[j].EpisodeNum
|
|
}
|
|
return out[i].CreatedAt.Before(out[j].CreatedAt)
|
|
})
|
|
return out, nil
|
|
}
|
|
|
|
func (s *MediaService) listAllMediaVisible(ctx context.Context, libraryID string, visibility MediaVisibility) ([]model.Media, int64, error) {
|
|
const pageSize = 2000
|
|
var all []model.Media
|
|
var total int64
|
|
for page := 1; ; page++ {
|
|
rows, n, err := s.ListMediaVisible(ctx, libraryID, page, pageSize, visibility)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if page == 1 {
|
|
total = n
|
|
all = make([]model.Media, 0, minInt64(n, pageSize))
|
|
}
|
|
all = append(all, rows...)
|
|
if int64(len(all)) >= n || len(rows) < pageSize {
|
|
break
|
|
}
|
|
}
|
|
return all, total, nil
|
|
}
|
|
|
|
func groupMediaSeriesCards(items []model.Media) []SeriesCard {
|
|
if len(items) == 0 {
|
|
return nil
|
|
}
|
|
cards := make([]SeriesCard, 0)
|
|
byKey := make(map[string]int, len(items))
|
|
for _, item := range items {
|
|
key := mediaSeriesKey(item)
|
|
if key == "" {
|
|
continue
|
|
}
|
|
if idx, ok := byKey[key]; ok {
|
|
card := &cards[idx]
|
|
card.Count++
|
|
if betterSeriesLinkMedia(item, card.LinkMedia) {
|
|
card.LinkMedia = item
|
|
}
|
|
currentArtwork := seriesArtworkScore(item)
|
|
representativeArtwork := seriesArtworkScore(card.Rep)
|
|
if currentArtwork > representativeArtwork {
|
|
card.Rep = item
|
|
} else if currentArtwork == representativeArtwork {
|
|
cur := item.SeasonNum*10000 + item.EpisodeNum
|
|
rep := card.Rep.SeasonNum*10000 + card.Rep.EpisodeNum
|
|
if cur > 0 && (rep == 0 || cur < rep) {
|
|
card.Rep = item
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
byKey[key] = len(cards)
|
|
cards = append(cards, SeriesCard{Key: key, Rep: item, LinkMedia: item, Count: 1})
|
|
}
|
|
return cards
|
|
}
|
|
|
|
func betterSeriesLinkMedia(candidate, current model.Media) bool {
|
|
candidateScore := librarySpecificityScore(candidate)
|
|
currentScore := librarySpecificityScore(current)
|
|
if candidateScore != currentScore {
|
|
return candidateScore > currentScore
|
|
}
|
|
return seriesArtworkScore(candidate) > seriesArtworkScore(current)
|
|
}
|
|
|
|
func librarySpecificityScore(media model.Media) int {
|
|
rawPath := strings.TrimSpace(firstNonEmpty(media.DisplayLibraryPath, media.LibraryPath))
|
|
if rawPath == "" {
|
|
return 0
|
|
}
|
|
normalized := strings.TrimRight(strings.ReplaceAll(rawPath, "\\", "/"), "/")
|
|
lower := strings.ToLower(normalized)
|
|
if strings.HasPrefix(lower, "cloud://") {
|
|
rest := normalized[len("cloud://"):]
|
|
slash := strings.Index(rest, "/")
|
|
if slash < 0 || slash == len(rest)-1 {
|
|
return 0
|
|
}
|
|
return 100 + len(nonEmptySlashParts(rest[slash+1:]))
|
|
}
|
|
return 200 + len(nonEmptySlashParts(normalized))
|
|
}
|
|
|
|
func nonEmptySlashParts(value string) []string {
|
|
parts := strings.Split(value, "/")
|
|
out := parts[:0]
|
|
for _, part := range parts {
|
|
if strings.TrimSpace(part) != "" {
|
|
out = append(out, part)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
var (
|
|
posterArtworkRE = regexp.MustCompile(`(poster|folder|cover|movie|show|pl)(?:[._-]|\.[a-z0-9]+$|$)`)
|
|
badArtworkRE = regexp.MustCompile(`(actor|actress|cast|avatar|sample|screenshot|screen|still|scene|fanart|backdrop|background|landscape|banner|logo|disc)`)
|
|
)
|
|
|
|
func seriesArtworkScore(media model.Media) int {
|
|
poster := strings.ToLower(media.PosterURL)
|
|
backdrop := strings.ToLower(media.BackdropURL)
|
|
if poster == "" {
|
|
if backdrop != "" {
|
|
return 5
|
|
}
|
|
return 0
|
|
}
|
|
if posterArtworkRE.MatchString(poster) {
|
|
return 40
|
|
}
|
|
if badArtworkRE.MatchString(poster) {
|
|
return 10
|
|
}
|
|
if strings.Contains(poster, "thumb") {
|
|
return 20
|
|
}
|
|
return 30
|
|
}
|
|
|
|
func minInt64(a int64, b int) int {
|
|
if a <= 0 {
|
|
return 0
|
|
}
|
|
if a > int64(b) {
|
|
return b
|
|
}
|
|
return int(a)
|
|
}
|