2024-09-24 06:17:11 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-11-20 06:09:04 -05:00
|
|
|
"pcgamedb/crawler"
|
|
|
|
"pcgamedb/db"
|
|
|
|
"pcgamedb/log"
|
2024-09-24 06:17:11 -04:00
|
|
|
|
|
|
|
"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) {
|
2024-11-16 00:48:48 -05:00
|
|
|
games, err := db.GetUnorganizedGameItems(organizeCmdCfg.Num)
|
2024-09-24 06:17:11 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Logger.Error("Failed to get games", zap.Error(err))
|
|
|
|
}
|
|
|
|
for _, game := range games {
|
2024-11-16 00:48:48 -05:00
|
|
|
gameInfo, err := crawler.OrganizeGameItem(game)
|
2024-09-24 06:17:11 -04:00
|
|
|
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))
|
2024-11-17 00:29:04 -05:00
|
|
|
} else {
|
|
|
|
log.Logger.Error("Failed to organize game", zap.String("name", game.Name))
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|