Skip to content

Commit

Permalink
Scanner: bette rerror handling for path parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthi-chaud committed Mar 28, 2024
1 parent d4884a5 commit 8c9ccc9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 33 deletions.
6 changes: 6 additions & 0 deletions scanner/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/glenn-brown/golang-pkg-pcre v0.0.0-20120522223659-48bb82a8b8ce // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.19.0
github.com/goccy/go-json v0.10.2 // indirect
github.com/kpango/fastime v1.1.9 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
Expand Down
12 changes: 12 additions & 0 deletions scanner/go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/glenn-brown/golang-pkg-pcre v0.0.0-20120522223659-48bb82a8b8ce h1:MS/JOOAHf4U2iKl8+1+vzUcG9t9ru1hnZJ9NEBDvMnY=
github.com/glenn-brown/golang-pkg-pcre v0.0.0-20120522223659-48bb82a8b8ce/go.mod h1:5385NDJ+Gt5loLrAlc8Rr5lKA1L5BE5O94jfdwEX9kg=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4=
github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/kpango/fastime v1.1.9 h1:xVQHcqyPt5M69DyFH7g1EPRns1YQNap9d5eLhl/Jy84=
Expand All @@ -20,6 +24,14 @@ github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8t
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/validator.v9 v9.31.0 h1:bmXmP2RSNtFES+bn4uYuHT7iJFJv7Vj+an+ZQdDaD1M=
gopkg.in/go-playground/validator.v9 v9.31.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
Expand Down
61 changes: 28 additions & 33 deletions scanner/pkg/parser/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,50 @@ package parser

import (
"errors"
"github.com/Arthi-chaud/Blee/scanner/pkg/config"
"github.com/Arthi-chaud/Blee/scanner/pkg/models"
validator "github.com/go-playground/validator/v10"
"regexp"
"strconv"
"strings"
"time"

"github.com/Arthi-chaud/Blee/scanner/pkg/config"
"github.com/Arthi-chaud/Blee/scanner/pkg/models"
)

// Metadata Extracted from the path of a file
type PackageMetadataFromPath struct {
// Name of the package
// Required
name string
name string `validate:"required"`
// Name of the artist of the package
// Optional
artist_name string
// Release Year of the package
// Required
release_year time.Time
}

// Metadata Extracted from the path of an Extra file
type ExtraMetadataFromPath struct {
// Name of the extra
// Required
name string
name string `validate:"required"`
// Name of the artist of the extra
// Required
artist_name string
artist_name string `validate:"required"`
// Index of the disc of the extra
// Optional
disc_index int
disc_index int `validate:"gte=0"`
// Index of the track of the extra
// Optional
track_index int
track_index int `validate:"gte=0"`
// Types of the extra
// Cannot be empty
types [](models.ExtraType)
package_ PackageMetadataFromPath
types [](models.ExtraType) `validate:"min=1"`
package_ PackageMetadataFromPath `validate:"required,dive,required"`
}

// Metadata Extracted from the path of a Movie file
type MovieMetadataFromPath struct {
// Name of the movie
// Required
name string
name string `validate:"required"`
// Name of the artist of the movie
// Required
artist_name string
artist_name string `validate:"required"`
// Type of movie
type_ models.MovieType
package_ PackageMetadataFromPath
type_ models.MovieType `validate:"required"`
package_ PackageMetadataFromPath `validate:"required,dive,required"`
}

type PathMetadataParsingResult struct {
Expand All @@ -69,20 +60,22 @@ func ParseMetadataFromPath(filePath string, userConfig *config.UserConfiguration
if len(matches) == 0 {
continue
}
return PathMetadataParsingResult{extra: parseExtraMetadataFromMatches(matches, regex)}, nil
extra, err := parseExtraMetadataFromMatches(matches, regex)
return PathMetadataParsingResult{extra: extra }, err
}
for _, movieRegex := range userConfig.Regexes.Movie {
regex := regexp.MustCompile(movieRegex)
matches := regex.FindStringSubmatch(filePath)
if len(matches) == 0 {
continue
}
return PathMetadataParsingResult{movie: parseMovieMetadataFromMatches(matches, regex)}, nil
movie, err := parseMovieMetadataFromMatches(matches, regex)
return PathMetadataParsingResult{movie: movie}, err
}
return PathMetadataParsingResult{}, errors.New("No match")
}

