-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathfetch.go
216 lines (198 loc) · 4.64 KB
/
fetch.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
package food
import (
"encoding/json"
"fmt"
"github.com/huichen/sego"
"gopkg.in/mgo.v2/bson"
"log"
"net/url"
"os"
"strings"
"sync"
"time"
)
const sectionSize = 1000
var insertMap = make(map[int]section)
type section struct {
sec *[sectionSize]bool
lock sync.Mutex
}
func noUseWord(word string) bool {
mp := map[string]bool{
"(": true,
")": true,
"°": true,
",": true,
}
_, ok := mp[word]
return ok
}
func hasInsert(id int) bool {
sectionIndex := id / sectionSize
offset := id - sectionSize*sectionIndex
sec, ok := insertMap[sectionIndex]
if !ok {
insertMap[sectionIndex] = section{sec: &[sectionSize]bool{}, lock: sync.Mutex{}}
sec = insertMap[sectionIndex]
}
return sec.sec[offset]
}
func markInsert(id int) {
sectionIndex := id / sectionSize
offset := id - sectionSize*sectionIndex
sec, ok := insertMap[sectionIndex]
if !ok {
insertMap[sectionIndex] = section{sec: &[sectionSize]bool{}}
sec = insertMap[sectionIndex]
}
sec.lock.Lock()
sec.sec[offset] = true
sec.lock.Unlock()
}
func fetchCats() {
foodCh := make(chan SJson, 5)
go receiveFood(foodCh)
for cat := 1; cat <= 12; cat++ {
for page := 1; page <= 10; page++ {
go fetchList(cat, page, foodCh)
}
}
}
// 搜索关键词循环
func searchKeywordLoop() {
taskCh := make(chan string, 10)
taskOkCh := make(chan string, 10)
foodCh := make(chan SJson, 10)
go receiveFood(foodCh)
go receiveSearchTask(taskCh, taskOkCh, foodCh)
for {
var words []interface{}
err := WordsCollection.Find(bson.M{"searched": false}).Sort("-_id").Limit(100).All(&words)
if err != nil {
log.Println(err)
time.Sleep(1 * time.Second)
continue
}
for _, word := range words {
taskCh <- word.(bson.M)["_id"].(string)
}
<-waitSearchKeywordDone(taskOkCh, len(words), func(s string) {
WordsCollection.Upsert(bson.M{"_id": s}, bson.M{"searched": true})
})
}
}
// 等待一轮关键词搜索完成
func waitSearchKeywordDone(taskOkCh chan string, num int, cb func(string)) chan bool {
done := make(chan bool)
go func() {
for i := 0; i < num; i++ {
str := <-taskOkCh
cb(str)
}
done <- true
}()
return done
}
// 搜索一个关键词
func receiveSearchTask(taskCh, taskOkCh chan string, foodCh chan SJson) {
for {
keyword := <-taskCh
go func(keyword string) {
totalPage := 10
for page := 1; page <= totalPage; page++ {
u := fmt.Sprintf("https://food.boohee.com/fb/v1/search?q=%s&page=%d", url.QueryEscape(keyword), page)
str, err := cli.Get(u, 2)
if err != nil {
log.Println(err)
continue
}
log.Println(u, str)
var j SJson
err = json.Unmarshal([]byte(str), &j)
if err != nil {
log.Println(err)
continue
}
if p, ok := j["total_pages"]; ok {
p1 := int(p.(float64))
if p1 < totalPage {
totalPage = p1
}
}
if items, ok := j["items"]; ok {
for _, item := range items.([]interface{}) {
foodCh <- SJson(item.(map[string]interface{}))
}
}
}
taskOkCh <- keyword
}(keyword)
}
}
// 食物条目处理器
func receiveFood(ch chan SJson) {
// 关键词通道
wordsCh := make(chan []string, 10)
go receiveWords(wordsCh) // 关键词处理协程
// 分词器
var segmenter sego.Segmenter
segmenter.LoadDictionary(strings.Split(os.Getenv("GOPATH"), ":")[0] + "/src/github.com/huichen/sego/data/dictionary.txt")
segmenter.Dictionary()
for {
food := <-ch
if id, ok := food["id"]; ok {
idInt := int(id.(float64))
if !hasInsert(idInt) {
delete(food, "id")
FoodsCollection.Upsert(bson.M{"_id": idInt}, food)
markInsert(idInt)
// 分词
if name, ok := food["name"]; ok {
segments := segmenter.Segment([]byte(name.(string)))
output := sego.SegmentsToSlice(segments, false)
wordsCh <- output
}
}
}
}
}
// 关键字处理
func receiveWords(ch chan []string) {
dealt := make(map[string]bool)
for {
words := <-ch
for _, word := range words {
_, ok := dealt[word]
if len(word) <= 1 || ok || noUseWord(word) {
continue
}
c, err := WordsCollection.FindId(word).Count()
if c == 0 {
err = WordsCollection.Insert(bson.M{"_id": word, "searched": false})
if err == nil {
}
}
dealt[word] = true
}
}
}
func fetchList(cat, page int, foodCh chan SJson) error {
url := fmt.Sprintf("https://food.boohee.com/fb/v1/foods?value=%d&kind=group&page=%d", cat, page)
//log.Println(url)
str, err := cli.Get(url, 2)
if err != nil {
return err
}
log.Println(url, str)
var j SJson
err = json.Unmarshal([]byte(str), &j)
if err != nil {
return err
}
if foods, ok := j["foods"]; ok {
for _, food := range foods.([]interface{}) {
foodCh <- SJson(food.(map[string]interface{}))
}
}
return nil
}