-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (171 loc) · 6.65 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
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
package main
import (
"fmt"
"reflect"
"runtime"
"strconv"
"sync"
"time"
"github.com/qiusnay/3dorderquery/model"
"github.com/qiusnay/3dorderquery/service"
"github.com/qiusnay/3dorderquery/util"
)
func main() {
Log := util.NewLogger("main")
model.DbStart()
model.RedisStart()
var wg sync.WaitGroup
wg.Add(6)
c := ZfyxSdk{}
//京东订单抓取 3分钟 - 过去1小时
go c.JdOrderFetchBy3min(3)
//拼多多订单抓取 3分钟 - 过去1小时
go c.PddOrderFetchBy3min(3)
//京东商城抓商品 30分钟 - 最新 50条
go c.FetchJdGoodsBy30Min(30)
//拼多多商城抓商品 30分钟 - 最新500条
go c.FetchPddGoodsBy30Min(30)
//订单同步 - 京东 1分钟
go c.SyncJdOrderFromOriginal(1)
//订单同步 - 拼多多 1分钟
go c.SyncPddOrderFromOriginal(1)
Log.Info(fmt.Sprintf("作业启动,当前协程数 : " + strconv.Itoa(runtime.NumGoroutine())))
wg.Wait()
}
type ZfyxSdk struct{}
//京东订单同步 1分钟
func (z *ZfyxSdk) SyncJdOrderFromOriginal(TimeMinutes int) {
log := util.NewLogger("sync_order_jd")
defer func() {
if err := recover(); err != nil {
log.Error(fmt.Sprintf("京东订单同步异常,错误信息 : %v, 自动恢复 : %s", err, time.Now().Format("2006-01-02 15:04:05")))
log.Error(util.PanicTrace(err))
z.RestartRoutine("SyncJdOrderFromOriginal", TimeMinutes)
}
}()
for {
log.Info(fmt.Sprintf("开始京东订单同步,当前时间 : " + time.Now().Format("2006-01-02 15:04:05")))
timer1 := time.NewTimer(time.Second * time.Duration(60*TimeMinutes))
<-timer1.C
//京东订单同步tb_dingdan,tb_dingdan_items
syncJdOrderModel := new(service.JdOrderCreate)
syncJdOrderModel.Sync()
}
}
//拼多多订单同步 1分钟
func (z *ZfyxSdk) SyncPddOrderFromOriginal(TimeMinutes int) {
log := util.NewLogger("sync_order_pdd")
defer func() {
if err := recover(); err != nil {
log.Error(fmt.Sprintf("拼多多订单同步异常,错误信息 : %v, 自动恢复 : %s", err, time.Now().Format("2006-01-02 15:04:05")))
log.Error(util.PanicTrace(err))
z.RestartRoutine("SyncPddOrderFromOriginal", TimeMinutes)
}
}()
for {
log.Info(fmt.Sprintf("开始拼多多订单同步,当前时间 : " + time.Now().Format("2006-01-02 15:04:05")))
timer1 := time.NewTimer(time.Second * time.Duration(60*TimeMinutes))
<-timer1.C
//拼多多订单同步tb_dingdan,tb_dingdan_items
syncPddOrderModel := new(service.PddOrderCreate)
syncPddOrderModel.Sync()
}
}
//拼多多抓商品 30min
//频道id:
//0-1.9包邮, 1-今日爆款, 2-品牌清仓,3-相似商品推荐,4-猜你喜欢,5-实时热销,6-实时收益,7-今日畅销,8-高佣榜单,默认1
func (z *ZfyxSdk) FetchPddGoodsBy30Min(TimeMinutes int) {
log := util.NewLogger("fetch_pdd_goods")
defer func() {
if err := recover(); err != nil {
log.Error(fmt.Sprintf("拼多多商品抓取异常,错误信息 : %v, 自动恢复 : %s", err, time.Now().Format("2006-01-02 15:04:05")))
log.Error(util.PanicTrace(err))
z.RestartRoutine("FetchPddGoodsBy30Min", TimeMinutes)
}
}()
for {
log.Info(fmt.Sprintf("开始拼多多商品抓取,当前时间 : " + time.Now().Format("2006-01-02 15:04:05")))
timer1 := time.NewTimer(time.Second * time.Duration(60*TimeMinutes))
<-timer1.C
ShopSdk := new(service.PddItemsdk)
for _, brand := range []int{0, 1, 2, 3, 4, 5, 6, 7, 8} {
FetchUrl := ShopSdk.FetchPddItems(brand)
log.Info(fmt.Sprintf("当前抓取地址: %s", FetchUrl))
}
}
}
//京东商城抓商品
//频道id:
//1-好券商品,2-超级大卖场,10-9.9专区,22-热销爆品,23-为你推荐,24-数码家电,25-超市,26-母婴玩具,27-家具日用,28-美妆穿搭,
//29-医药保健,30-图书文具,31-今日必推,32-品牌好货,33-秒杀商品,34-拼购商品,109-新品首发,110-自营,125-首购商品,129-高佣榜单,130-视频商品
func (z *ZfyxSdk) FetchJdGoodsBy30Min(TimeMinutes int) {
log := util.NewLogger("fetch_jd_goods")
defer func() {
if err := recover(); err != nil {
log.Error(fmt.Sprintf("京东商品抓取异常,错误信息 : %v, 自动恢复 : %s", err, time.Now().Format("2006-01-02 15:04:05")))
log.Error(util.PanicTrace(err))
z.RestartRoutine("FetchJdGoodsBy30Min", TimeMinutes)
}
}()
for {
log.Info(fmt.Sprintf("开始京东商品抓取,当前时间 : " + time.Now().Format("2006-01-02 15:04:05")))
timer1 := time.NewTimer(time.Second * time.Duration(60*TimeMinutes))
<-timer1.C
ShopSdk := new(service.JdItemsdk)
for _, brand := range []int{1, 2, 10, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 109, 110, 125, 129, 130} {
FetchUrl := ShopSdk.FetchJdItems(brand, log)
log.Info(fmt.Sprintf("当前抓取地址: %s", FetchUrl))
}
}
}
//京东订单抓取 3分钟
func (z *ZfyxSdk) JdOrderFetchBy3min(TimeMinutes int) {
log := util.NewLogger("fetch_jd_order")
defer func() {
if err := recover(); err != nil {
log.Error(fmt.Sprintf("京东订单抓取异常,错误信息 : %v, 自动恢复 : %s", err, time.Now().Format("2006-01-02 15:04:05")))
log.Error(util.PanicTrace(err))
z.RestartRoutine("JdOrderFetchBy3min", TimeMinutes)
}
}()
for {
log.Info(fmt.Sprintf("开始京东订单抓取,当前时间 : " + time.Now().Format("2006-01-02 15:04:05")))
timer1 := time.NewTimer(time.Second * time.Duration(60*TimeMinutes))
<-timer1.C
//京东订单抓单
// starttime := time.Now().Add(-time.Minute * 60).Format("2006-01-02 15:04:05")
// endtime := time.Now().Format("2006-01-02 15:04:05")
ShopSdk := new(service.Jdsdk)
FetchUrl := ShopSdk.FetchOrders("2020-10-15 11:04:05", "2020-10-15 12:00:05")
log.Info(fmt.Sprintf("当前抓取地址: %s", FetchUrl))
}
}
//拼多多订单抓取 3分钟
func (z *ZfyxSdk) PddOrderFetchBy3min(TimeMinutes int) {
curTime := time.Now().Format("2006-01-02 15:04:05")
log := util.NewLogger("fetch_pdd_order")
defer func() {
if err := recover(); err != nil {
log.Error(fmt.Sprintf("拼多多订单抓取异常,错误信息 : %v, 自动恢复 : %s", err, time.Now().Format("2006-01-02 15:04:05")))
log.Error(util.PanicTrace(err))
z.RestartRoutine("PddOrderFetchBy3min", TimeMinutes)
}
}()
for {
log.Info(fmt.Sprintf("开始拼多多订单抓取,当前时间 : " + curTime))
timer1 := time.NewTimer(time.Second * time.Duration(60*TimeMinutes))
<-timer1.C
//拼多多订单抓单
ShopSdk := new(service.Pddsdk)
startTime := time.Unix(int64((time.Now().Unix() - 60 * 60)), 0).Format("2006-01-02 15:04:05")
FetchUrl := ShopSdk.FetchOrders(startTime, curTime)
log.Info(fmt.Sprintf("当前抓取地址: %s", FetchUrl))
}
}
//反射调用
func (z *ZfyxSdk) RestartRoutine(Method string, TimeMinutes int) {
v := reflect.ValueOf(&ZfyxSdk{})
argsFunc := v.MethodByName(Method)
args := []reflect.Value{reflect.ValueOf(TimeMinutes)}
argsFunc.Call(args)
}