-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
280 lines (226 loc) · 5.36 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"flag"
"fmt"
"github.com/xuri/excelize/v2"
"log"
"os"
"regexp"
"strconv"
"strings"
"sync"
"unicode"
)
const (
DataPath = "./data"
City = "Москва"
FromDate = 2021
ToDate = 2024
)
// FlagOptions define the options for flags.
//
// Path specifies the path for xlsx files folder.
//
// City specifies a city looking for.
//
// From describes the year the search starts from.
//
// To describes the last year in which the search ends.
type FlagOptions struct {
Path string
City string
From int
To int
}
func main() {
fmt.Println("Parsing flags...")
options := readFlags()
entries, err := os.ReadDir(options.Path)
if err != nil {
log.Fatal(err)
}
var allPhones []string
var muAllPhones sync.Mutex
var wgAllPhones sync.WaitGroup
wgAllPhones.Add(len(entries))
fmt.Println("Reading spreadsheets...")
for _, entry := range entries {
go appendAllNumbers(&wgAllPhones, &muAllPhones, entry, &allPhones, options)
}
wgAllPhones.Wait()
fmt.Println("Removing repetitions...")
err = removeRepetitions(&allPhones)
if err != nil {
log.Fatal(err)
}
fmt.Println("Writing result xlsx file...")
err = writeResultPhones(&allPhones)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Done: %d phones parsed.\n", len(allPhones))
}
func writeResultPhones(phones *[]string) error {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
log.Fatal(err)
}
}()
index, err := f.NewSheet("Sheet1")
if err != nil {
return err
}
f.SetActiveSheet(index)
for i, phone := range *phones {
err := f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i+1), phone)
if err != nil {
return err
}
}
if err := f.SaveAs("./result/base.xlsx"); err != nil {
return err
}
return nil
}
// readFlags read flags (path, city, from and to).
func readFlags() FlagOptions {
dataPath := flag.String("path", DataPath, "Path for xlsx files")
city := flag.String("city", City, "City")
fromDate := flag.Int("from", FromDate, "From date")
toDate := flag.Int("to", ToDate, "To date")
flag.Parse()
return FlagOptions{*dataPath, *city, *fromDate, *toDate}
}
// readSpreadsheets reading spreadsheets in xlsx format
func readSpreadsheets(fileName string, options FlagOptions) []string {
f, err := excelize.OpenFile(fmt.Sprintf("%s/%s", options.Path, fileName))
if err != nil {
panic(err)
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
var result []string
var mu sync.Mutex
cols, err := f.GetCols("Лист1")
phones := cols[0]
cities := cols[1]
dates := cols[2]
var wg sync.WaitGroup
wg.Add(len(phones))
for i := 0; i < len(phones); i++ {
go appendColumnNumbers(cities[i], dates[i], phones[i], &result, &wg, &mu, options)
}
wg.Wait()
return result
}
// appendAllNumbers append to array string all numbers from all entries in directory.
func appendAllNumbers(wg *sync.WaitGroup, mu *sync.Mutex, entry os.DirEntry, allPhones *[]string, options FlagOptions) {
defer wg.Done()
if !entry.IsDir() {
phones := readSpreadsheets(entry.Name(), options)
mu.Lock()
*allPhones = append(*allPhones, phones...)
mu.Unlock()
}
}
// appendColumnNumbers append all number in xlsx file column.
func appendColumnNumbers(
city string,
date string,
phone string,
res *[]string,
wg *sync.WaitGroup,
mutex *sync.Mutex,
options FlagOptions,
) {
defer wg.Done()
if city != options.City {
return
}
year, err := getLastOrderYear(date)
if err != nil {
return
}
if year < options.From || year > options.To {
return
}
parsedPhone, err := parsePhone(phone)
if err != nil {
return
}
mutex.Lock()
*res = append(*res, parsedPhone)
mutex.Unlock()
}
// parsePhone formatting phone number to format +X(XXX)XXX-XX-XX.
func parsePhone(phone string) (string, error) {
re := regexp.MustCompile("[^0-9]")
resultPhone := re.ReplaceAllString(phone, "")
for _, r := range resultPhone {
if !unicode.IsDigit(r) {
return "", fmt.Errorf("is not a digit")
}
}
if len(resultPhone) == 10 {
resultPhone = "8" + resultPhone
}
if len(resultPhone) == 11 {
if resultPhone[0] == '8' {
resultPhone = "7" + resultPhone[1:]
}
}
if len(resultPhone) != 11 {
return "", fmt.Errorf("invalid phone length")
}
if resultPhone[:2] != "79" {
return "", fmt.Errorf("invalid phone number")
}
resultPhone = fmt.Sprintf("+%s(%s)%s-%s-%s", resultPhone[:1], resultPhone[1:4], resultPhone[4:7], resultPhone[7:9], resultPhone[9:11])
return resultPhone, nil
}
// getLastOrderYear returning the last year when the order was carried out.
func getLastOrderYear(date string) (int, error) {
if date == "" {
return 0, fmt.Errorf("date is empty")
}
dateArr := strings.Split(date, "/")
if len(dateArr) != 3 {
return 0, fmt.Errorf("invalid date format")
}
intYear, err := strconv.Atoi(dateArr[2])
if err != nil {
return 0, err
}
return intYear, nil
}
// removeRepetitions removes similar phone numbers from string of array.
func removeRepetitions(phones *[]string) error {
uniquePhones := make(map[string]struct{})
chPhone := make(chan string)
var mu sync.Mutex
var result []string
go func() {
defer close(chPhone)
for _, phone := range *phones {
mu.Lock()
if _, ok := uniquePhones[phone]; !ok {
uniquePhones[phone] = struct{}{}
chPhone <- phone
}
mu.Unlock()
}
}()
for {
phone, opened := <-chPhone
if !opened {
break
}
result = append(result, phone)
}
*phones = result
return nil
}