-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (100 loc) · 2.2 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
package main
import (
"flag"
"fmt"
"github.com/gosuri/uiprogress"
"math"
"net/url"
"os"
"runtime"
"sync"
"zhihu/client"
)
var queryUrl *string
var saveImage *bool
func init() {
queryUrl = flag.String("u", "", "User home page")
saveImage = flag.Bool("d", false, "Save images")
}
func main() {
flag.Parse()
if queryUrl == nil || *queryUrl == "" {
fmt.Print("The URL is empty \n")
os.Exit(0)
}
u, err := url.Parse(*queryUrl)
if err != nil {
panic(err)
}
client, err := client.NewClient(*u)
if err != nil{
fmt.Printf("%v\n", err)
os.Exit(0)
}
if err := client.CreateDirectory(); err != nil {
panic(err)
}
imagesArr := make([]url.URL, 0)
var page uint64 = 1
fmt.Print("Parsing...\n")
for {
data, err := client.Query(page)
if err != nil {
fmt.Printf("Page acquisition failed: %v\n", err)
os.Exit(0)
}
count := client.GetAllAnswerCount(*data)
if count == nil {
fmt.Printf("Failed to get count: %v\n", err)
os.Exit(0)
}
images, err := client.GetImages(*data)
if err != nil {
fmt.Printf("Failed to get image: %v\n", err)
os.Exit(0)
} else {
for _, item := range images {
imagesArr = append(imagesArr, item)
}
}
allPageSize := uint64(math.Ceil(float64(*count) / 20))
if page >= allPageSize {
break
} else {
page += 1
}
}
imagesArr = client.RemoveDuplicate(imagesArr)
fmt.Printf("searched: %v pages, found: %v photos\n", page, len(imagesArr))
if saveImage != nil && *saveImage == true{
runtime.GOMAXPROCS(runtime.NumCPU())
bar := uiprogress.AddBar(len(imagesArr)).AppendCompleted().PrependElapsed()
bar.PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Saving (%d/%d)", b.Current(), len(imagesArr))
})
uiprogress.Start()
miao := make(chan url.URL, 0)
wg := sync.WaitGroup{}
for i := 0; i < runtime.NumCPU(); i++ {
go func(ch chan url.URL) {
for s := range ch {
if err := client.Download(s); err != nil {
fmt.Printf("%v\n", err)
}
wg.Done()
bar.Incr()
}
}(miao)
}
for _, item := range imagesArr {
miao <- item
wg.Add(1)
}
wg.Wait()
uiprogress.Stop()
}else {
if err := client.OutputTextFile(imagesArr); err != nil {
fmt.Printf("%v\n", err)
}
}
}