From a62f59a73d5a6a6dede8ba8291601e40a092530f Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sat, 16 Apr 2022 11:56:00 +0100 Subject: [PATCH] Add the org-mode entry --- README.md | 4 ++- orion.go | 5 ++-- pkg/core/core.go | 67 +++++++++++++++++++++++++++++++----------------- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 74d128b..7ad33d1 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,13 @@ In order to use it, first you need to setup the project which you can just call following command: ```bash -./builder run -owner TELEGRAM_ID -model PATH_TO_MODEL -scorer PATH_TO_SCORER +./builder run -owner TELEGRAM_ID -org-file ORG_FILE_PATH -model PATH_TO_MODEL -scorer PATH_TO_SCORER ``` * **TELEGRAM_ID**: Is your telegram id. It will only reply to you and tell others to F off. +* **ORG_FILE_PATH**: Path to an `org-mode` file that **HAS TO EXIST**. Orion will append entries +to this file. * **PATH_TO_MODEL(optoinal)**: Is the path to the model file you want to use for speech recognition. the default points to the default model in the models directory. diff --git a/orion.go b/orion.go index 9068bad..cd68ea4 100644 --- a/orion.go +++ b/orion.go @@ -33,6 +33,7 @@ var model = flag.String("model", "models/default.tflite", "Path to the model (pr var scorer = flag.String("scorer", "models/default.scorer", "Path to the external scorer") var owner = flag.String("owner", "", "Telegram user id that is allowed to use this bot") var storage = flag.String("voice-storage", "", "Where to store the voices") +var orgfile = flag.String("org-file", "", "The target Orgfile") func main() { flag.Parse() @@ -44,7 +45,7 @@ func main() { return } - if *model == "" || *scorer == "" { + if *model == "" || *scorer == "" || *orgfile == "" { // In case of error print error and print usage // This can also be done by passing -h or --help flags fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0]) @@ -61,7 +62,7 @@ func main() { *storage = filepath.Join(dir, ".orion", "storage") } - bot, err := core.CreateBot(storage) + bot, err := core.CreateBot(orgfile, storage) if err != nil { log.Fatal(err) return diff --git a/pkg/core/core.go b/pkg/core/core.go index ccde7b6..94b5ad2 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -18,10 +18,10 @@ package core import ( "fmt" "io" - "io/ioutil" "log" "os" "path/filepath" + "strconv" "strings" "time" @@ -31,23 +31,25 @@ import ( ) type Bot struct { - Owner string - StoragePath string - VoiceStorage string - Token string - Model *asticoqui.Model + Owner string + StoragePath string + VoiceStorage string + Token string + Model *asticoqui.Model ExtendedMetaData bool - MaxResults uint - db *DB + MaxResults uint + OrgFile string + Format string + db *DB } type Entry struct { - FilePath string + FilePath string Transcript string - Visited bool + Visited bool } -func CreateBot(storage *string) (*Bot,error) { +func CreateBot(orgfile *string, storage *string) (*Bot, error) { voiceStorage := filepath.Join(*storage, "voices") dbpath := filepath.Join(*storage, "db") err := os.MkdirAll(voiceStorage, 0750) @@ -63,12 +65,14 @@ func CreateBot(storage *string) (*Bot,error) { } return &Bot{ - Token: os.Getenv("BOT_TOKEN"), - MaxResults: 5, + Token: os.Getenv("BOT_TOKEN"), + MaxResults: 5, ExtendedMetaData: false, - StoragePath: *storage, - VoiceStorage: voiceStorage, - db: db, + StoragePath: *storage, + VoiceStorage: voiceStorage, + OrgFile: *orgfile, + db: db, + Format: "* %s\n:FILE:\npath:%s\nsize:%s\n:END:\n", }, nil } @@ -81,7 +85,7 @@ func (bot *Bot) ShutDown() error { func (bot *Bot) StartBot() { pref := tele.Settings{ - Token: bot.Token, + Token: bot.Token, // TODO: Move this to the config Poller: &tele.LongPoller{Timeout: 10 * time.Second}, } @@ -125,8 +129,27 @@ func (bot *Bot) StartBot() { b.Start() } -func (bot *Bot) SaveTranscript(filepath *string, text *string) error { - return ioutil.WriteFile(*filepath + ".txt", []byte(*text), 0600) +func (bot *Bot) SaveTranscript(voicefilepath *string, text *string) error { + fi, err := os.Stat(*voicefilepath) + if err != nil { + return err + } + + entry := fmt.Sprintf(bot.Format, + *text, *voicefilepath, + strconv.FormatInt(fi.Size(), 10)) + + f, err := os.OpenFile(bot.OrgFile, os.O_APPEND|os.O_WRONLY, 0600) + if err != nil { + return err + } + + defer f.Close() + + if _, err = f.WriteString(entry); err != nil { + return err + } + return nil } func (bot *Bot) isOwner(c tele.Context) bool { @@ -144,8 +167,8 @@ func (bot *Bot) StoreVoice(c tele.Context) (*string, error) { v := c.Message().Voice.File time := c.Message().Time().Format("2006-01-02_15:04:05") - path := filepath.Join(bot.VoiceStorage, time + ".ogg") - wavePath := filepath.Join(bot.VoiceStorage, time + ".wav") + path := filepath.Join(bot.VoiceStorage, time+".ogg") + wavePath := filepath.Join(bot.VoiceStorage, time+".wav") if err := c.Bot().Download(&v, path); err != nil { return nil, err @@ -162,7 +185,6 @@ func (bot *Bot) StoreVoice(c tele.Context) (*string, error) { return &wavePath, nil } - func (bot *Bot) ConvertToText(voice *string) (*[]string, error) { // Stat audio i, err := os.Stat(*voice) @@ -222,7 +244,6 @@ func (bot *Bot) ConvertToText(voice *string) (*[]string, error) { return &results, nil } - func metadataToStrings(m *asticoqui.Metadata) []string { results := make([]string, 0, m.NumTranscripts()) for _, tr := range m.Transcripts() {