Git Repository

Mini Video Library

Mini Video Library scannt lokale Medienordner, verwaltet Metadaten und Vorschaubilder und streamt kompatible Videos direkt per HTTP an den Browser.

Projektseite ↗
HTTPShttps://zanvex.de/git/mini-video-library.git

internal/filenameparser/parser.go

Zum Verzeichnis
package filenameparser

import (
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
)

type Result struct {
	Series       string `json:"series"`
	Season       int    `json:"season"`
	Episode      int    `json:"episode"`
	ReleaseGroup string `json:"release_group"`
	Quality      string `json:"quality"`
}

var patterns = []*regexp.Regexp{
	regexp.MustCompile(`(?i)^(.*?)\s*[._ -]+s(\d{1,2})e(\d{1,4})(?:\D|$)`),
	regexp.MustCompile(`(?i)^(.*?)\s*[._ -]+(\d{1,2})x(\d{1,4})(?:\D|$)`),
	regexp.MustCompile(`(?i)^(.*?)\s*[._ -]+(?:episode|ep)[._ -]*(\d{1,4})(?:\D|$)`),
	regexp.MustCompile(`(?i)^(.*?)\s+-\s+(\d{1,4})(?:\D|$)`),
}
var qualityPattern = regexp.MustCompile(`(?i)(2160p|4k|1080p|720p|576p|480p)`)
var groupPattern = regexp.MustCompile(`\[([^\]]+)]\s*$`)

func Parse(filename string) Result {
	name := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
	result := Result{}
	if match := groupPattern.FindStringSubmatch(name); len(match) == 2 {
		result.ReleaseGroup = strings.TrimSpace(match[1])
	}
	if quality := qualityPattern.FindString(name); quality != "" {
		result.Quality = strings.ToLower(quality)
	}
	for index, pattern := range patterns {
		match := pattern.FindStringSubmatch(name)
		if len(match) == 0 {
			continue
		}
		result.Series = clean(match[1])
		if index < 2 {
			result.Season, _ = strconv.Atoi(match[2])
			result.Episode, _ = strconv.Atoi(match[3])
		} else {
			result.Episode, _ = strconv.Atoi(match[2])
		}
		break
	}
	return result
}

func clean(value string) string {
	value = strings.NewReplacer(".", " ", "_", " ").Replace(value)
	return strings.Join(strings.Fields(strings.Trim(value, " -_[]()")), " ")
}