Compare commits

..

No commits in common. "08a66a905f44d33321c065b7be0ca110a2bdc361" and "51951bdda7a27c6aeae4ee62d69259bffa6dcaca" have entirely different histories.

5 changed files with 120 additions and 86 deletions

View File

@ -1,9 +0,0 @@
# guessit-go
```
python3 -m pip install -U nuitka
git clone https://github.com/guessit-io/guessit.git
nuitka3 --output-filename=guessit /path/to/guessit
```

11
go.mod
View File

@ -1,11 +1,10 @@
module git.stein-ivar.net/steino/guessit-go module git.stein-ivar.net/steino/guessit-go
go 1.20 go 1.17
require github.com/mitchellh/mapstructure v1.4.2
require ( require (
git.stein-ivar.net/steino/cpy3 v0.0.0-20230524083048-7d65f97b9fa8 // indirect git.stein-ivar.net/steino/cpy3 v3.8.0+incompatible
github.com/justincormack/go-memfd v0.0.0-20170219213707-6e4af0518993 // indirect github.com/mitchellh/mapstructure v1.4.2
github.com/stretchr/testify v1.7.0 // indirect
) )
require github.com/stretchr/testify v1.7.0 // indirect

4
go.sum
View File

@ -1,11 +1,7 @@
git.stein-ivar.net/steino/cpy3 v0.0.0-20230524083048-7d65f97b9fa8 h1:3wKFzSMTfDpqnjqr6oyPDsPrufRmC7PuVqJ6KzAdjwQ=
git.stein-ivar.net/steino/cpy3 v0.0.0-20230524083048-7d65f97b9fa8/go.mod h1:TUfV4l/zaEgFiclf97tC1i++NlQa/WYToI4VaYkNlJU=
git.stein-ivar.net/steino/cpy3 v3.8.0+incompatible h1:Cv7VXH+Av7HPDIMFWMzCD7XjR0+IG5PRKxB/5KueTCM= git.stein-ivar.net/steino/cpy3 v3.8.0+incompatible h1:Cv7VXH+Av7HPDIMFWMzCD7XjR0+IG5PRKxB/5KueTCM=
git.stein-ivar.net/steino/cpy3 v3.8.0+incompatible/go.mod h1:jDKy1+hHkC3cWb9ZpIoLDwhZTAhEqP3IVNcgvndmQEQ= git.stein-ivar.net/steino/cpy3 v3.8.0+incompatible/go.mod h1:jDKy1+hHkC3cWb9ZpIoLDwhZTAhEqP3IVNcgvndmQEQ=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/justincormack/go-memfd v0.0.0-20170219213707-6e4af0518993 h1:YnMJVKw7M5rE15UsVY7w2cnxcnArci7v1g3butq0YbI=
github.com/justincormack/go-memfd v0.0.0-20170219213707-6e4af0518993/go.mod h1:VYi8SD2j14Nh9hNT7l57A00YUx/tMxY6pPA1IGljdrg=
github.com/mitchellh/mapstructure v1.4.2 h1:6h7AQ0yhTcIsmFmnAwQls75jp2Gzs4iB8W7pjMO+rqo= github.com/mitchellh/mapstructure v1.4.2 h1:6h7AQ0yhTcIsmFmnAwQls75jp2Gzs4iB8W7pjMO+rqo=
github.com/mitchellh/mapstructure v1.4.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/mapstructure v1.4.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View File

