Skip to content

Commit

Permalink
Add support for yaml config file
Browse files Browse the repository at this point in the history
  • Loading branch information
nandesh-dev committed Oct 2, 2024
1 parent 7936bfb commit 5f3fc9e
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 2 deletions.
39 changes: 37 additions & 2 deletions cmd/subtle/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,54 @@
package main

import (
"fmt"
"log"
"net"

"github.com/nandesh-dev/subtle/internal/filemanager"
"github.com/nandesh-dev/subtle/internal/pb/library"
"github.com/nandesh-dev/subtle/internal/pgs"
"github.com/nandesh-dev/subtle/internal/services"
"github.com/nandesh-dev/subtle/internal/subtitle"
"github.com/nandesh-dev/subtle/internal/tesseract"
"golang.org/x/text/language"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

func main() {
client := tesseract.NewClient()
defer client.Close()
dir, err := filemanager.ReadDirectory("/media")
if err != nil {
log.Fatal(err)
}

fmt.Println(dir)

videos, warnings := dir.VideoFiles()
fmt.Println(warnings)

rawStreams, err := subtitle.ExtractRawStreams(&videos[0])
if err != nil {
log.Fatal(err)
}

subtitle, _, err := pgs.DecodeSubtitle(&rawStreams[0])
if err != nil {
log.Fatal(err)
}

tes := tesseract.NewClient()

for _, segment := range subtitle.Segments() {
for _, img := range segment.Images() {
text, err := tes.ExtractTextFromImage(img, language.English)
if err != nil {
log.Fatal(err)
}

fmt.Println(text)
}
}

listener, err := net.Listen("tcp", ":3000")
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ require (
require (
github.com/aws/aws-sdk-go v1.38.20 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mattn/go-sqlite3 v1.14.23 // indirect
github.com/u2takey/go-utils v0.3.1 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down Expand Up @@ -75,4 +77,6 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
69 changes: 69 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package config

import (
"fmt"
"os"
"path/filepath"
"sync"

"gopkg.in/yaml.v3"
)

type t struct {
}

var (
config t
path string
once sync.Once
)

func Config() *t {
return &config
}

func Init(basepath string) (e error) {
once.Do(func() {
path = filepath.Join(basepath, "config.yaml")

file, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
file, err := os.Create(filepath.Join(basepath, "config.yaml"))
if err != nil {
e = fmt.Errorf("Error creating config file: %v", err)
return
}
file.Close()
} else {
e = fmt.Errorf("Error reading config file: %v", err)
return
}

file = make([]byte, 0)
}

if err := yaml.Unmarshal(file, &config); err != nil {
e = fmt.Errorf("Error unmarshaling file: %v", err)
}
})

return
}

func Write() error {
if path == "" {
return fmt.Errorf("Config not initilized")
}

output, err := yaml.Marshal(&config)
if err != nil {
return fmt.Errorf("Error marshaling file: %v", err)
}

if err := os.WriteFile(path, output, 644); err != nil {
return fmt.Errorf("Error writing config: %v", err)
}

return nil
}
1 change: 1 addition & 0 deletions internal/srt/routines/library/library.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package library

0 comments on commit 5f3fc9e

Please sign in to comment.