func parseMovieMetadataFromMatches(matches []string, regex *regexp.Regexp) *MovieMetadataFromPath {
func parseMovieMetadataFromMatches(matches []string, regex *regexp.Regexp) (*MovieMetadataFromPath, error) {
res := MovieMetadataFromPath{}

if artist_index := regex.SubexpIndex("Artist"); artist_index != -1 {
Expand All @@ -103,7 +96,9 @@ func parseMovieMetadataFromMatches(matches []string, regex *regexp.Regexp) *Movi
}
res.type_ = parseMovieTypeFromName(res.name)
parseYearFromRegex(matches, regex, &res.package_)
return &res
validate := validator.New(validator.WithRequiredStructEnabled())
err := validate.Struct(res)
return &res, err
}

func parseYearFromRegex(matches []string, regex *regexp.Regexp, p *PackageMetadataFromPath) {
Expand All @@ -129,7 +124,7 @@ func parseMovieTypeFromName(movieName string) models.MovieType {
return models.Documentary
}

func parseExtraMetadataFromMatches(matches []string, regex *regexp.Regexp) *ExtraMetadataFromPath {
func parseExtraMetadataFromMatches(matches []string, regex *regexp.Regexp) (*ExtraMetadataFromPath, error) {
res := ExtraMetadataFromPath{}

if artist_index := regex.SubexpIndex("Artist"); artist_index != -1 {
Expand Down Expand Up @@ -161,12 +156,12 @@ func parseExtraMetadataFromMatches(matches []string, regex *regexp.Regexp) *Extr
res.types = append(res.types, models.Other)
}
parseYearFromRegex(matches, regex, &res.package_)
res.package_.name = matches[regex.SubexpIndex("Package")]
parsed_date, err := time.Parse(time.RFC3339, matches[regex.SubexpIndex("Package")])
if err == nil {
res.package_.release_year = parsed_date
if package_index := regex.SubexpIndex("Package"); package_index != -1 {
res.package_.name = matches[package_index]
}
return &res
validate := validator.New(validator.WithRequiredStructEnabled())
err := validate.Struct(res)
return &res, err
}

func parseExtraTypeFromPlexRegexGroup(group string) models.ExtraType {
Expand Down
17 changes: 17 additions & 0 deletions scanner/pkg/parser/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,20 @@ func TestExtraParsingWithPackageFolderWithoutArtistFolderWithIndexesWithoutTypes
assert.Equal(t, res.extra.package_.name, "Package")
assert.Equal(t, res.extra.package_.release_year.Year(), 2000)
}

func TestExtraWithoutDisc(t *testing.T) {
config := getTestConfig()
path := "/videos/Artist - Package (2000)/02 Extra1.mkv"
res, err := ParseMetadataFromPath(path, config)

assert.Nil(t, err)
assert.Nil(t, res.movie)
assert.Equal(t, res.extra.artist_name, "Artist")
assert.Equal(t, res.extra.disc_index, 0)
assert.Equal(t, res.extra.track_index, 2)
assert.Equal(t, res.extra.name, "Extra1")
assert.Equal(t, res.extra.types, []models.ExtraType{models.Other})
assert.Equal(t, res.extra.package_.artist_name, "Artist")
assert.Equal(t, res.extra.package_.name, "Package")
assert.Equal(t, res.extra.package_.release_year.Year(), 2000)
}

0 comments on commit 8c9ccc9

Please sign in to comment.