-
Notifications
You must be signed in to change notification settings - Fork 65
/
autobuy.go
90 lines (78 loc) · 2.04 KB
/
autobuy.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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/adyzng/go-jd/core"
clog "gopkg.in/clog.v1"
)
func init() {
if err := clog.New(clog.CONSOLE, clog.ConsoleConfig{
Level: clog.INFO,
BufferSize: 100},
); err != nil {
fmt.Printf("init console log failed. error %+v.", err)
os.Exit(1)
}
}
const (
AreaBeijing = "1_72_2799_0"
)
var (
area = flag.String("area", AreaBeijing, "ship location string, default to Beijing")
period = flag.Int("period", 500, "the refresh period when out of stock, unit: ms.")
rush = flag.Bool("rush", false, "continue to refresh when out of stock.")
order = flag.Bool("order", false, "submit the order to JingDong when get the Goods.")
goods = flag.String("goods", "", `the goods you want to by, find it from JD website.
Single Goods:
2567304(:1)
Multiple Goods:
2567304(:1),3133851(:2)`)
)
func main() {
flag.Parse()
defer clog.Shutdown()
gs := parseGoods(*goods)
clog.Trace("[Area: %+v, Goods: %qv, Period: %+v, Rush: %+v, Order: %+v]",
*area, gs, *period, *rush, *order)
jd := core.NewJingDong(core.JDConfig{
Period: time.Millisecond * time.Duration(*period),
ShipArea: *area,
AutoRush: *rush,
AutoSubmit: *order,
})
defer jd.Release()
if err := jd.Login(); err == nil {
jd.RushBuy(gs)
}
}
// parseGoods parse the input goods list. Support to input multiple goods sperated
// by comma(,). With an (:count) after goods ID to specify the count of each goods.
//
// Example as following:
//
// 2567304 single goods with default count 1
// 2567304:3 single goods with count 3
// 2567304,3133851:4 multiple goods with defferent count 1, 4
// 2567304:2,3133851:5 ...
//
func parseGoods(goods string) map[string]int {
lst := make(map[string]int)
if goods == "" {
return lst
}
for _, good := range strings.Split(goods, ",") {
pair := strings.Split(good, ":")
name := strings.Trim(pair[0], " ")
if len(pair) == 2 {
v, _ := strconv.ParseInt(pair[1], 10, 32)
lst[name] = int(v)
} else {
lst[name] = 1
}
}
return lst
}