This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (75 loc) · 1.72 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
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/sclevine/agouti"
)
func main() {
var url string
fmt.Print("Enter site url: ")
fmt.Scanf("%s\n", &url)
driver := agouti.ChromeDriver(
agouti.ChromeOptions("args", []string{"--headless", "--disable-gpu", "--no-sandbox", "--log-level=3"}),
)
if err := driver.Start(); err != nil {
log.Fatal("Failed to start driver:", err)
}
page, err := driver.NewPage()
if err != nil {
log.Fatal("Failed to open page:", err)
}
if err := page.Navigate(url); err != nil {
log.Fatal("Failed to navigate:", err)
}
vid, err := page.Find(`video`).Attribute("src")
log.Println(vid == "")
if err != nil {
log.Fatal("Failed search source tag")
}
if vid == "" {
log.Println("Failed search video tag. Trying searcg source tag")
vid, err := page.Find(`source`).Attribute("src")
if err != nil {
log.Fatal("Failed search source tag")
}
log.Println(vid)
download(vid)
} else {
log.Println(vid)
download(vid)
}
if err := driver.Stop(); err != nil {
log.Fatal("Failed to close pages and stop WebDriver:", err)
}
}
func download(url string) {
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
var filename = getFileName(url)
if _, err := os.Stat(filename); os.IsNotExist(err) {
out, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
defer out.Close()
io.Copy(out, res.Body)
fmt.Println("Saved file ", filename)
} else {
fmt.Println(filename, " already exist")
}
}
func getFileName(url string) string {
hasher := md5.New()
io.WriteString(hasher, url)
var hashStr = hex.EncodeToString(hasher.Sum(nil)) + ".mp4"
fmt.Printf("%#v", hashStr)
return hashStr
}