-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathudlx.go
462 lines (371 loc) · 12 KB
/
udlx.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// the is a rework for easy understanding & use, not built from scratch!
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/machinebox/progress"
"golang.org/x/net/html"
)
// StreamUrls download link response struct
type StreamUrls struct {
Video []Video
}
// Response download link response struct
type Response struct {
AssetType string `json:"asset_type"`
StreamUrls StreamUrls `json:"stream_urls"`
}
// Video videos response struct
type Video struct {
File, Type, Label string
}
// CourseResponse videos response struct
type CourseResponse struct {
Results []Course
}
// CourseDetail videos response struct
type CourseDetail struct {
Results []CourseContent
}
// CourseContent detail
type CourseContent struct {
Class string `json:"_class"`
ID int
Title string
Asset Asset
ObjectIndex int `json:"object_index"`
}
// Asset detail
type Asset struct {
Class string `json:"_class"`
ID int
AssetType string `json:"asset_type"`
Filename string
SupplementaryAssets []SupplementaryAssets `json:"supplementary_assets"`
Title string
ObjectIndex int
}
// SupplementaryAssets detail
type SupplementaryAssets struct {
Class string `json:"_class"`
ID int
AssetType string `json:"asset_type"`
Filename string
}
// Course videso response struct
type Course struct {
ID int
Title, URL string
}
// Udemy struct
type Udemy struct {
AccessToken string
CourseURL string
SelectedCourseID string
Start int
End int
Resolution string
SessionMaxAttempt int
CurrentAttempt int
DownloadPath string
}
// Udemy URLs
const (
GetCoursesURL = "https://www.udemy.com/api-2.0/users/me/subscribed-courses/?ordering=-last_accessed&fields[course]=@min,title,id&page=1&page_size=100"
GetDownloadURL = "https://www.udemy.com/api-2.0/assets/{{assetID}}?fields[asset]=@min,status,asset_type,time_estimation,stream_urls&fields"
GetCourseDetailURL = "https://www.udemy.com/api-2.0/courses/{{courseID}}/subscriber-curriculum-items/?page_size=1400&fields[lecture]=title,object_index,asset,supplementary_assets&fields[chapter]=title,object_index&fields[asset]=filename,asset_type&caching_intent=True"
)
func main() {
// Prints the package info message
Info()
accessToken := flag.String("access-token", "false", "Authentication Token")
CourseURL := flag.String("course-url", "false", "Course URL")
Start := flag.Int("start", 0, "Start Lecture Id")
End := flag.Int("end", 0, "End Lecture Id")
Resolution := flag.String("resolution", "false", "Video Resolution")
DownloadPath := flag.String("download-location", "false", "Download Path")
flag.Parse()
u := Udemy{
AccessToken: "Bearer " + *accessToken,
CourseURL: *CourseURL,
SelectedCourseID: "false",
Start: *Start,
End: *End,
Resolution: *Resolution,
SessionMaxAttempt: 3,
CurrentAttempt: 0,
DownloadPath: *DownloadPath,
}
_, err := u.AuthenticateToken()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
_, err = u.GetCourses()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
courseAssests, err := u.GetCourseDetail()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
u.startDownloading(courseAssests)
// Wait for user input before exiting the program
fmt.Println("✅ All downloads completed. Press Enter to exit.")
fmt.Scanln()
}
// Info Package info message
func Info() {
info := `
...
//...the revive projekt!
*****************************************************
** **
** ██╗░░░██╗██████╗░██╗░░░░░██╗░░██╗ **
** ██║░░░██║██╔══██╗██║░░░░░╚██╗██╔╝ **
** ██║░░░██║██║░░██║██║░░░░░░╚███╔╝░ **
** ██║░░░██║██║░░██║██║░░░░░░██╔██╗░ **
** ╚██████╔╝██████╔╝███████╗██╔╝╚██╗ **
** ░╚═════╝░╚═════╝░╚══════╝╚═╝░░╚═╝ **
** Yet Another Udemy Course Downloader **
*****************************************************
Name : UdlX
Version : 1.0b
Author : mr•vybes (iykex)
Github : https://github.com/iykex
`
fmt.Println(info)
}
// BytesToMegaBytes convert bytes to mb
func BytesToMegaBytes(n int64) float64 {
mb := float64(n / (1000 * 1024))
return math.Floor(mb*100) / 100
}
// NewRequest to create new request for udemy
func (u Udemy) NewRequest(method, url string) *http.Response {
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
}
req.Header.Add("Authorization", u.AccessToken)
res, err := client.Do(req)
return res
}
// AuthenticateToken Authnticate user provided token
func (u *Udemy) AuthenticateToken() (bool, error) {
if u.AccessToken == "Bearer false" {
u.getAuthenticationToken()
}
fmt.Println("✳️ : Authenticating Access Token...")
resp := u.NewRequest("HEAD", GetCoursesURL)
defer resp.Body.Close()
if resp.StatusCode > 299 {
return false, errors.New("❌ : Invalid Authentication Token")
}
fmt.Println("➕ : Succesfully Authenticated ✅")
return true, nil
}
// GetCourses to get all the courses details
func (u Udemy) GetCourses() (bool, error) {
// Return if CourseURL is present
if u.CourseURL != "false" {
return false, nil
}
fmt.Println("✳️ : Fetching courses...")
resp := u.NewRequest("GET", GetCoursesURL)
defer resp.Body.Close()
if resp.StatusCode > 299 {
return false, errors.New("❌ : Error fetching courses, try to open link in your browser \n" + GetCoursesURL)
}
body, _ := ioutil.ReadAll(resp.Body)
// fmt.Println(body)
var response CourseResponse
json.Unmarshal(body, &response)
fmt.Println("➕ : Courses [ID]")
for i := range response.Results {
fmt.Printf(" ID[%v] : %v \n", response.Results[i].ID, response.Results[i].Title)
}
return true, nil
}
// GetCourseDetail to get course detail
func (u *Udemy) GetCourseDetail() ([]Asset, error) {
if u.CourseURL != "false" {
u.ParseHTMLAndGetCourseID()
}
if u.SelectedCourseID == "false" {
u.getCourseID()
}
fmt.Println("✳️ : Fetching course lectures...")
url := strings.Replace(GetCourseDetailURL, "{{courseID}}", u.SelectedCourseID, 1)
res := u.NewRequest("GET", url)
defer res.Body.Close()
if res.StatusCode == 404 {
fmt.Println("❌ : Invalid course ID")
u.SelectedCourseID = "false"
return u.GetCourseDetail()
}
if res.StatusCode > 299 {
return nil, errors.New("❌ : Error fetching course lectures, try to open link in your browser \n" + url)
}
body, _ := ioutil.ReadAll(res.Body)
var response CourseDetail
json.Unmarshal(body, &response)
finalAsset := make([]Asset, len(response.Results))
fmt.Println("➕ : Lectures [No.]")
for i := range response.Results {
if response.Results[i].Class == "lecture" && (response.Results[i].Asset.AssetType == "Video" || response.Results[i].Asset.AssetType == "File") {
fmt.Printf(" No.[%v] : %v[%v] \n", response.Results[i].ObjectIndex, response.Results[i].Title, response.Results[i].Asset.AssetType)
response.Results[i].Asset.Title = response.Results[i].Title
response.Results[i].Asset.ObjectIndex = response.Results[i].ObjectIndex
finalAsset[response.Results[i].ObjectIndex] = response.Results[i].Asset
}
}
return finalAsset, nil
}
// getCourseID get course id from user input
func (u *Udemy) getCourseID() {
fmt.Print("❓ : Enter the course ID which you want to download: ")
var courseID string
fmt.Scanln(&courseID)
u.SelectedCourseID = courseID
}
// getAuthenticationToken get lectures id which needs to download
func (u *Udemy) getAuthenticationToken() {
var token string
fmt.Print("⚠️ You must have your Udemy Access Token Ready ⚠️\n")
fmt.Print("➡️ Enter your udemy access token: ")
fmt.Scanln(&token)
u.AccessToken = "Bearer " + token
}
// getLecturesIDs get lectures id which needs to download
func (u *Udemy) getLecturesIDs() {
var start, end int
fmt.Print("❓ : Enter the Lecture No. to start download from: ")
fmt.Scanln(&start)
fmt.Print("❓ : Enter the Lecture No. you want end download: ")
fmt.Scanln(&end)
u.Start = start
u.End = end
}
// getVideoResolution get resolution which need to download
func (u *Udemy) getVideoResolution() {
var resolution string
fmt.Print("❓ : Enter the video Resolution(360|480|720|1080): ")
fmt.Scanln(&resolution)
u.Resolution = resolution
}
// GetDownloadLink to get the video download link
func (u *Udemy) GetDownloadLink(asset Asset) error {
u.CurrentAttempt = u.CurrentAttempt + 1
url := strings.Replace(GetDownloadURL, "{{assetID}}", strconv.Itoa(asset.ID), 1)
res := u.NewRequest("GET", url)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var response Response
json.Unmarshal(body, &response)
var videosUrls = response.StreamUrls.Video
for i := range videosUrls {
if videosUrls[i].Label == u.Resolution {
return u.Download(videosUrls[i].File, asset)
}
}
fmt.Printf("❌ Don't have any valid download link for resolution %v, try with different resolution. \n", u.Resolution)
if u.SessionMaxAttempt >= u.CurrentAttempt {
u.getVideoResolution()
u.GetDownloadLink(asset)
} else {
fmt.Println("❌ Max attempt exceeded, please try again.")
os.Exit(0)
}
return nil
}
func (u *Udemy) startDownloading(courseAsset []Asset) {
if u.Start == 0 || u.End == 0 {
u.getLecturesIDs()
}
if u.Resolution == "false" {
u.getVideoResolution()
}
for l := u.Start; l <= u.End; l++ {
if courseAsset[l].ID != 0 {
u.GetDownloadLink(courseAsset[l])
}
}
}
// Download to download files and vidoes
func (u *Udemy) Download(downloadURL string, asset Asset) error {
out, err := os.Create(strconv.Itoa(asset.ObjectIndex) + ". " + asset.Title + ".mp4")
if err != nil {
return errors.New("❌ Error creating a new file, try to download from link" + downloadURL)
}
defer out.Close()
resp, err := http.Head(downloadURL)
if err != nil {
fmt.Print(err)
return err
}
size, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
defer resp.Body.Close()
res, err := http.Get(downloadURL)
if err != nil {
return errors.New("❌ Error in downloading video, try to download from link" + downloadURL)
}
defer res.Body.Close()
r := progress.NewReader(res.Body)
// Start a goroutine printing progress
go func() {
ctx := context.Background()
progressChan := progress.NewTicker(ctx, r, size, 1*time.Second)
for p := range progressChan {
fmt.Printf("\r 🔻 Downloading : %v(%.2f MB/%.2f MB)", asset.Title, BytesToMegaBytes(p.N()), BytesToMegaBytes(p.Size()))
}
s := strconv.FormatFloat(BytesToMegaBytes(size), 'f', -1, 64)
fmt.Println(" - Download Finished : " + asset.Title + "(" + s + "MB)")
}()
var _, copyError = io.Copy(out, r)
if copyError != nil {
return copyError
}
return nil
}
// ParseHTMLAndGetCourseID it will parse the html content and get course id
func (u *Udemy) ParseHTMLAndGetCourseID() {
res := u.NewRequest("GET", u.CourseURL)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
bodyString := string(body)
doc, err := html.Parse(strings.NewReader(bodyString))
if err != nil {
log.Fatal(err)
}
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "body" {
for _, a := range n.Attr {
if a.Key == "data-clp-course-id" {
u.SelectedCourseID = a.Val
break
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
}