/
shop_grabber.go
101 lines (77 loc) · 2.27 KB
/
shop_grabber.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 grabber
import (
"context"
"github.com/pdcgo/go_v2_shopeelib/app/upload_app/legacy_source"
"github.com/pdcgo/go_v2_shopeelib/lib/legacy"
"github.com/pdcgo/tokopedia_lib/lib/grabber/filter"
"github.com/pdcgo/tokopedia_lib/lib/grabber/iterator"
"github.com/pdcgo/tokopedia_lib/lib/model_public"
)
type ShopGrabber struct {
*BaseGrabber
}
func NewShopGrabber(base *BaseGrabber) *ShopGrabber {
return &ShopGrabber{
base,
}
}
func (g *ShopGrabber) Run() error {
fname := g.Base.Path(g.GrabTasker.TokoUsername)
filterText := legacy_source.NewFilterText(g.Base)
grabBasic := legacy.NewGrabBasic(g.Base)
grabTokopedia := legacy.NewGrabTokopedia(g.Base)
markupConfig := legacy.NewLegacyMarkupConfigWithBase(g.Base)
filtersOpt := filter.NewGrabFilterBundle(g.Api, g.Base, filterText, grabBasic, grabTokopedia, markupConfig)
return iterator.IterateShops(g.Api, fname, func(shopCore *model_public.ShopCoreInfoResp) error {
filterLimit, addCount := filter.CreateLimiter(grabBasic)
filters := []filter.FilterHandler{
filterLimit,
}
if g.GrabTasker.UseFilter {
filters = append(filters, filtersOpt...)
}
shopId := shopCore.Data.Result[0].ShopCore.ShopID
searchVar := model_public.NewShopProductVar(shopId)
ctx, cancel := context.WithCancel(context.Background())
filterItem := filter.NewFilterItem(ctx, filters...)
err := iterator.IterateProductShopPage(g.Api, ctx, searchVar, func(items []*model_public.ShopProductData) error {
var urls []string
for _, item := range items {
urls = append(urls, item.ProductURL)
}
return iterator.GetBatchLayout(g.Api, ctx, urls, func(layout *model_public.PdpGetlayoutQueryResp) error {
g.wg.Add(1)
g.limitGuard <- 1
go func() {
defer g.wg.Done()
go func() {
<-g.limitGuard
}()
pdp := g.GetPdpDataP2(ctx, layout)
if pdp == nil {
return
}
filtered, finished := g.ApplyFilter(ctx, filterItem, layout, pdp)
if finished {
cancel()
}
if filtered {
return
}
g.limitLock.Lock()
defer g.limitLock.Unlock()
saved := g.SaveItem(ctx, layout, pdp)
if saved {
limitReached := addCount()
if limitReached {
cancel()
}
}
}()
return nil
})
})
g.wg.Wait()
return err
})
}