forked from reusee/taobao-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgcats.go
101 lines (92 loc) · 2.22 KB
/
bgcats.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
package taobao
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
func CollectBackgroundCategories(backend Backend) {
//clientSet := NewClientSet()
//defer clientSet.Close()
//clientSet.Logger = backend.LogClient
client := http.DefaultClient
cookie, err := ioutil.ReadFile("cookie")
ce(err, "get cookie")
var collect func(string, string)
collect = func(path string, sid string) {
if sid != "" {
cat, err := strconv.Atoi(sid)
ce(err, "strconv")
t, err := backend.GetBgCatLastUpdated(cat)
ce(err, "get last updated time")
if time.Now().Sub(t) < time.Hour*24*5 {
pt("skip %d\n", cat)
return
}
}
addr := "http://upload.taobao.com/auction/json/reload_cats.htm?customId="
req, err := http.NewRequest("POST", addr, strings.NewReader(url.Values{
"path": {path},
"sid": {sid},
}.Encode()))
ce(err, "new request")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Cookie", string(cookie))
resp, err := client.Do(req)
ce(err, "do")
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
ce(err, "read body")
r := transform.NewReader(bytes.NewReader(content), simplifiedchinese.GBK.NewDecoder())
content, err = ioutil.ReadAll(r)
ce(err, "conv gbk")
var data []struct {
Data []struct {
Data []struct {
Sid string
Name string
}
}
}
err = json.Unmarshal(content, &data)
ce(err, "unmarshal")
if len(data) == 0 {
return
}
for _, c := range data[0].Data {
for _, row := range c.Data {
if strings.Contains(row.Sid, ":") { // pv
continue
}
pt("%s %s\n", row.Sid, row.Name)
parent := 0
if sid != "" {
parent, err = strconv.Atoi(sid)
ce(err, "strconv")
}
cat, err := strconv.Atoi(row.Sid)
ce(err, "strconv")
backend.AddBgCat(Cat{
Cat: cat,
Name: row.Name,
Parent: parent,
})
collect("next", row.Sid)
}
}
if sid != "" {
cat, err := strconv.Atoi(sid)
ce(err, "strconv")
err = backend.SetBgCatLastUpdated(cat, time.Now())
ce(err, "set bgcat last updated")
}
return
}
collect("all", "")
}