Skip to content

Commit

Permalink
Added translation visualization by showing pictures ffrom urls
Browse files Browse the repository at this point in the history
  • Loading branch information
trezorg committed May 13, 2024
1 parent 12d4e84 commit c60a685
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 58 deletions.
13 changes: 12 additions & 1 deletion internal/fakeapi/data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fakeapi

import (
"net/url"
"strings"
"testing"

Expand All @@ -9,7 +10,17 @@ import (
)

var (
SoundURL = "http://audiocdn.lingualeo.com/v2/3/102085-631152000.mp3"
SoundURL = "http://audiocdn.lingualeo.com/v2/3/102085-631152000.mp3"
u1, _ = url.Parse("http://contentcdn.lingualeo.com/uploads/picture/31064.png")
u2, _ = url.Parse("http://contentcdn.lingualeo.com/uploads/picture/335521.png")
u3, _ = url.Parse("http://contentcdn.lingualeo.com/uploads/picture/374830.png")
u4, _ = url.Parse("http://contentcdn.lingualeo.com/uploads/picture/620779.png")
PictureUrls = []*url.URL{
u1,
u2,
u3,
u4,
}
ResponseData = []byte(`{"error_msg":"","translate_source":"base","is_user":0,
"word_forms":[{"word":"accommodation","type":"прил."}],
"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/3589594.png",
Expand Down
8 changes: 6 additions & 2 deletions internal/files/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func (f File) GetIndex() int {
// FileDownloader structure
type FileDownloader struct{}

// NewFileDownloader initialize new file downloader
func NewFileDownloader() *FileDownloader {
// New initialize new file downloader
func New() *FileDownloader {
return &FileDownloader{}
}

Expand Down Expand Up @@ -77,3 +77,7 @@ func (f *FileDownloader) Download(url string) (string, error) {
}
return filename, nil
}

func (f *FileDownloader) Remove(path string) error {
return os.Remove(path)
}
37 changes: 37 additions & 0 deletions internal/player/player.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package player

import (
"os/exec"
"strings"
)

const (
separator = " "
)

type Player struct {
player string
params []string
}

func New(player string) Player {
parts := strings.Split(player, separator)
playerExec := parts[0]
params := parts[1:]
return Player{
params: params,
player: playerExec,
}
}

func (p Player) Play(url string) error {
params := append(p.params[:len(p.params):len(p.params)], url)
cmd := exec.Command(p.player, params...)
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return err
}
return nil
}
33 changes: 33 additions & 0 deletions internal/visualizer/browser/browser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package browser

import (
"net/url"
"os/exec"
"runtime"
)

func open(u *url.URL) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, u.String())
return exec.Command(cmd, args...).Start()
}

type Visualizer func(u *url.URL) error

func (v Visualizer) Show(u *url.URL) error {
return v(u)
}

func New() Visualizer {
return Visualizer(open)
}
9 changes: 9 additions & 0 deletions pkg/translator/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ func prepareArgs(version string) (Lingualeo, error) {
Usage: "Pronounce words",
Destination: &args.Sound,
},
&cli.BoolFlag{
Name: "picture",
Aliases: []string{"pc"},
Usage: "Open translate pictures",
Destination: &args.Picture,
},
&cli.BoolFlag{
Name: "download",
Aliases: []string{"dl"},
Expand Down Expand Up @@ -339,6 +345,9 @@ func (args *Lingualeo) mergeConfigs(a *Lingualeo) {
if a.Sound {
args.Sound = a.Sound
}
if a.Picture {
args.Picture = a.Picture
}
if a.DownloadSoundFile {
args.DownloadSoundFile = a.DownloadSoundFile
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/translator/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
//go:generate mockery
type Downloader interface {
Download(url string) (string, error)
Remove(path string) error
}

// DownloadFiles download files from URLs channel
func DownloadFiles(ctx context.Context, urls <-chan string, downloader Downloader) <-chan files.File {
// downloadFiles download files from URLs channel
func downloadFiles(ctx context.Context, urls <-chan string, downloader Downloader) <-chan files.File {
out := make(chan files.File)
var wg sync.WaitGroup
wg.Add(1)
Expand Down
2 changes: 1 addition & 1 deletion pkg/translator/download_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestDownloadWordFile(t *testing.T) {

close(inChan)

out := DownloadFiles(ctx, inChan, downloader)
out := downloadFiles(ctx, inChan, downloader)
fileName := (<-out).Filename
assert.Equal(t, fileName, testFile)
}
32 changes: 20 additions & 12 deletions pkg/translator/get_word_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package translator
import (
"context"
"log/slog"
"sync"
"testing"

"github.com/trezorg/lingualeo/internal/logger"
Expand All @@ -23,11 +22,17 @@ func TestProcessTranslationResponseJson(t *testing.T) {
testFile := "/tmp/test.file"
count := 1000 // max for race checking
translator := NewMock_Translator(t)

player := NewMock_Pronouncer(t)
visualizer := NewMock_Visualizer(t)
res := translateWordResult(fakeapi.SearchWord)

downloader.EXPECT().Download(fakeapi.SoundURL).Return(testFile, nil).Times(count)
downloader.EXPECT().Remove(testFile).Return(nil).Times(count)
translator.EXPECT().TranslateWord(fakeapi.SearchWord).Return(res).Times(count)
player.EXPECT().Play(testFile).Return(nil).Times(count)
for _, u := range fakeapi.PictureUrls {
visualizer.EXPECT().Show(u).Return(nil).Times(count)
}

logger.Prepare(slog.LevelError + 10)
searchWords := make([]string, 0, count)
Expand All @@ -37,18 +42,21 @@ func TestProcessTranslationResponseJson(t *testing.T) {
}
ctx := context.Background()

args := Lingualeo{Sound: true, Words: searchWords, Add: false, Translator: translator}
var wg sync.WaitGroup

wg.Add(1)
soundChan, _, resultChan := args.Process(ctx, &wg)
wg.Add(1)
args := Lingualeo{
Sound: true,
Words: searchWords,
Add: false,
Picture: true,
DownloadSoundFile: true,
Translator: translator,
Downloader: downloader,
Pronouncer: player,
Visualizer: visualizer,
}

go args.downloadAndPronounce(ctx, soundChan, &wg, downloader)
ch := args.translateToChan(ctx)

for result := range resultChan {
for result := range ch {
fakeapi.CheckResult(t, result, searchWords[0], fakeapi.Expected)
}

wg.Wait()
}
24 changes: 0 additions & 24 deletions pkg/translator/helpers.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
package translator

import (
"os/exec"
"strings"
"unicode"
)

func PlaySound(player string, url string) error {
parts := strings.Split(player, " ")
playerExec := parts[0]
params := append(parts[1:], url)
cmd := exec.Command(playerExec, params...)
err := cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
return err
}
return nil
}

func isCommandAvailable(name string) bool {
execName := strings.Split(name, " ")[0]
_, err := exec.LookPath(execName)
return err == nil
}

func isRussianWord(s string) bool {
for _, symbol := range s {
if !unicode.Is(unicode.Cyrillic, symbol) && !unicode.Is(unicode.Number, symbol) {
Expand Down
Loading

0 comments on commit c60a685

Please sign in to comment.