From 9ef0c1a7d1b4c33ed6ffc15884f7917758ef4ea8 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Wed, 7 Dec 2022 19:57:40 +0100 Subject: [PATCH] AnalyzeFile implemented --- file_parse.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++- go.sum | 6 +++-- parsers.go | 6 ++--- torrent_parsing.go | 2 +- 5 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 file_parse.go diff --git a/file_parse.go b/file_parse.go new file mode 100644 index 0000000..cda1ef4 --- /dev/null +++ b/file_parse.go @@ -0,0 +1,58 @@ +package parsers + +import ( + "context" + "os" + + "git.milar.in/nyaanime/model" + "gopkg.in/vansante/go-ffprobe.v2" +) + +func ParseFile(path string) (*model.ParsedFile, error) { + parsedFile := &model.ParsedFile{File: path} + + return parsedFile, nil +} + +// TODO cache +func AnalyzeFile(path string) (*model.ParsedFile, error) { + props := &model.ParsedFile{File: path} + + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + + data, err := ffprobe.ProbeReader(context.Background(), file) + if err != nil { + return nil, err + } + + defaultVideoLang := "" + for _, s := range data.StreamType(ffprobe.StreamVideo) { + if s.Disposition.Default > 0 { + props.Resolution = model.Resolution(s.Height) + defaultVideoLang = ParseLanguage(s.Tags.Language) + break + } + } + + for _, s := range data.StreamType(ffprobe.StreamAudio) { + if s.Tags.Language != "" { + props.Languages = append(props.Languages, ParseLanguage(s.Tags.Language)) + } else if s.Disposition.Default > 0 { + props.Languages = append(props.Languages, defaultVideoLang) + } + } + + for _, s := range data.StreamType(ffprobe.StreamSubtitle) { + if s.Tags.Language != "" { + props.Subtitles = append(props.Subtitles, ParseLanguage(s.Tags.Language)) + } else if s.Disposition.Default > 0 { + props.Subtitles = append(props.Subtitles, defaultVideoLang) + } + } + + return props, nil +} diff --git a/go.mod b/go.mod index 6738edb..f15555e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,8 @@ go 1.18 require ( git.milar.in/milarin/adverr v0.2.1 - git.milar.in/nyaanime/model v0.0.0-20220815143950-c3d8a5af20c3 + git.milar.in/nyaanime/model v0.0.0-20221008104642-466e1111ddea + gopkg.in/vansante/go-ffprobe.v2 v2.1.1 ) require git.milar.in/milarin/anilist v1.5.0 // indirect diff --git a/go.sum b/go.sum index 27fd23a..f9b5465 100644 --- a/go.sum +++ b/go.sum @@ -2,5 +2,7 @@ git.milar.in/milarin/adverr v0.2.1 h1:eyXFGC+Ui/kcNt2+NqP3HiAplwxzqeNr9DfitsUb3c git.milar.in/milarin/adverr v0.2.1/go.mod h1:wwfglcey4R3vqjNL/d8mbnvFJGzETRXzAEolIHZY32w= git.milar.in/milarin/anilist v1.5.0 h1:fSiAXY/topNk4ISEp2QtcG9HHKLJfMc8w05iqc+Paf0= git.milar.in/milarin/anilist v1.5.0/go.mod h1:8PTHXFMA45JpfRFIpcdrKwDHue8fbT/wwV1BuHFn6c0= -git.milar.in/nyaanime/model v0.0.0-20220815143950-c3d8a5af20c3 h1:GyAhYs3Nl8CuzfOeLKmWwRe/Z+pEEtnWty39HfGz6iE= -git.milar.in/nyaanime/model v0.0.0-20220815143950-c3d8a5af20c3/go.mod h1:OzhQgj0b/Hf9fg8VXYxYt8ONQOvHm8xC44TmS9kQ150= +git.milar.in/nyaanime/model v0.0.0-20221008104642-466e1111ddea h1:iBwxI3vZ+Hix/5HHB3k9f9/R0nkwR0EvVJ3o5RJEKP4= +git.milar.in/nyaanime/model v0.0.0-20221008104642-466e1111ddea/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg= +gopkg.in/vansante/go-ffprobe.v2 v2.1.1 h1:DIh5fMn+tlBvG7pXyUZdemVmLdERnf2xX6XOFF+0BBU= +gopkg.in/vansante/go-ffprobe.v2 v2.1.1/go.mod h1:qF0AlAjk7Nqzqf3y333Ly+KxN3cKF2JqA3JT5ZheUGE= diff --git a/parsers.go b/parsers.go index 2fe4c27..a525363 100644 --- a/parsers.go +++ b/parsers.go @@ -16,7 +16,7 @@ console.log(s); var Parsers = []model.Parser{ { Identity: "Erai-Raws", - TorrentParser: regexTorrentParser( + TorrentParser: RegexTorrentParser( `^\[Erai-raws\] (.*) - (.*?) \[(.*?)p\](?:\[HEVC\])?(?:\[Multiple Subtitle\])? (\[.*?\]+)?$`, TorrentParseOptions{ Name: 1, @@ -32,7 +32,7 @@ var Parsers = []model.Parser{ { Identity: "SubsPlease", - TorrentParser: regexTorrentParser( + TorrentParser: RegexTorrentParser( `^\[SubsPlease\] (.*) - (\d+?) \((.*?)\) \[.*?\].mkv$`, TorrentParseOptions{ Name: 1, @@ -47,7 +47,7 @@ var Parsers = []model.Parser{ { Identity: "PuyaSubs!", - TorrentParser: regexTorrentParser( + TorrentParser: RegexTorrentParser( `^\[PuyaSubs!\] (.*) - (\d+?) \[ESP-ENG\]\[(.*?)\]\[.*?\]\.mkv$`, TorrentParseOptions{ Name: 1, diff --git a/torrent_parsing.go b/torrent_parsing.go index 4d92db4..ffa8725 100644 --- a/torrent_parsing.go +++ b/torrent_parsing.go @@ -26,7 +26,7 @@ type TorrentParseOptions struct { DefaultResolution model.Resolution } -func regexTorrentParser(regex string, options TorrentParseOptions) model.TorrentParserFunc { +func RegexTorrentParser(regex string, options TorrentParseOptions) model.TorrentParserFunc { pattern := regexp.MustCompile(regex) // handle faulty regexes