@ -1,95 +1,148 @@
package guessit package guessit
// #cgo pkg-config: python-3.9-embed
// #include <Python.h>
// import "C"
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "os"
"os/exec" "strings"
_ "embed" python3 "git.stein-ivar.net/steino/cpy3"
"github.com/mitchellh/mapstructure"
) )
type GuessitConfig struct {
Python string
Git bool
Pip bool
}
type Guessit struct {
GuessitConfig
}
type Match struct { type Match struct {
// Main // Main
Type string `json:"type,omitempty"` Type string `mapstructure:"type,omitempty"`
Title string `json:"title,omitempty"` Title string `mapstructure:"title,omitempty"`
ReleaseGroup string `json:"release_group,omitempty"` ReleaseGroup string `mapstructure:"release_group,omitempty"`
StreamingService string `json:"streaming_service,omitempty"` StreamingService string `mapstructure:"streaming_service,omitempty"`
// Episode // Episode
Season int64 `json:"season,omitempty"` Season int64 `mapstructure:"season,omitempty"`
Episode int64 `json:"episode,omitempty"` Episode int64 `mapstructure:"episode,omitempty"`
Version int64 `json:"version,omitempty"` Version int64 `mapstructure:"version,omitempty"`
// Video // Video
ScreenSize string `json:"screen_size,omitempty"` ScreenSize string `mapstructure:"screen_size,omitempty"`
Container string `json:"container,omitempty"` Container string `mapstructure:"container,omitempty"`
// Audio // Audio
AudioChannels string `json:"audio_channels,omitempty"` AudioChannels string `mapstructure:"audio_channels,omitempty"`
AudioCodec []string `json:"audio_codec,omitempty"` AudioCodec []string `mapstructure:"audio_codec,omitempty"`
// Other // Other
Other []string `json:"other,omitempty"` Other []string `mapstructure:"other,omitempty"`
} }
func (g GuessitConfig) PipInstall() (err error) { type Guessit struct {
args := []string{"-m", "pip", "install", "guessit3"} fn *python3.PyObject
cmd := exec.Command(g.Python, args...)
cmdout, err := cmd.Output()
if err != nil {
return
}
fmt.Println(string(cmdout))
return nil
} }
func New(conf GuessitConfig) (Guessit, error) { func init() {
if conf.Python == "" { python3.Py_Initialize()
conf.Python = "python3" if !python3.Py_IsInitialized() {
} fmt.Println("Error initializing the python interpreter")
os.Exit(1)
pyPath, err := exec.LookPath(conf.Python)
if err != nil {
log.Fatal(err)
}
conf.Python = pyPath
if conf.Pip {
err = conf.PipInstall()
if err != nil {
return Guessit{}, err
} }
} }
return Guessit{conf}, nil func New() (*Guessit, error) {
python3.PyErr_Clear()
module := python3.PyImport_ImportModule("guessit") //ret val: new ref
if !(module != nil && python3.PyErr_Occurred() == nil) {
return nil, fmt.Errorf("failed to import module 'guessit'")
}
defer module.DecRef()
dict := python3.PyModule_GetDict(module) //ret val: Borrowed
if !(dict != nil && python3.PyErr_Occurred() == nil) {
return nil, fmt.Errorf("could not get dict for module")
}
fn := python3.PyDict_GetItemString(dict, "guessit") //retval: Borrowed
if !(fn != nil && python3.PyCallable_Check(fn)) {
return nil, fmt.Errorf("could not find function 'guessit'")
}
return &Guessit{
fn: fn,
}, nil
} }
func (g Guessit) Guessit(s string, options ...string) (out Match, err error) { func (g Guessit) Guessit(s string, options ...string) (out Match, err error) {
args := []string{"-m", "guessit", s} python3.PyErr_Clear()
args = append(args, options...)
args = append(args, "--json")
cmd := exec.Command(g.Python, args...) if len(s) == 0 {
cmdout, err := cmd.Output() err = fmt.Errorf("input string is empty")
return
}
item := python3.PyUnicode_FromString(s)
defer item.DecRef()
opts := python3.PyUnicode_FromString(strings.Join(options[:], " "))
defer opts.DecRef()
testdataPy := g.fn.CallFunctionObjArgs(item, opts) //retval: New reference
if !(testdataPy != nil && python3.PyErr_Occurred() == nil) {
return
}
defer testdataPy.DecRef()
size := python3.PyDict_Size(testdataPy)
keys := python3.PyDict_Keys(testdataPy)
defer keys.DecRef()
vals := python3.PyDict_Values(testdataPy)
defer vals.DecRef()
tmpmap := make(map[string]interface{})
for i := 0; i < size; i++ {
kitem := python3.PyList_GetItem(keys, i)
key := python3.PyUnicode_AsUTF8(kitem)
kitem = nil
val := python3.PyList_GetItem(vals, i)
switch {
case python3.PyLong_Check(val):
tmpmap[key] = python3.PyLong_AsLongLong(val)
case python3.PyUnicode_Check(val):
tmpmap[key] = python3.PyUnicode_AsUTF8(val)
case python3.PyList_Check(val):
var tmp []string
for i := 0; i < python3.PyList_Size(val); i++ {
item := python3.PyList_GetItem(val, i)
v := python3.PyUnicode_AsUTF8(item)
tmp = append(tmp, v)
}
tmpmap[key] = tmp
default:
}
}
if _, ok := tmpmap["version"]; !ok {
tmpmap["version"] = -1
}
config := &mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: &out,
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil { if err != nil {
return return
} }
err = json.Unmarshal(cmdout, &out) err = decoder.Decode(tmpmap)
if err != nil {
return
}
return return
} }

View File

@ -2,18 +2,13 @@ package guessit
import ( import (
"fmt" "fmt"
"log"
"testing" "testing"
) )
func TestGuessit(t *testing.T) { func TestGuessit(t *testing.T) {
g, err := New(GuessitConfig{ g, err := New()
Git: false,
Pip: true,
})
if err != nil { if err != nil {
log.Fatal(err) return
} }
ss := []string{ ss := []string{
@ -27,6 +22,6 @@ func TestGuessit(t *testing.T) {
} }
for _, s := range ss { for _, s := range ss {
fmt.Println(g.Guessit(s, "--excludes=season")) fmt.Println(g.Guessit(s)) //, "--excludes=season"))
} }
} }