-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
285 lines (210 loc) · 5.99 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net/url"
"os"
"os/exec"
"path"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/kennygrant/sanitize"
)
type WebServicesResponse struct {
Video_1_m3u8_list string `json:"video_1_m3u8_list"`
Video_1_360_m3u8 string `json:"video_1_360_m3u8"`
Video_1_720_m3u8 string `json:"video_1_720_m3u8"`
Video_2_m3u8_list string `json:"video_2_m3u8_list"`
Video_2_360_m3u8 string `json:"video_2_360_m3u8"`
Video_2_720_m3u8 string `json:"video_2_720_m3u8"`
}
func main() {
ed_url := flag.String("url", "", "Eduscope URL")
username := flag.String("u", "", "Eduscope User Name")
password := flag.String("p", "", "Eduscope Password")
high_quality := flag.Bool("high-quality", false, "Downloads video at a higher quality")
threads := flag.Int("t", 8, "Set Thread Count for the HTTP client")
custom_ffmpeg := flag.String("ff", "-c copy", "Custom FFMPEG arguments")
flag.Parse()
reader := bufio.NewReader(os.Stdin)
defer func() {
fmt.Println("Press Enter key to exit.")
reader.ReadByte()
}()
// Check for ffmpeg
_, err := exec.LookPath("ffmpeg")
if err != nil {
log.Println(err)
log.Println("FFMPEG not found. Put ffmpeg.exe in the current dir or in the PATH")
return
}
// log.Printf("FFMEPG found in %s", ffpath)
if len(*ed_url) <= 0 {
InteractiveMode(username, password, ed_url, high_quality, reader)
}
username_trimmed := strings.TrimSpace(*username)
password_trimmed := strings.TrimSpace(*password)
// Parse URL
u, err := url.Parse(*ed_url)
if err != nil {
log.Println(err)
return
}
videoId := strings.TrimSpace(u.Query().Get("id"))
log.Println(videoId)
if len(videoId) <= 0 {
log.Println("This is not a URL to a eduscope video.")
return
}
videoName := sanitize.Path(videoId)
client := NewHttpClient()
if len(username_trimmed) > 0 {
log.Println(username_trimmed, password_trimmed)
resp, err := client.PostForm("https://lecturecapture.sliit.lk/login.php", url.Values{
"inputEmail": {username_trimmed},
"inputPassword": {password_trimmed},
"submit": {""},
})
if err != nil {
log.Println("PostForm Error")
return
}
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
log.Println(err)
return
}
user := strings.TrimSpace(doc.Find("#dropdown08").Text())
if len(user) <= 0 {
log.Println("Username or password error")
return
}
log.Println("Logged in as " + user)
resp, rErr := client.Client.Get(*ed_url)
if rErr != nil {
log.Println(err)
return
}
doc2, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
log.Println(err)
return
}
title := strings.TrimSpace(doc2.Find("#content-wrapper > div > div.col-md-12 > h2").Text())
videoName = sanitize.Path(title)
}
var res WebServicesResponse
query := url.Values{}
query.Add("key", "vhjgyu456dCT")
query.Add("type", "video_paths")
query.Add("id", videoId)
query.Add("full", "ZnVsbA==")
if err := client.GetJson("https://lecturecapture.sliit.lk/webservice.php", &res, &query); err != nil {
log.Println(err)
return
}
ur, err := url.Parse("https://lecturecapture.sliit.lk/webservice.php")
if err != nil {
log.Println(err)
return
}
if *high_quality {
if len(res.Video_1_720_m3u8) <= 0 {
log.Println("High quality is not available")
return
}
log.Println("Selected High Quality video")
ur.Path = path.Join(u.Path, res.Video_1_720_m3u8)
videoName += "-high-quality"
} else {
ur.Path = path.Join(u.Path, res.Video_1_360_m3u8)
}
log.Printf("M3U8 Playlist URL : %s", ur.String())
log.Println("Starting Download...")
dErr, m3u8_file := DownloadWithHttp(client.Client, ur.String(), videoName, *threads)
if dErr != nil {
log.Println(dErr, 1)
return
}
before := []string{"-y", "-i", m3u8_file}
mid := strings.Split(*custom_ffmpeg, " ")
after := []string{videoName + ".mkv"}
args := append(before, mid...)
args = append(args, after...)
log.Println("Merging all parts together using FFMPEG")
log.Printf("Using Arguments %v", args)
if err := FFMPEG(args...); err != nil {
log.Println(err)
return
}
if err := os.RemoveAll(videoName); err != nil {
log.Println("Unable to remove " + videoName)
}
log.Printf("Video %s download complete. Byeee", videoName)
}
func InteractiveMode(username *string, password *string, ed_url *string, high_quality *bool, reader *bufio.Reader) {
fmt.Print("~~~~~~~~~ Welcome to Dehemi's Eduscope Downloader ~~~~~~~~~\n\n")
fmt.Println("--- Enter an Eduscope URL")
for {
u := getInput(reader, "--- Enter an Eduscope URL", true)
parsed, err := url.Parse(u)
if err != nil {
goto wrongurl
}
if strings.Compare(parsed.Host, "lecturecapture.sliit.lk") != 0 {
goto wrongurl
}
if len(parsed.Query().Get("id")) <= 0 {
goto wrongurl
}
*ed_url = string(u)
break
wrongurl:
fmt.Println("--- Invalid URL, Enter a correct one")
}
fmt.Print("\n--- Use username and password? (y,N)\n--- (That way it will fetch the acctual lecture name and rename the video to it)\n")
if getChoice(reader) {
fmt.Println("--- Enter Username")
*username = getInput(reader, "--- Please enter an username", true)
fmt.Println("--- Enter Password")
*password = getInput(reader, "--- Please enter a password", false)
}
fmt.Println("--- Want High Quality Video? (y,N)")
if getChoice(reader) {
*high_quality = true
}
}
func getChoice(reader *bufio.Reader) bool {
choice, _, _ := reader.ReadLine()
low_choice := strings.ToLower(string(choice))
return (strings.Compare(low_choice, "y") == 0)
}
func getInput(reader *bufio.Reader, invalid string, trim bool) string {
input := ""
for {
input_bytes, _, _ := reader.ReadLine()
if trim {
input = strings.TrimSpace(string(input_bytes))
} else {
input = string(input_bytes)
}
if len(input) <= 0 {
fmt.Println(invalid)
} else {
break
}
}
return input
}
func FFMPEG(args ...string) error {
cmd := exec.Command("ffmpeg", args...)
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return err
}
return nil
}