2021-11-19 13:22:44 +00:00
|
|
|
package guessit
|
|
|
|
|
|
|
|
import (
|
2023-05-25 05:00:03 +00:00
|
|
|
"encoding/json"
|
2021-11-19 13:22:44 +00:00
|
|
|
"fmt"
|
2023-05-25 05:00:03 +00:00
|
|
|
"log"
|
|
|
|
"os/exec"
|
2021-11-19 13:22:44 +00:00
|
|
|
|
2023-05-25 05:00:03 +00:00
|
|
|
_ "embed"
|
2021-11-19 13:22:44 +00:00
|
|
|
)
|
|
|
|
|
2023-05-25 05:00:03 +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
|
2023-05-25 05:00:03 +00:00
|
|
|
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
|
2023-05-25 05:00:03 +00:00
|
|
|
Season int64 `json:"season,omitempty"`
|
|
|
|
Episode int64 `json:"episode,omitempty"`
|
|
|
|
Version int64 `json:"version,omitempty"`
|
2021-11-19 13:22:44 +00:00
|
|
|
|
|
|
|
// Video
|
2023-05-25 05:00:03 +00:00
|
|
|
ScreenSize string `json:"screen_size,omitempty"`
|
|
|
|
Container string `json:"container,omitempty"`
|
2021-11-19 13:22:44 +00:00
|
|
|
|
|
|
|
// Audio
|
2023-05-25 05:00:03 +00:00
|
|
|
AudioChannels string `json:"audio_channels,omitempty"`
|
|
|
|
AudioCodec []string `json:"audio_codec,omitempty"`
|
2021-11-19 13:22:44 +00:00
|
|
|
|
|
|
|
// Other
|
2023-05-25 05:00:03 +00:00
|
|
|
Other []string `json:"other,omitempty"`
|
2021-11-19 13:22:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 05:00:03 +00:00
|
|
|
func (g GuessitConfig) PipInstall() (err error) {
|
|
|
|
args := []string{"-m", "pip", "install", "guessit3"}
|
|
|
|
cmd := exec.Command(g.Python, args...)
|
|
|
|
cmdout, err := cmd.Output()
|
2021-11-19 13:22:44 +00:00
|
|
|
|
2023-05-25 05:00:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
2021-11-19 13:22:44 +00:00
|
|
|
}
|
2023-05-25 05:00:03 +00:00
|
|
|
fmt.Println(string(cmdout))
|
2021-11-21 01:05:35 +00:00
|
|
|
|
2023-05-25 05:00:03 +00:00
|
|
|
return nil
|
2021-11-19 13:22:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 05:00:03 +00:00
|
|
|
func New(conf GuessitConfig) (Guessit, error) {
|
|
|
|
if conf.Python == "" {
|
|
|
|
conf.Python = "python3"
|
2021-11-19 13:22:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 05:00:03 +00:00
|
|
|
pyPath, err := exec.LookPath(conf.Python)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2021-11-19 13:22:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 05:00:03 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 05:00:03 +00:00
|
|
|
return Guessit{conf}, nil
|
|
|
|
}
|
2021-11-21 16:38:52 +00:00
|
|
|
|
2023-05-25 05:00:03 +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
|
|
|
|
2023-05-25 05:00:03 +00:00
|
|
|
cmd := exec.Command(g.Python, args...)
|
|
|
|
cmdout, err := cmd.Output()
|
2021-11-19 13:22:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-25 05:00:03 +00:00
|
|
|
err = json.Unmarshal(cmdout, &out)
|
2021-11-19 13:22:44 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|