-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
342 lines (297 loc) · 8.94 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
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
package main
import (
"bufio"
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"os/exec"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/publicsuffix"
)
var email = flag.String("email", "", "your account email")
var password = flag.String("password", "", "your account password")
var course = flag.String("course", "gophercises", "course name")
var outputdir = flag.String("output", "", "output directory\n ie. where the course videos are saved")
var cachelocation = flag.String("cache", "", "specifies where the cache will be saved\nDefaults to a joncalhoun-dl-cache folder within the output directoy")
var help = flag.Bool("help", false, "prints this output")
// this will be used by youtube-dl binary to download video
var referer = "https://courses.calhoun.io"
var courses = map[string]string{
"testwithgo": "https://courses.calhoun.io/courses/cor_test",
"gophercises": "https://courses.calhoun.io/courses/cor_gophercises",
"algorithmswithgo": "https://courses.calhoun.io/courses/cor_algo",
"webdevwithgo": "https://courses.calhoun.io/courses/cor_webdev",
}
var delayDuration = 5
// ClientOption is the type of constructor options for NewClient(...).
type ClientOption func(*http.Client) error
func checkError(err error) {
if err != nil {
log.Fatalf("[joncalhoun-dl]:[Error]: %v.\nif you think this is unusual please create an issue %s\n",
err,
"https://github.com/timolinn/joncalhoun-dl/issues/new")
}
}
// NewClient constructs anew client which can make requests
// to course website
func NewClient(options ...ClientOption) (*http.Client, error) {
// Cookiejar provides automatic cookie management
// that would normally be accessed only via the browser
opts := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, err := cookiejar.New(&opts)
checkError(err)
c := &http.Client{Jar: jar}
for _, option := range options {
err := option(c)
if err != nil {
return nil, err
}
}
return c, nil
}
// WithTransport configures the client to use a different transport
func WithTransport(fn RoundTripperFunc) ClientOption {
return func(client *http.Client) error {
client.Transport = RoundTripperFunc(fn)
return nil
}
}
func main() {
// Parse commandline options
flag.Parse()
// Print usage options
if *help {
flag.PrintDefaults()
return
}
// validate input
validateInput()
client, err := NewClient()
checkError(err)
// Login
signin(client)
location := *outputdir + "/%(title)s.%(ext)s"
if *outputdir == "" {
cwd, err := os.Getwd()
checkError(err)
*outputdir = cwd + "/" + *course
location = *outputdir + "/%(title)s.%(ext)s"
}
fmt.Printf("[joncalhoun-dl]: output directory is %s\n", *outputdir)
// do some chores
setup()
// fetch video urls
videoURLs := getURLs(client)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for i, videoURL := range videoURLs {
if videoURL != "" {
fmt.Printf("[joncalhoun-dl]: downloading lesson 0%d of %s\n", i+1, *course)
fmt.Printf("[joncalhoun-dl]:[exec]: youtube-dl %s --referer %s -o %s\n", videoURL, referer, location)
cmd := exec.CommandContext(ctx, "youtube-dl", videoURL, "--referer", referer, "-o", location)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
fmt.Printf("[joncalhoun-dl]: downloaded lesson 0%d\n", i+1)
} else {
fmt.Printf("[joncalhoun-dl]: Page for lesson 0%d does not have an embedded video \n", i+1)
}
}
fmt.Println("Done! 🚀")
}
func validateInput() {
if !isSupported(*course) {
err := errors.New("course not supported yet")
checkError(err)
}
if *email == "" || *password == "" {
checkError(errors.New("try adding: [email protected] --password=12345' to the command"))
}
}
func setup() {
// create output directory if it does not exist yet
if !dirExists(*outputdir) {
err := os.Mkdir(*outputdir, 0755)
checkError(err)
}
if *cachelocation == "" {
*cachelocation = *outputdir + "/" + "joncalhoun-dl-cache"
}
// create cache location if it does not exist
if !dirExists(*cachelocation) {
err := os.Mkdir(*cachelocation, 0755)
checkError(err)
}
}
func signin(client *http.Client) {
// Login and create session
fmt.Println("[joncalhoun-dl]: signing in...")
_, err := client.PostForm("https://courses.calhoun.io/signin", url.Values{
"email": {*email},
"password": {*password},
})
checkError(err)
fmt.Println("[joncalhoun-dl]: sign in successful")
}
func getCourseHTML(url string, client *http.Client) {
// Make a Get Request to the course URL and fetch the HTML
// user must be logged in
fmt.Printf("[joncalhoun-dl]: fetching data for %s...\n", url)
res, err := client.Get(url)
checkError(err)
defer res.Body.Close()
// Write data to file
saveHTMLContent(*course+".html", res.Body)
}
func getURLs(client *http.Client) []string {
fmt.Printf("[joncalhoun-dl]: fetching video urls for %s\n", *course)
var urls []string
var file *os.File
var err error
// check if course page is cached
if isCached(*course + ".html") {
fmt.Printf("[joncalhoun-dl]: loading %s data from cache \n", *course)
file, err = loadFromCache(*course + ".html")
checkError(err)
} else {
// fecth from remote if not cached
fmt.Printf("[joncalhoun-dl]: fetching %s data from remote\n", *course)
res, err := client.Get(courses[*course])
checkError(err)
defer res.Body.Close()
// cache raw HTML data
getCourseHTML(courses[*course], client)
file, err = loadFromCache(*course + ".html")
checkError(err)
}
doc, err := goquery.NewDocumentFromReader(file)
checkError(err)
// parses the HTML tree to extract url
// where the lesson video is located
doc.Find("a").Each(func(i int, s *goquery.Selection) {
href, _ := s.Attr("href")
switch *course {
case "testwithgo":
// each lesson link should contain this substring
// else ignore
if strings.Contains(href, "/lessons/les_twg") {
urls = append(urls, "https://courses.calhoun.io"+href)
}
case "gophercises":
// each lesson link should contain this substring
// else ignore
if strings.Contains(href, "/lessons/les_goph") {
urls = append(urls, "https://courses.calhoun.io"+href)
}
case "webdevwithgo":
if strings.Contains(href, "/lessons/les_wd") {
urls = append(urls, "https://courses.calhoun.io"+href)
}
case "advancedwebdevwithgo":
log.Fatal("'Advanced Web Development with Go' not supported yet")
case "algorithmswithgo":
if strings.Contains(href, "/lessons/les_algo") {
urls = append(urls, "https://courses.calhoun.io"+href)
}
default:
log.Fatal("course not supported yet. feel free to send a pull request")
}
})
videoURLs := []string{}
for _, url := range urls {
videoURLs = append(videoURLs, getVideoURL(url, client))
// we don't want to send too many requests in a short time
// this naively simulates human behaviour
fmt.Printf("[joncalhoun-dl]: waiting 5 seconds\n")
time.Sleep(time.Duration(delayDuration) * time.Second)
}
return videoURLs
}
func getVideoURL(url string, client *http.Client) string {
fmt.Printf("[joncalhoun-dl]: fetching video url for lesson %s\n", url)
var videoID string
var file *os.File
var err error
// check cache for existing webpage
name := strings.Split(url, "/")[4]
filename := name + ".html"
if isCached(filename) {
fmt.Printf("[joncalhoun-dl]: loading %s from cache\n", name)
file, err = loadFromCache(filename)
checkError(err)
// no need to delay when loading from cash
delayDuration = 0
} else {
// fetch web page where video lives
fmt.Printf("[joncalhoun-dl]: fetching %s from remote\n", filename)
res, err := client.Get(url)
checkError(err)
defer res.Body.Close()
// To provide caching support we save the resulting
// html in the cache folder
saveHTMLContent(filename, res.Body)
file, err = loadFromCache(filename)
delayDuration = 5
}
// convert return data to parsable HTML Document
doc, err := goquery.NewDocumentFromReader(file)
checkError(err)
iframe := doc.Find("iframe")
videoID, _ = iframe.Attr("src")
fmt.Printf("[joncalhoun-dl]:[video ID] %s\n", videoID)
return videoID
}
func saveHTMLContent(filename string, r io.Reader) {
f, err := os.Create(*cachelocation + "/" + filename)
checkError(err)
defer f.Close()
filewriter := bufio.NewWriter(f)
_, err = filewriter.ReadFrom(r)
checkError(err)
filewriter.Flush()
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func dirExists(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
func isCached(name string) bool {
if fileExists(*cachelocation + "/" + name) {
return true
}
return false
}
func loadFromCache(name string) (*os.File, error) {
return os.OpenFile(*cachelocation+"/"+name, os.O_RDWR, 0666)
}
func isSupported(coursename string) bool {
if courses[coursename] != "" {
return true
}
return false
}