44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
|
package tv
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/ollien/gobbler/categorize/match"
|
||
|
)
|
||
|
|
||
|
var errNotTvShow = errors.New("no tv-like information found in show name")
|
||
|
|
||
|
// FindShowFolder finds the show that the given filename likey belongs to, among the given show folders.
|
||
|
// Returns errNotTvShow if no match can be found
|
||
|
func FindShowFolder(filename string, showFolders []string) (string, error) {
|
||
|
possibleShowName, err := extractShowName(filename)
|
||
|
if err != nil {
|
||
|
return "", errNotTvShow
|
||
|
}
|
||
|
|
||
|
return matchToFolder(possibleShowName, showFolders)
|
||
|
}
|
||
|
|
||
|
func extractShowName(filename string) (string, error) {
|
||
|
seasonString, err := extractSeasonNumberString(filename)
|
||
|
if err != nil {
|
||
|
return "", errNoSeasonInfo
|
||
|
}
|
||
|
|
||
|
seasonStartPos := strings.Index(filename, seasonString)
|
||
|
if seasonStartPos == -1 {
|
||
|
// This should never really happen; if the call to extractSeasonNumberString returns a match, we should always
|
||
|
// be able to find it in the string. However, I don't know that it deserves a panic either...
|
||
|
return "", errors.New("failed to find season information in filename")
|
||
|
}
|
||
|
|
||
|
possibleShowName := filename[:seasonStartPos]
|
||
|
|
||
|
return strings.TrimFunc(possibleShowName, isTrimmableSymbol), nil
|
||
|
}
|
||
|
|
||
|
func matchToFolder(candidate string, showFolders []string) (string, error) {
|
||
|
return match.FindBestMatch(candidate, showFolders)
|
||
|
}
|