add json option for transl. + gofmt

This commit is contained in:
Arya 2023-08-29 14:50:56 +05:30
parent d92958aae0
commit 5ffd20046a
Signed by: arya
GPG Key ID: 842D12BDA50DF120
4 changed files with 58 additions and 30 deletions

View File

@ -2,15 +2,17 @@ package cmd
import ( import (
"fmt" "fmt"
"codeberg.org/aryak/mozhi/utils" "codeberg.org/aryak/mozhi/utils"
"github.com/spf13/cobra"
"github.com/ktr0731/go-fuzzyfinder" "github.com/ktr0731/go-fuzzyfinder"
"github.com/spf13/cobra"
) )
var ( var (
engineused string engineused string
listtype string listtype string
raw bool raw bool
) )
// langlistCmd represents the langlist command // langlistCmd represents the langlist command

View File

@ -1,17 +1,20 @@
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"codeberg.org/aryak/mozhi/utils" "codeberg.org/aryak/mozhi/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var ( var (
engine string engine string
query string query string
source string source string
dest string dest string
rawjson bool
) )
var translateCmd = &cobra.Command{ var translateCmd = &cobra.Command{
@ -20,27 +23,45 @@ var translateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if engine == "all" { if engine == "all" {
data := utils.TranslateAll(dest, source, query) data := utils.TranslateAll(dest, source, query)
for i := 0; i < len(data); i++ { if rawjson {
fmt.Println("-----------------------------------") j, err := json.Marshal(data)
fmt.Println("Engine: "+data[i].Engine) if err != nil {
fmt.Println("Translated Text: "+data[i].OutputText) fmt.Println(err)
if source == "auto" { } else {
fmt.Println("Detected Language: "+data[i].AutoDetect) fmt.Println(string(j))
}
} else {
for i := 0; i < len(data); i++ {
fmt.Println("-----------------------------------")
fmt.Println("Engine: " + data[i].Engine)
fmt.Println("Translated Text: " + data[i].OutputText)
if source == "auto" {
fmt.Println("Detected Language: " + data[i].AutoDetect)
}
fmt.Println("Source Language: " + data[i].SourceLang)
fmt.Println("Target Language: " + data[i].TargetLang)
} }
fmt.Println("Source Language: "+data[i].SourceLang)
fmt.Println("Target Language: "+data[i].TargetLang)
} }
} else { } else {
data, err := utils.Translate(engine, dest, source, query) data, err := utils.Translate(engine, dest, source, query)
if err != nil { if rawjson {
fmt.Println(err) j, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(j))
}
} else {
if err != nil {
fmt.Println(err)
}
fmt.Println("Translated Text: " + data.OutputText)
if source == "auto" {
fmt.Println("Detected Language: " + data.AutoDetect)
}
fmt.Println("Source Language: " + data.SourceLang)
fmt.Println("Target Language: " + data.TargetLang)
} }
fmt.Println("Translated Text: "+data.OutputText)
if source == "auto" {
fmt.Println("Detected Language: "+data.AutoDetect)
}
fmt.Println("Source Language: "+data.SourceLang)
fmt.Println("Target Language: "+data.TargetLang)
} }
}, },
} }
@ -53,6 +74,7 @@ func init() {
translateCmd.Flags().StringVarP(&source, "source", "s", "", "Source language. Use langlist command to get code for your language") translateCmd.Flags().StringVarP(&source, "source", "s", "", "Source language. Use langlist command to get code for your language")
translateCmd.Flags().StringVarP(&dest, "dest", "t", "", "Target language. Use langlist command to get code for your language") translateCmd.Flags().StringVarP(&dest, "dest", "t", "", "Target language. Use langlist command to get code for your language")
translateCmd.Flags().StringVarP(&query, "query", "q", "", "Text to be translated") translateCmd.Flags().StringVarP(&query, "query", "q", "", "Text to be translated")
translateCmd.Flags().BoolVarP(&rawjson, "raw", "r", false, "Return output as json")
translateCmd.MarkFlagRequired("engine") translateCmd.MarkFlagRequired("engine")
translateCmd.MarkFlagRequired("source") translateCmd.MarkFlagRequired("source")

View File

@ -6,6 +6,7 @@ import (
) )
// HandleSourceLanguages godoc // HandleSourceLanguages godoc
//
// @Summary Show list of available source languages for engine // @Summary Show list of available source languages for engine
// @Param engine query string true "Engine name" // @Param engine query string true "Engine name"
// @Success 200 {object} utils.List // @Success 200 {object} utils.List
@ -23,6 +24,7 @@ func HandleSourceLanguages(c *fiber.Ctx) error {
} }
// HandleTargetLanguages godoc // HandleTargetLanguages godoc
//
// @Summary Show list of available target languages for engine // @Summary Show list of available target languages for engine
// @Param engine query string true "Engine name" // @Param engine query string true "Engine name"
// @Success 200 {object} utils.List // @Success 200 {object} utils.List
@ -40,6 +42,7 @@ func HandleTargetLanguages(c *fiber.Ctx) error {
} }
// HandleTTS godoc // HandleTTS godoc
//
// @Summary Get Text-To-Speech for specified language using specified engine // @Summary Get Text-To-Speech for specified language using specified engine
// @Param engine query string true "Engine name" // @Param engine query string true "Engine name"
// @Param lang query string true "Language being TTS'd" // @Param lang query string true "Language being TTS'd"
@ -61,6 +64,7 @@ func HandleTTS(c *fiber.Ctx) error {
} }
// HandleTranslate godoc // HandleTranslate godoc
//
// @Summary Translate text // @Summary Translate text
// @Description When engine is set to all, it will return an array of utils.LangOut. // @Description When engine is set to all, it will return an array of utils.LangOut.
// @Param engine query string true "Engine name" // @Param engine query string true "Engine name"

View File

@ -6,9 +6,9 @@ import (
"os" "os"
"runtime" "runtime"
_ "codeberg.org/aryak/mozhi/docs"
"codeberg.org/aryak/mozhi/pages" "codeberg.org/aryak/mozhi/pages"
"codeberg.org/aryak/mozhi/utils" "codeberg.org/aryak/mozhi/utils"
_ "codeberg.org/aryak/mozhi/docs"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress" "github.com/gofiber/fiber/v2/middleware/compress"
@ -22,12 +22,12 @@ import (
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
) )
// @title Mozhi API // @title Mozhi API
// @version 1.0 // @version 1.0
// @description API for Mozhi, the alternative-frontend for many translation engines. // @description API for Mozhi, the alternative-frontend for many translation engines.
// @license.name AGPL 3.0 // @license.name AGPL 3.0
// @license.url https://www.gnu.org/licenses/agpl-3.0.txt // @license.url https://www.gnu.org/licenses/agpl-3.0.txt
// @BasePath /api // @BasePath /api
func Serve(port string) { func Serve(port string) {
engine := html.New("./views", ".html") engine := html.New("./views", ".html")