-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (112 loc) · 2.38 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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
)
func downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func getPage(url string) (resp *http.Response) {
resp, err := http.Get(url)
if err != nil {
fmt.Println("Website is giving us issues, retrying.")
time.Sleep(2 * time.Second)
resp = getPage(url)
}
return
}
func digest4chanPage(url string) (imgList []string, title string) {
doc, err := goquery.NewDocument(url)
if err != nil {
panic(err)
}
doc.Find("span.subject").Each(func(i int, s *goquery.Selection) {
fmt.Println(s.Text())
title = s.Text()
})
doc.Find("div.fileText a").Each(func(i int, s *goquery.Selection) {
imgURL, _ := s.Attr("href")
imgURL = strings.Replace(imgURL, "//", "http://", 100)
imgList = append(imgList, imgURL)
})
return imgList, title
}
func safeStringToFilepath(badString string) (filepath string) {
badChars := []string{
"'",
`"`,
`\`,
`/`,
"#",
"!",
"?",
"(",
")",
}
for _, badChar := range badChars {
badString = strings.Replace(badString, badChar, "", -1)
}
filepath = strings.Replace(badString, " ", "_", -1)
return
}
//func verboseWait
func main() {
threadURL := os.Args[1]
preImgList, title := digest4chanPage(threadURL)
imgList := []string{}
re := regexp.MustCompile(`\d{10,30}\.\w*`)
downloadDir := safeStringToFilepath(title)
os.MkdirAll(downloadDir, os.FileMode(0777))
files, _ := ioutil.ReadDir(downloadDir)
// Skip files we've already downloaded
for _, item := range preImgList {
skip := false
for _, file := range files {
if strings.Contains(item, file.Name()) {
skip = true
}
}
if !skip {
imgList = append(imgList, item)
}
}
var wg sync.WaitGroup
wg.Add(len(imgList))
for _, item := range imgList {
go func(item string) {
filename := re.FindString(item)
filepath := downloadDir + "/" + filename
downloadFile(filepath, item)
wg.Done()
}(item)
//time.Sleep(100 * time.Millisecond)
}
wg.Wait()
fmt.Println(len(imgList))
//fmt.Println(os.Args[1])
}