guessit-go/guessit.go

117 lines
2.1 KiB
Go
Raw Permalink 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:47:42 +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
}
2023-05-30 07:47:42 +00:00
func (u *Match) UnmarshalJSON(data []byte) error {
type Alias Match
aux := &struct {
AudioCodec interface{} `json:"audio_codec,omitempty"`
*Alias
}{
Alias: (*Alias)(u),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
switch ac := aux.AudioCodec.(type) {
case string:
u.AudioCodec = append(u.AudioCodec, ac)
case []string:
u.AudioCodec = ac
}
return nil
}
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
}