forked from LeOndaz/qurandw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
120 lines (94 loc) · 2.4 KB
/
utils.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
package utils
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
)
func ExpectUint() (int, error) {
fmt.Println("option:")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
err := scanner.Err()
if err != nil {
log.Fatal(err)
}
scannerText := scanner.Text()
value, err := strconv.Atoi(scannerText)
if err != nil {
return -1, err
}
return value, nil
}
func GetRequest(url string, queryParams map[string]string) (*http.Response, error) {
var queryParamsArray []string
for key, value := range queryParams {
queryParamsArray = append(queryParamsArray, fmt.Sprintf("%s=%s", key, value))
}
queryString := strings.Join(queryParamsArray, "&")
if queryString != "" {
url = fmt.Sprintf("%s?%s", url, queryString)
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
return resp, nil
}
func DisplayProgressBar(progressData *Progress) {
currentProgress := progressData.CurrentProgress
totalProgress := progressData.TotalProgress
percentage := currentProgress * 100 / totalProgress
barWidth := 50
completed := int(float64(percentage) / 100 * float64(barWidth))
remaining := barWidth - completed
bar := "[" + strings.Repeat("=", completed) + strings.Repeat("-", remaining) + "]"
bar += fmt.Sprintf("%d/%d", progressData.CurrentProgress, progressData.TotalProgress)
fmt.Print("\r")
fmt.Print(bar)
}
func DownloadFile(download *FileDownload, progress *Progress) error {
resp, err := GetRequest(download.Url, map[string]string{})
if err != nil {
return err
}
defer resp.Body.Close()
outputFile, err := os.Create(download.OutputPath)
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer outputFile.Close()
progressWriter := &ProgressWriter{
Progress: progress,
}
_, err = io.Copy(
outputFile,
io.TeeReader(
resp.Body,
progressWriter,
),
)
if err != nil {
return fmt.Errorf("error copying response body: %v", err)
}
fmt.Printf(" ---> File downloaded successfully to: %s\n", download.OutputPath)
return nil
}
type ProgressWriter struct {
Progress *Progress
}
func (pw *ProgressWriter) Write(p []byte) (int, error) {
n := len(p)
pw.Progress.Update(int64(n))
DisplayProgressBar(pw.Progress)
return n, nil
}