-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
142 lines (124 loc) · 3.16 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"os"
)
// Check if a slice contains a string
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
// Return GET http.Request with provided url
func createRequest(url string) *http.Request {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal("createRequest error occurred")
}
return req
}
// Call getJsonToTarget and decode JSON-encoded value into some interface
func decodeJsonToTarget(url string, target interface{}) {
if err := getJsonToTarget(url, target); err != nil {
log.Fatal("Can't decode json")
}
}
// Sends an HTTP request, return JSON-encoded value
func getJsonToTarget(url string, target interface{}) error {
client := &http.Client{}
res, err := client.Do(createRequest(url))
if err != nil {
log.Fatal("Possible no internet connection")
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal("res.Body.Close() error occurred")
}
}(res.Body)
return json.NewDecoder(res.Body).Decode(target)
}
// Get randomJokeCategory and assigns it to map
// Concurrent version using channels
func jokesCategoriesToMapConcurrent(jokesCount int, categories []string) map[string][]string {
categoriesJokes := map[string][]string{}
categoriesChan := make(map[string]chan []string, len(categories))
for _, category := range categories {
categoriesChan[category] = make(chan []string, 1)
go func(category string) {
var jokes []string
for retryCount := 0; len(jokes) < jokesCount; retryCount++ {
joke := randomJokeCategory(category)
if contains(jokes, joke) {
if retryCount == jokeGetRetryCount {
break
}
retryCount++
continue
}
retryCount = 0
jokes = append(jokes, joke)
}
categoriesChan[category] <- jokes
close(categoriesChan[category])
}(category)
}
for _, category := range categories {
jokes, ok := <-categoriesChan[category]
if ok {
categoriesJokes[category] = jokes
}
}
return categoriesJokes
}
// Get randomJokeCategory and assigns it to map
func jokesCategoriesToMap(jokesCount int, categories []string) map[string][]string {
categoriesJokes := map[string][]string{}
for _, category := range categories {
var jokes []string
for retryCount := 0; len(jokes) < jokesCount; retryCount++ {
joke := randomJokeCategory(category)
if contains(jokes, joke) {
if retryCount == jokeGetRetryCount {
break
}
retryCount++
continue
}
retryCount = 0
jokes = append(jokes, joke)
}
categoriesJokes[category] = jokes
}
return categoriesJokes
}
// Write dataMap value to a file in the following format: <key>.txt
func writeMapToFile(dataMap map[string][]string) error {
for key, slice := range dataMap {
err := os.MkdirAll("jokes", os.ModePerm)
if err != nil {
return err
}
file, err := os.Create("jokes/" + key + ".txt")
if err != nil {
return err
}
for index, value := range slice {
// Add a newline except the last one
if index != len(slice)-1 {
value = value + "\n"
}
_, err := file.WriteString(value)
if err != nil {
return err
}
}
}
return nil
}