guessit-go/guessit.go

94 lines
1.7 KiB
Go
Raw Normal View History

2021-11-19 13:22:44 +00:00
package guessit
import (
"encoding/json"
2021-11-19 13:22:44 +00:00
"fmt"
"log"
"os/exec"
2021-11-19 13:22:44 +00:00
)
type GuessitConfig struct {
Python string
Git bool
Pip bool
}
type Guessit struct {
GuessitConfig
}
2021-11-19 13:22:44 +00:00
type Match struct {
// Main
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
ReleaseGroup string `json:"release_group,omitempty"`
StreamingService string `json:"streaming_service,omitempty"`
2021-11-19 13:22:44 +00:00
// Episode
Season int64 `json:"season,omitempty"`
Episode int64 `json:"episode,omitempty"`
Version int64 `json:"version,omitempty"`
2021-11-19 13:22:44 +00:00
// Video
ScreenSize string `json:"screen_size,omitempty"`
Container string `json:"container,omitempty"`
2021-11-19 13:22:44 +00:00
// Audio
2023-05-30 07:20:14 +00:00
AudioChannels string `json:"audio_channels,omitempty"`
AudioCodec string `json:"audio_codec,omitempty"`
2021-11-19 13:22:44 +00:00
// Other
Other []string `json:"other,omitempty"`
2021-11-19 13:22:44 +00:00
}
func (g GuessitConfig) PipInstall() (err error) {
args := []string{"-m", "pip", "install", "guessit"}
cmd := exec.Command(g.Python, args...)
cmdout, err := cmd.Output()
2021-11-19 13:22:44 +00:00
if err != nil {
return
2021-11-19 13:22:44 +00:00
}
fmt.Println(string(cmdout))
2021-11-21 01:05:35 +00:00
return nil
2021-11-19 13:22:44 +00:00
}
func New(conf GuessitConfig) (Guessit, error) {
if conf.Python == "" {
conf.Python = "python3"
2021-11-19 13:22:44 +00:00
}
pyPath, err := exec.LookPath(conf.Python)
if err != nil {
log.Fatal(err)
2021-11-19 13:22:44 +00:00
}
conf.Python = pyPath
if conf.Pip {
err = conf.PipInstall()
if err != nil {
return Guessit{}, err
2021-11-19 13:22:44 +00:00
}
}
return Guessit{conf}, nil
}
2021-11-21 16:38:52 +00:00
func (g Guessit) Guessit(s string, options ...string) (out Match, err error) {
args := []string{"-m", "guessit", s}
args = append(args, options...)
args = append(args, "--json")
2021-11-19 13:22:44 +00:00
cmd := exec.Command(g.Python, args...)
cmdout, err := cmd.Output()
2021-11-19 13:22:44 +00:00
if err != nil {
return
}
err = json.Unmarshal(cmdout, &out)
2021-11-19 13:22:44 +00:00
return
}