update
1. add cron module 2. add pool module 3. add tcgplayer background job 4. add card price model
This commit is contained in:
@@ -285,5 +285,48 @@ type TCGPlayerProductPrice struct {
|
||||
|
||||
// ListGroupProductPrice -
|
||||
func (p *TCGPlayer) ListGroupProductPrice(groupID int) (prices []*TCGPlayerProductPrice, err error) {
|
||||
return
|
||||
apiURL, err := p.getAPIURL(fmt.Sprintf("/pricing/group/%d", groupID), true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tmpStruct := struct {
|
||||
TCGPlayerAPIRes
|
||||
Results []*TCGPlayerProductPrice `json:"results"`
|
||||
}{}
|
||||
|
||||
reqObj := RequestObject{
|
||||
URL: apiURL,
|
||||
Headers: p.getHeader(),
|
||||
Method: "GET",
|
||||
}
|
||||
req, err := GetRequest(reqObj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respByte, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(respByte, &tmpStruct)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if tmpStruct.Success != true {
|
||||
if len(tmpStruct.Errors) > 0 {
|
||||
return nil, errors.New(tmpStruct.Errors[0])
|
||||
}
|
||||
return nil, errors.New("get product price fail")
|
||||
}
|
||||
|
||||
return tmpStruct.Results, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package background
|
||||
|
||||
import "github.com/robfig/cron"
|
||||
|
||||
var c *cron.Cron
|
||||
|
||||
// SetCronJob -
|
||||
func SetCronJob() {
|
||||
c = cron.New()
|
||||
|
||||
c.Start()
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package background
|
||||
|
||||
import (
|
||||
"git.trj.tw/golang/mtgbot/models"
|
||||
"git.trj.tw/golang/mtgbot/modules/apiact"
|
||||
"git.trj.tw/golang/mtgbot/modules/pool"
|
||||
"git.trj.tw/golang/utils"
|
||||
)
|
||||
|
||||
func runMTGPriceJob() {
|
||||
api, err := apiact.NewTCGApi("v1.19.0")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = api.GetToken()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
numSets, err := models.CountSets(models.ListSetsOpts{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sets, err := models.ListSets(models.ListSetsOpts{
|
||||
Limit: numSets,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_ = sets
|
||||
|
||||
pool := pool.NewPool(5)
|
||||
|
||||
for _, set := range sets {
|
||||
pool.Add()
|
||||
go getPriceFromAPI(pool, api, set.TCGPlayerID)
|
||||
}
|
||||
|
||||
pool.Wait()
|
||||
}
|
||||
|
||||
func getPriceFromAPI(pool *pool.Pool, api *apiact.TCGPlayer, groupid int) {
|
||||
defer pool.Done()
|
||||
|
||||
prices, err := api.ListGroupProductPrice(groupid)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ids := make([]int, 0, len(prices))
|
||||
|
||||
type cPrice struct {
|
||||
CardID string
|
||||
PriceN float32
|
||||
PriceF float32
|
||||
}
|
||||
|
||||
cardMap := make(map[int]cPrice)
|
||||
|
||||
for _, v := range prices {
|
||||
if utils.SliceIndex(len(ids), func(idx int) bool { return ids[idx] == v.ProductID }) == -1 {
|
||||
ids = append(ids, v.ProductID)
|
||||
}
|
||||
|
||||
s, ok := cardMap[v.ProductID]
|
||||
if !ok {
|
||||
s = cPrice{}
|
||||
}
|
||||
if v.SubTypeName == "Normal" {
|
||||
s.PriceN = v.MidPrice
|
||||
} else {
|
||||
s.PriceF = v.MidPrice
|
||||
}
|
||||
|
||||
cardMap[v.ProductID] = s
|
||||
}
|
||||
|
||||
cards, err := models.GetCardByTCGPlayerIDs(ids)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, v := range cards {
|
||||
if s, ok := cardMap[v.TCGPlayerID]; ok {
|
||||
s.CardID = v.ID
|
||||
cardMap[v.TCGPlayerID] = s
|
||||
}
|
||||
}
|
||||
|
||||
insOpts := models.InsertCardPriceOpts{}
|
||||
|
||||
insOpts.Data = make([]models.CardPrice, 0, len(cardMap))
|
||||
|
||||
for _, v := range cardMap {
|
||||
if len(v.CardID) > 0 {
|
||||
insOpts.Data = append(insOpts.Data, models.CardPrice{
|
||||
Card: v.CardID,
|
||||
PriceN: v.PriceN,
|
||||
PriceF: v.PriceF,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
models.InsertCardPrice(insOpts)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package pool
|
||||
|
||||
import "sync"
|
||||
|
||||
// Pool -
|
||||
type Pool struct {
|
||||
queue chan bool
|
||||
wg *sync.WaitGroup
|
||||
}
|
||||
|
||||
// NewPool -
|
||||
func NewPool(size int) *Pool {
|
||||
if size < 1 {
|
||||
size = 1
|
||||
}
|
||||
p := &Pool{
|
||||
queue: make(chan bool, size),
|
||||
wg: &sync.WaitGroup{},
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// Add -
|
||||
func (p *Pool) Add() {
|
||||
p.queue <- true
|
||||
p.wg.Add(1)
|
||||
}
|
||||
|
||||
// Done -
|
||||
func (p *Pool) Done() {
|
||||
<-p.queue
|
||||
p.wg.Done()
|
||||
}
|
||||
|
||||
// Wait -
|
||||
func (p *Pool) Wait() {
|
||||
p.wg.Wait()
|
||||
}
|
||||
Reference in New Issue
Block a user