pcgamedb/cmd/organize.go
nite07 ed12329be7 add import
fix organize log
change crawl task trigger body
fix CleanOrphanGamesInGameInfos delete all data
2024-11-17 13:29:04 +08:00

49 lines
1.2 KiB
Go

package cmd
import (
"github.com/nitezs/pcgamedb/crawler"
"github.com/nitezs/pcgamedb/db"
"github.com/nitezs/pcgamedb/log"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var organizeCmd = &cobra.Command{
Use: "organize",
Long: "Organize game info by repack game's name",
Short: "Organize game info by repack game's name",
Run: organizeRun,
}
type organizeCommandConfig struct {
Num int
}
var organizeCmdCfg organizeCommandConfig
func init() {
organizeCmd.Flags().IntVarP(&organizeCmdCfg.Num, "num", "n", -1, "number of items to process")
RootCmd.AddCommand(organizeCmd)
}
func organizeRun(cmd *cobra.Command, args []string) {
games, err := db.GetUnorganizedGameItems(organizeCmdCfg.Num)
if err != nil {
log.Logger.Error("Failed to get games", zap.Error(err))
}
for _, game := range games {
gameInfo, err := crawler.OrganizeGameItem(game)
if err == nil {
err = db.SaveGameInfo(gameInfo)
if err != nil {
log.Logger.Error("Failed to save game info", zap.Error(err))
continue
}
log.Logger.Info("Organized game", zap.String("name", game.Name))
} else {
log.Logger.Error("Failed to organize game", zap.String("name", game.Name))
}
}
}