-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (85 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
package main
import (
"encoding/json"
"fmt"
"gowall/utils"
"net/http"
"strconv"
"sync"
)
type pic struct {
name string
url string
}
func GetPics(page int, pics *[]pic) {
counter := 0
var url string
if api_key != "" {
url = fmt.Sprintf("%s/%s?categories=%s&atleast=%s&ratios=%s&topRange=%s&sorting=%s&page=%s&purity=%s&apikey=%s&order=desc&ai_art_filter=1",
site, mode, categories, atleast, ratios, topRange, sorting, strconv.Itoa(page), purity, api_key)
} else {
url = fmt.Sprintf("%s/%s?categories=%s&atleast=%s&ratios=%s&topRange=%s&sorting=%s&page=%s&purity=%s&order=desc&ai_art_filter=1",
site, mode, categories, atleast, ratios, topRange, sorting, strconv.Itoa(page), purity)
}
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
}
var json_data map[string]interface{}
json.NewDecoder(resp.Body).Decode(&json_data)
data := json_data["data"].([]interface{})
for _, value := range data {
counter++
file_url, ok := value.(map[string]interface{})["path"].(string)
if !ok {
continue
}
file_name := strconv.Itoa(counter)
*pics = append(*pics, pic{name: file_name, url: file_url})
}
}
func DownlaodInParallel(pics []pic, save_directory string, pic_num int) {
var wg sync.WaitGroup
sem := make(chan bool, 3) // Limit to 3 parallel downloads
counter := 0
for _, item := range pics {
wg.Add(1)
go func(counter int, pic pic) {
defer wg.Done()
sem <- true // Will block if there are already 3 downloads in progress
file := strconv.Itoa(counter + 1)
err := utils.DownloadFile(save_directory+"/"+file+".jpg", pic.url)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Downloaded: ", counter+1)
}
<-sem // Release a slot
}(counter, item)
counter++
if counter >= pic_num {
break
}
}
wg.Wait() // Wait for all downloads to finish
}
func main() {
pics := []pic{}
utils.CreateDirectory(save_directory)
var err error
var total_pic int
fmt.Print("Enter an integer for number of wallpaper that you want downlaod: ")
_, err = fmt.Scan(&total_pic)
if err != nil {
panic(err)
}
page := 0
for {
if len(pics) >= total_pic {
break
}
page++
GetPics(page, &pics)
}
DownlaodInParallel(pics, save_directory, total_pic)
}