Skip to content

Commit

Permalink
feature: HDR type (#5)
Browse files Browse the repository at this point in the history
* feature: hdr as slice to see which versions of hdr

* refactor: backwards compatible

* fix: test

* remove debug app
  • Loading branch information
ProfChaos authored May 8, 2023
1 parent 6b2abb4 commit 25fb25f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/ProfChaos/torrent-name-parser

go 1.20

require github.com/stretchr/testify v1.8.2
require (
github.com/stretchr/testify v1.8.2
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o=
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
24 changes: 21 additions & 3 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package torrentparser

import (
"regexp"

"golang.org/x/exp/slices"
)

var (
Expand All @@ -23,7 +25,7 @@ func init() {
hardcodedGeneral = regexp.MustCompile(`(?i)\bHC|HARDCODED\b`)
regionGeneral = regexp.MustCompile(`(?i)dvd(R[0-9])`)
containerGeneral = regexp.MustCompile(`(?i)\.(MKV|AVI|MP4)$`)
hdrGeneral = regexp.MustCompile("(?i)hdr")
hdrGeneral = regexp.MustCompile("(?i)hdr(?:10)?|dv")
repackGeneral = regexp.MustCompile("(?i)repack|rerip")
extendedGeneral = regexp.MustCompile("(?i)extended")
properGeneral = regexp.MustCompile("(?i)proper")
Expand All @@ -50,8 +52,24 @@ func (p *parser) GetContainer() string {
return p.FindString("container", containerGeneral, FindStringOptions{})
}

func (p *parser) GetHdr() bool {
return p.FindBoolean("hdr", hdrGeneral)
func (p *parser) Hdr() ([]string, bool) {
isHDR := false
return p.FindStrings("hdr", hdrGeneral, FindStringsOptions{
Handler: func(s []string) []string {
if len(s) > 0 {
isHDR = true
}
pos := slices.Index(s, "HDR")
if pos != -1 {
if len(s) == 1 {
return nil
}

return slices.Delete(s, pos, pos+1)
}
return s
},
}), isHDR
}

func (p *parser) Repack() bool {
Expand Down
17 changes: 14 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Torrent struct {
Episode int `json:"episode"`
Languages []string `json:"languages"`
Hdr bool `json:"hdr"`
HdrType []string `json:"hdrType"`
ColorDepth string `json:"colorDepth"`
Date string `json:"date"`
}
Expand Down Expand Up @@ -101,6 +102,15 @@ func ParseName(name string) (Torrent, error) {
return p.Parse()
}

func DebugParser(name string) {
p := &parser{Name: name, MatchedIndicies: map[string]index{}, LowestIndex: len(name)}
p.Parse()

for _, index := range p.MatchedIndicies {
fmt.Printf("%s\033[34m%s\033[0m%s | \033[34m%s\033[0m\n", name[:index.Start], name[index.Start:index.End], name[index.End:], index.Name)
}
}

func (p *parser) Parse() (Torrent, error) {
torrent := Torrent{
Season: -1,
Expand All @@ -127,7 +137,7 @@ func (p *parser) Parse() (Torrent, error) {
}
torrent.Episode = p.GetEpisode()
torrent.Unrated = p.GetUnrated()
torrent.Hdr = p.GetHdr()
torrent.HdrType, torrent.Hdr = p.Hdr()
torrent.ColorDepth = p.GetColorDepth()
torrent.Languages = p.GetLanguages()

Expand Down Expand Up @@ -180,6 +190,7 @@ func (p *parser) FindBoolean(attr string, rx *regexp.Regexp) bool {
if p.MatchedRange(loc[0], loc[1]) {
return false
}
p.AddMatchedIndex(attr, loc)
p.setLowestIndex(loc[0])
return true
}
Expand Down Expand Up @@ -357,8 +368,8 @@ func (p *parser) shouldAllReturnNil(name string, locs [][]int) ([]string, bool)
}

matches := make([]string, 0)
for _, loc := range locs {
match, returnNil := p.shouldReturnNil(name, loc)
for i, loc := range locs {
match, returnNil := p.shouldReturnNil(fmt.Sprintf("%s%d", name, i), loc)
if returnNil {
return nil, true
}
Expand Down
20 changes: 19 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ func TestParser_Parse(t *testing.T) {
Group: "NAHOM",
Source: "bdremux",
Languages: []string{"ita", "eng"},
HdrType: []string{"DV"},
Hdr: true,
Season: -1,
},
Expand Down Expand Up @@ -403,6 +404,23 @@ func TestParser_Parse(t *testing.T) {
Container: "mp4",
},
},
{
name: "Ant-Man.and.the.Wasp.Quantumania.2023.2160p.MA.WEB-DL.DDP5.1.Atmos.DV.HDR10.H.265-CMRG.mkv",
want: Torrent{
Title: "Ant-Man and the Wasp Quantumania",
Year: 2023,
Resolution: "4k",
Container: "mkv",
Source: "web-dl",
Codec: "h265",
Audio: "DDP5.1 Atmos",
HdrType: []string{"DV", "HDR10"},
Hdr: true,
Season: -1,
Group: "CMRG",
ContentType: Movie,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -446,7 +464,7 @@ func TestContentType(t *testing.T) {
func TestTorrentScanAndValue(t *testing.T) {
var torrent Torrent

jsonStr := `{"title":"Pirates of the Caribbean Dead Mans Chest","alternativeTitle":"","contentType":0,"year":0,"resolution":"4k","extended":false,"unrated":false,"proper":false,"repack":false,"convert":false,"hardcoded":false,"retail":false,"remastered":false,"region":"","container":"mkv","source":"web-dl","codec":"hevc","audio":"dts-hd","group":"WATCHER","season":-1,"seasons":null,"episode":0,"languages":null,"hdr":true,"colorDepth":"","date":""}`
jsonStr := `{"title":"Pirates of the Caribbean Dead Mans Chest","alternativeTitle":"","contentType":0,"year":0,"resolution":"4k","extended":false,"unrated":false,"proper":false,"repack":false,"convert":false,"hardcoded":false,"retail":false,"remastered":false,"region":"","container":"mkv","source":"web-dl","codec":"hevc","audio":"dts-hd","group":"WATCHER","season":-1,"seasons":null,"episode":0,"languages":null,"hdr":true,"hdrType":null,"colorDepth":"","date":""}`

err := torrent.Scan(jsonStr)
if err != nil {
Expand Down

0 comments on commit 25fb25f

Please sign in to comment.