package main import ( "encoding/csv" "fmt" "io/ioutil" "log" "net/http" "os" "sort" "strconv" "strings" "golang.org/x/text/encoding/charmap" ) type params struct { URL string Region string Operator string Comment bool Prefix string Suffix string Group bool } func main() { p := readArgs() var values [][]string if p.Region != "" { values = filterRegion(parse(getCodes(p.URL)), p.Region) } else { values = parse(getCodes(p.URL)) } if p.Operator != "" { values = filterOperator(values, p.Operator) } if p.Group { sort.Slice(values, func(i, j int) bool { return values[i][4] < values[j][4] }) } op := "" for _, v := range values { _, min, max, dif := convert(v) if !validate(min, max, dif) { fmt.Printf("wrong interval: from %d to %d != %d\n", min, max, dif) continue } if p.Comment { fmt.Printf("; %v, %v, %v, %v, %v, %v\n", v[0], v[1], v[2], v[3], v[4], v[5]) } if p.Group { if v[4] != op { fmt.Printf("; %s\n", v[4]) op = v[4] } } if len([]rune(v[0])) != 3 || len([]rune(v[1])) != 7 || len([]rune(v[2])) != 7 { fmt.Printf("wrong interval: from %d to %d != %d\n", min, max, dif) continue } compute(p.Prefix+v[0], v[1], v[2], ""+p.Suffix) } } func getCodes(url string) string { var client http.Client res, err := client.Get(url) if err != nil { log.Panic(err) } defer res.Body.Close() if res.StatusCode == http.StatusOK { b, err := ioutil.ReadAll(res.Body) if err != nil { log.Panic(err) } dec := charmap.Windows1251.NewDecoder() body := make([]byte, len(b)*2) n, _, err := dec.Transform(body, []byte(b), false) if err != nil { log.Println(err) } return string(body[:n]) } log.Panic("bad response") return "" }