init
This commit is contained in:
55
internal/moc.go
Normal file
55
internal/moc.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"hakushi-crawl/api"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func SaveDataMOCEvent(locale string) error {
|
||||
listItem, err := api.GetListMOCEventApi()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mapItemDetail := make(map[string]*any)
|
||||
|
||||
var mu sync.Mutex
|
||||
|
||||
jobs := make(chan string)
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
workerCount := 12
|
||||
|
||||
for range workerCount {
|
||||
wg.Go(func() {
|
||||
for itemId := range jobs {
|
||||
itemDetail, err := api.GetMOCEventInfoApi(itemId, locale)
|
||||
if err != nil || itemDetail == nil {
|
||||
continue
|
||||
}
|
||||
mu.Lock()
|
||||
mapItemDetail[itemId] = itemDetail
|
||||
mu.Unlock()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
for itemId := range listItem {
|
||||
jobs <- itemId
|
||||
}
|
||||
close(jobs)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
fileName := fmt.Sprintf("moc.%s.json", locale)
|
||||
|
||||
data, err := json.Marshal(mapItemDetail)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(fileName, data, 0644)
|
||||
}
|
||||
Reference in New Issue
Block a user