-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
2,681 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/http/cookiejar" | ||
"net/url" | ||
"os" | ||
"strings" | ||
|
||
"golang.org/x/net/publicsuffix" | ||
"github.com/PuerkitoBio/goquery" | ||
) | ||
|
||
var email = flag.String("email", "", "your email") | ||
var password = flag.String("password", "", "your password") | ||
var course = flag.String("course", "testwithgo", "course name") | ||
|
||
// 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", | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func main() { | ||
// Parse commanline options | ||
flag.Parse() | ||
|
||
// Cookiejar provides automatic cookie management | ||
// that would normally be aaccessed only via the browser | ||
// options := cookiejar.Options{ | ||
// PublicSuffixList: publicsuffix.List, | ||
// } | ||
// jar, err := cookiejar.New(&options) | ||
// checkError(err) | ||
|
||
// client := &http.Client{Jar: jar} | ||
|
||
// Login | ||
// res, client := signin(client) | ||
|
||
// Visit selected course page and fetch video paths | ||
// There are currently 4 different path pattern | ||
// `/les_twg_les_01` => for tESTwITHgO tests videos | ||
// `/les_form_les_01` => for tESTwITHgO form project videos | ||
// `/les_stripe_les_01` => for tESTwITHgO stripe project videos | ||
// `/les_swag_les_01` => for tESTwITHgO swag project videos | ||
// res, err := client.Get(courses[*course]) | ||
// checkError(err) | ||
getURLs() | ||
} | ||
|
||
func signin(client *http.Client) (*http.Response, *http.Client) { | ||
// Login and create session | ||
if *email == "" || *password == "" { | ||
log.Fatal(errors.New("[Error] try: 'go run main.go [email protected] -password=12345'")) | ||
} | ||
panic("Stop") | ||
options := cookiejar.Options{ | ||
PublicSuffixList: publicsuffix.List, | ||
log.Fatal(errors.New("[Error] try: 'go run main.go [email protected] --password=12345'")) | ||
} | ||
jar, err := cookiejar.New(&options) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
client := http.Client{Jar: jar} | ||
|
||
res, err := client.PostForm("https://courses.calhoun.io/signin", url.Values{ | ||
"email": {*email}, | ||
"password": {*password}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer res.Body.Close() | ||
checkError(err) | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
checkError(err) | ||
res.Body.Close() | ||
fmt.Println(string(body)) | ||
return res, client | ||
} | ||
|
||
func getCourseHTML(name string, res io.Reader) { | ||
f, err := os.Create(name) | ||
checkError(err) | ||
defer f.Close() | ||
writer := bufio.NewWriter(f) | ||
num, err := writer.ReadFrom(res) | ||
checkError(err) | ||
fmt.Printf("Wrote %d bytes", num) | ||
|
||
writer.Flush() | ||
} | ||
|
||
func getURLs() []string { | ||
var urls []string | ||
f, err := os.OpenFile("testwithgo.html", os.O_RDWR, 066) | ||
doc, err := goquery.NewDocumentFromReader(f) | ||
checkError(err) | ||
doc.Find("a").Each(func(i int, s *goquery.Selection) { | ||
href, _ := s.Attr("href") | ||
if strings.Contains(href, "/lessons/les_twg") { | ||
urls = append(urls, "https://courses.calhoun.io"+href) | ||
} | ||
}) | ||
return urls | ||
} |
Oops, something went wrong.