-
Notifications
You must be signed in to change notification settings - Fork 2
/
binding.go
113 lines (102 loc) · 3.51 KB
/
binding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package YouTubeDownloader
import (
"github.com/zserge/lorca"
D "github.com/s77rt/YouTubeDownloader/downloader"
M "github.com/s77rt/YouTubeDownloader/merger"
)
func BindUI(ui lorca.UI) {
ui.Bind("Ready", func() {
ui.Eval(`setVersion(`+MarshalJSONS(Version)+`);`)
ui.Eval(`w_CheckFFmpeg();`)
})
ui.Bind("CheckFFmpeg", func() {
if exists := M.FFmpeg_exists(); !exists {
ui.Eval(`w_CheckFFmpeg__onerror("FFmpeg is not installed");`)
}
})
ui.Bind("GetVideo", func(url string) {
video, err := extractor.GetVideo(url)
if err != nil {
ui.Eval(`w_GetVideo__onerror(`+MarshalJSONS(err.Error())+`);`)
} else {
ui.Eval(`add_video(`+MarshalJSONS(video)+`);`)
}
ui.Eval(`h_unlock();`)
})
ui.Bind("GetVideos", func(url string) {
videos, err := extractor.GetVideos(url)
if err != nil {
ui.Eval(`w_GetVideo__onerror(`+MarshalJSONS(err.Error())+`);`)
} else {
for _, video := range videos {
ui.Eval(`add_video(`+MarshalJSONS(video)+`);`)
}
}
ui.Eval(`h_unlock();`)
})
ui.Bind("GetExistingVideo", func(uuid string, video interface{}, formats []interface{}) {
found := false
for _, format := range(formats) {
filename, err := getOutputFile(extractor, downloader, video, format)
if err != nil {
ui.Eval(`w_GetExistingVideo__onerror(`+MarshalJSONS(uuid)+`, `+MarshalJSONS(err.Error())+`);`)
break
}
if FileExists(filename) {
found = true
ui.Eval(`update_video_success(`+MarshalJSONS(uuid)+`,`+MarshalJSONS(filename)+`);`)
break
}
}
if !found {
ui.Eval(`w_GetExistingVideo__onerror(`+MarshalJSONS(uuid)+`, "not available");`)
}
})
ui.Bind("DownloadVideo", func(uuid string, video interface{}, format interface{}) {
task := tasks.New(uuid)
filename, err := DownloadVideo(task.Context, task.Tmpdir, extractor, downloader, video, format, func(p1, p2 *D.Progress) {
ui.Eval(`update_video_progress(`+MarshalJSONS(uuid)+`,`+MarshalJSONS(p1)+`,`+MarshalJSONS(p2)+`);`)
})
if err != nil {
if task.Context.Err() != nil {
/*
Errors that are catched in this state where the context is closed
are probably fine to ignore, because as the context get closed, by default a "post" function
get executed and will delete the tmp files for that task, which may result in "no such file or directory" error
for a function that may still trying to use such temp files, thus the error.
the error may also be context.Canceled which is fine.
this should be further investigated...
*/
} else {
ui.Eval(`w_DownloadVideo__onerror(`+MarshalJSONS(uuid)+`, `+MarshalJSONS(err.Error())+`);`)
}
} else {
ui.Eval(`update_video_success(`+MarshalJSONS(uuid)+`,`+MarshalJSONS(filename)+`);`)
}
task.Done()
})
ui.Bind("CancelDownload", func(uuid string) {
err := tasks.Abort(uuid)
if err != nil {
ui.Eval(`w_CancelDownload__onerror(`+MarshalJSONS(uuid)+`, `+MarshalJSONS(err.Error())+`);`)
}
})
ui.Bind("PlayVideo", func(uuid string, video_filename string) {
err := RunFile(video_filename)
if err != nil {
ui.Eval(`w_PlayVideo__onerror(`+MarshalJSONS(uuid)+`, `+MarshalJSONS(err.Error())+`);`)
}
})
ui.Bind("OpenFolder", func(uuid string, video_filename string) {
err := OpenFolder(video_filename)
if err != nil {
ui.Eval(`w_OpenFolder__onerror(`+MarshalJSONS(uuid)+`, `+MarshalJSONS(err.Error())+`);`)
}
})
ui.Bind("DeleteVideo", func(uuid string, video_filename string) {
err := DeleteFile(video_filename)
if err != nil {
ui.Eval(`w_DeleteVideo__onerror(`+MarshalJSONS(uuid)+`, `+MarshalJSONS(err.Error())+`);`)
}
})
}