This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfgcats.go
77 lines (72 loc) · 1.54 KB
/
fgcats.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
package taobao
import (
"encoding/json"
"net/http"
"strconv"
"sync"
)
func CollectForegroundCategories(backend Backend) {
cats := make(map[int]Cat)
clientSet := NewClientSet()
var collectCategory func(Cat)
collectCategory = func(cat Cat) {
if _, ok := cats[cat.Cat]; ok {
return // skip
}
pt("%10d %s\n", cat.Cat, cat.Name)
var relatives []Cat
clientSet.Do(func(client *http.Client) ClientState {
var catStr string
if cat.Cat != 0 {
catStr = strconv.Itoa(cat.Cat)
}
bs, err := getBytes(client, sp("http://s.taobao.com/list?cat=%s", catStr))
if err != nil {
return Bad
}
jstr, err := GetPageConfigJson(bs)
if err != nil {
return Bad
}
var config PageConfig
err = json.Unmarshal(jstr, &config)
if err != nil {
return Bad
}
var nav NavData
err = json.Unmarshal(config.Mods["nav"].Data, &nav)
if err != nil {
return Bad
}
for _, e := range nav.Common {
if e.Text == "相关分类" {
for _, sub := range e.Sub {
id, err := strconv.Atoi(sub.Value)
ce(err, sp("parse cat id %s", sub.Value))
relatives = append(relatives, Cat{
Cat: id,
Name: sub.Text,
})
cat.Relatives = append(cat.Relatives, id)
}
}
}
return Good
})
if cat.Cat != 0 {
cats[cat.Cat] = cat
ce(backend.AddFgCat(cat), "add cat")
}
wg := new(sync.WaitGroup)
wg.Add(len(relatives))
for _, r := range relatives {
r := r
go func() {
defer wg.Done()
collectCategory(r)
}()
}
wg.Wait()
}
collectCategory(Cat{})
}