-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
157 lines (131 loc) · 3.25 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
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
)
var (
sections = [...]string{
"shop/kupit-mate-2.html",
"shop/paragvayskiy-mate.html",
"shop/brazilskiy-mate.html",
}
sections_FIXME = [...]string{
"category_148_show_all.html",
"category_72_show_all.html",
"category_54_show_all.html",
"category_68_show_all.html",
}
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
start := time.Now()
wg := new(sync.WaitGroup)
ch := make(chan string)
wg.Add(len(sections))
for _, v := range sections {
go grabSection(makeCompleteURL(v), wg, ch)
}
results := make([]string, 0, 100)
go func(wg *sync.WaitGroup) {
for v := range ch {
if strings.HasPrefix(v, "+") {
results = append(results, strings.Replace(v, "+", "", 1))
} else {
wg.Add(1)
go grabPage(v, wg, ch)
}
}
}(wg)
wg.Wait()
close(ch)
// It is filter output for poors :)
var filterText string
if len(os.Args) > 1 {
filterText = os.Args[1]
}
fmt.Printf("%s\n", makeCompleteURL(""))
var count int
for _, v := range results {
if strings.Contains(strings.ToLower(v), strings.ToLower(filterText)) {
fmt.Println(v)
count++
}
}
fmt.Printf("Count: %d Elapsed time: %s\n", count, time.Since(start).String())
//FIXME
start = time.Now()
results = results[0:0]
fmt.Printf("%s\n", makeCompleteURL(""))
fmt.Printf("Count: %d Elapsed time: %s\n", count, time.Since(start).String())
}
func grabSection(url string, wg *sync.WaitGroup, c chan<- string) {
defer wg.Done()
d, err := goquery.NewDocument(url)
panicIfError(err)
d.Find("#content table a").Each(func(i int, s *goquery.Selection) {
if l, ok := s.Attr("href"); ok && strings.HasPrefix(l, "http") {
c <- l
}
})
}
func grabPage(url string, wg *sync.WaitGroup, c chan<- string) {
defer wg.Done()
b := new(bytes.Buffer)
r, err := http.Get(url)
panicIfError(err)
defer r.Body.Close()
_, err = io.Copy(b, r.Body)
panicIfError(err)
d, err := goquery.NewDocumentFromReader(convRdrWin1251toUTF8(b))
panicIfError(err)
d.Find("div h3 a").Each(func(i int, s *goquery.Selection) {
price := ""
s.Parent().Parent().Find("span").Each(func(i int, s *goquery.Selection) {
price = strings.TrimSpace(s.Text())
})
exists := false
s.Parent().Parent().Find(".inputbox").Each(func(i int, s *goquery.Selection) {
exists = true
})
if exists {
// The prefix "+" here - it is small workaround
c <- "+" + strings.TrimSpace(s.Text()) + " " + price
}
})
}
func grabPage_FIXME(url string, wg *sync.WaitGroup, c chan<- string) {
//
}
func makeCompleteURL(url string) string {
return fmt.Sprintf("http://mate-kiev.com/%s", url)
}
func makeCompleteURL_FIXME(url string) string {
return fmt.Sprintf("http://www.mate-tea.in.ua/%s", url)
}
func convRdrWin1251toUTF8(r io.Reader) io.Reader {
return transform.NewReader(r, charmap.Windows1251.NewDecoder())
}
func convStrWin1251toUTF8(s string) string {
b := new(bytes.Buffer)
r := transform.NewReader(strings.NewReader(s), charmap.Windows1251.NewEncoder())
_, err := io.Copy(b, r)
panicIfError(err)
return b.String()
}
func panicIfError(err error) {
if err != nil {
panic(err)
}
}