@@ -10,60 +10,77 @@ import (
10
10
"path/filepath"
11
11
"strings"
12
12
"sync"
13
+ "time"
13
14
14
15
"github.com/coderj001/go-wallheven/src"
15
16
)
16
17
17
- const numGoroutines = src .BASE_CONFIG .NUMGOROUTINES
18
- const dir = src .BASE_CONFIG .DIR
19
-
20
- func DownloadImgs (searchList src.SearchList ) error {
18
+ func Downloader (searchList * src.SearchList ) {
21
19
var wg sync.WaitGroup
22
20
wg .Add (len (searchList .Data ))
23
- for i := 0 ; i < numGoroutines ; i ++ {
24
- go func (i int ){
25
- for j := i ; i < len (searchList .Data ); j += numGoroutines {
26
- return nil
27
- }
21
+ completed := make (chan bool )
22
+
23
+ for i := 0 ; i < len (searchList .Data ); i ++ {
24
+ go downloadImage (& searchList .Data [i ], & wg , completed )
25
+ }
26
+
27
+ // Code for spinner - it takes
28
+ spinner := []string {"█" , "▒" , "▓" , "░" }
29
+ spinnerIndex := 0
30
+ completedCount := 0
31
+
32
+ for completedCount < len (searchList .Data ) {
33
+ select {
34
+ case <- completed :
35
+ completedCount ++
36
+ spinnerIndex = (spinnerIndex + 1 ) % 4
37
+ fmt .Printf ("\r Downloading... %s %d/%d" , spinner [spinnerIndex ], completedCount , len (searchList .Data ))
38
+ default :
39
+ time .Sleep (100 * time .Millisecond )
40
+ spinnerIndex = (spinnerIndex + 1 ) % 4
41
+ fmt .Printf ("\r Downloading... %s %d/%d" , spinner [spinnerIndex ], completedCount , len (searchList .Data ))
28
42
}
29
43
}
30
- return nil
44
+
45
+ wg .Wait ()
46
+ close (completed )
31
47
}
32
48
33
- func downloadImage (imgInfo src.ImageInfo , wg * sync.WaitGroup ) {
49
+ func downloadImage (imgInfo * src.ImageInfo , wg * sync.WaitGroup , completed chan bool ) {
34
50
defer wg .Done ()
35
-
51
+ dir := src .BASE_CONFIG .DIR
52
+
36
53
resp , err := http .Get (imgInfo .Path )
37
54
if err != nil {
38
55
log .Printf ("Error while downloading (%s): %s\n " , imgInfo .Url , err )
39
56
return
40
57
}
41
58
defer resp .Body .Close ()
42
-
59
+
43
60
// check the dir is exists or not
44
- if _ , err := os .Stat (dir ); errors .Is (err , os .ErrNotExist ){
45
- err := os .Mkdir (dir , os .ModePerm )
61
+ if _ , err := os .Stat (dir ); errors .Is (err , os .ErrNotExist ) {
62
+ err := os .Mkdir (dir , os .ModePerm )
46
63
if err != nil {
47
64
log .Printf ("Error while creating dir (%s): %s\n " , dir , err )
48
65
return
49
66
}
50
67
}
51
68
52
69
// create the file to store download content
53
-
54
70
filename := strings .Join ([]string {dir , strings .Join ([]string {imgInfo .Id , filepath .Ext (imgInfo .Path )}, "" )}, "/" )
55
71
fileOut , err := os .Create (filename )
56
72
if err != nil {
57
73
log .Printf ("Unable to create file (%s): %s\n " , imgInfo .Id , err )
58
74
return
59
75
}
60
76
defer fileOut .Close ()
61
-
77
+
62
78
_ , err = io .Copy (fileOut , resp .Body )
63
79
if err != nil {
64
80
log .Printf ("Error while writing content to file: %s\n " , err )
65
81
return
66
82
}
67
-
68
- fmt .Printf ("File downloaded successfully: %s\n " , imgInfo .ShortUrl )
69
- }
83
+
84
+ // fmt.Printf("File downloaded successfully: %s\n", imgInfo.ShortUrl)
85
+ completed <- true
86
+ }
0 commit comments