AnalyzeFile implemented

This commit is contained in:
Timon Ringwald 2022-12-07 19:57:40 +01:00
parent c840c68b76
commit 9ef0c1a7d1
5 changed files with 68 additions and 7 deletions

58
file_parse.go Normal file
View File

@ -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
}

3
go.mod
View File

@ -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

6
go.sum
View File

@ -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=

View File

@ -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,

View File

@ -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