2024-09-24 06:17:11 -04:00
|
|
|
|
package crawler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2024-11-20 06:09:04 -05:00
|
|
|
|
"pcgamedb/db"
|
|
|
|
|
"pcgamedb/model"
|
|
|
|
|
"pcgamedb/utils"
|
2024-11-15 02:02:45 -05:00
|
|
|
|
|
2024-11-28 05:37:01 -05:00
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
2024-09-24 06:17:11 -04:00
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func GenerateGameInfo(platform string, id int) (*model.GameInfo, error) {
|
|
|
|
|
switch platform {
|
|
|
|
|
case "steam":
|
|
|
|
|
return GenerateSteamGameInfo(id)
|
|
|
|
|
case "igdb":
|
|
|
|
|
return GenerateIGDBGameInfo(id)
|
|
|
|
|
default:
|
2024-11-21 12:30:26 -05:00
|
|
|
|
return nil, errors.New("invalid ID type")
|
2024-09-24 06:17:11 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 05:37:01 -05:00
|
|
|
|
// OrganizeGameItem Organize game item and save game info to database
|
2024-11-21 12:30:26 -05:00
|
|
|
|
func OrganizeGameItem(game *model.GameItem) error {
|
|
|
|
|
hasOriganized, _ := db.HasGameItemOrganized(game.ID)
|
|
|
|
|
if hasOriganized {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 05:37:01 -05:00
|
|
|
|
item, err := OrganizeGameItemWithIGDB(game)
|
2024-09-24 06:17:11 -04:00
|
|
|
|
if err == nil {
|
|
|
|
|
if item.SteamID == 0 {
|
2024-11-16 00:48:48 -05:00
|
|
|
|
// get steam id from igdb
|
2024-09-24 06:17:11 -04:00
|
|
|
|
steamID, err := GetSteamIDByIGDBIDCache(item.IGDBID)
|
|
|
|
|
if err == nil {
|
|
|
|
|
item.SteamID = steamID
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-21 12:30:26 -05:00
|
|
|
|
err = db.SaveGameInfo(item)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2024-09-24 06:17:11 -04:00
|
|
|
|
}
|
2024-11-21 12:30:26 -05:00
|
|
|
|
return err
|
2024-09-24 06:17:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AddGameInfoManually(gameID primitive.ObjectID, platform string, plateformID int) (*model.GameInfo, error) {
|
|
|
|
|
info, err := GenerateGameInfo(platform, plateformID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
info.GameIDs = append(info.GameIDs, gameID)
|
|
|
|
|
info.GameIDs = utils.Unique(info.GameIDs)
|
|
|
|
|
return info, db.SaveGameInfo(info)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-16 00:48:48 -05:00
|
|
|
|
func OrganizeGameItemManually(gameID primitive.ObjectID, platform string, platformID int) (*model.GameInfo, error) {
|
2024-09-24 06:17:11 -04:00
|
|
|
|
info, err := db.GetGameInfoByPlatformID(platform, platformID)
|
|
|
|
|
if err != nil {
|
2024-11-21 12:30:26 -05:00
|
|
|
|
if errors.Is(err, mongo.ErrNoDocuments) {
|
2024-09-24 06:17:11 -04:00
|
|
|
|
info, err = AddGameInfoManually(gameID, platform, platformID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
info.GameIDs = append(info.GameIDs, gameID)
|
|
|
|
|
info.GameIDs = utils.Unique(info.GameIDs)
|
|
|
|
|
err = db.SaveGameInfo(info)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if platform == "igdb" {
|
|
|
|
|
steamID, err := GetSteamIDByIGDBIDCache(platformID)
|
|
|
|
|
if err == nil {
|
|
|
|
|
info.SteamID = steamID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if platform == "steam" {
|
2024-11-28 05:37:01 -05:00
|
|
|
|
igdbID, err := GetIGDBIDBySteamAppIDCache(platformID)
|
2024-09-24 06:17:11 -04:00
|
|
|
|
if err == nil {
|
|
|
|
|
info.IGDBID = igdbID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return info, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FormatName(name string) string {
|
|
|
|
|
name = regexp.MustCompile(`(?i)[\w’'-]+\s(Edition|Vision|Collection|Bundle|Pack|Deluxe)`).ReplaceAllString(name, " ")
|
|
|
|
|
name = regexp.MustCompile(`(?i)GOTY`).ReplaceAllString(name, "")
|
|
|
|
|
name = regexp.MustCompile(`(?i)nsw for pc`).ReplaceAllString(name, "")
|
2024-11-21 12:30:26 -05:00
|
|
|
|
name = regexp.MustCompile(`\([^)]+\)`).ReplaceAllString(name, "")
|
2024-09-24 06:17:11 -04:00
|
|
|
|
name = regexp.MustCompile(`\s+`).ReplaceAllString(name, " ")
|
|
|
|
|
name = strings.Replace(name, ": Remastered", "", -1)
|
|
|
|
|
name = strings.Replace(name, ": Remaster", "", -1)
|
|
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
|
name = strings.Trim(name, ":")
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-16 00:48:48 -05:00
|
|
|
|
func SupplementPlatformIDToGameInfo(logger *zap.Logger) error {
|
2024-09-24 06:17:11 -04:00
|
|
|
|
infos, err := db.GetAllGameInfos()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, info := range infos {
|
|
|
|
|
changed := false
|
|
|
|
|
if info.IGDBID != 0 && info.SteamID == 0 {
|
|
|
|
|
steamID, err := GetSteamIDByIGDBIDCache(info.IGDBID)
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
info.SteamID = steamID
|
|
|
|
|
changed = true
|
|
|
|
|
}
|
|
|
|
|
if info.SteamID != 0 && info.IGDBID == 0 {
|
2024-11-28 05:37:01 -05:00
|
|
|
|
igdbID, err := GetIGDBIDBySteamAppIDCache(info.SteamID)
|
2024-09-24 06:17:11 -04:00
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
info.IGDBID = igdbID
|
|
|
|
|
changed = true
|
|
|
|
|
}
|
|
|
|
|
if changed {
|
2024-11-28 05:37:01 -05:00
|
|
|
|
logger.Info("supp", zap.String("name", info.Name), zap.Int("igdb", info.IGDBID), zap.Int("steam", info.SteamID))
|
2024-09-24 06:17:11 -04:00
|
|
|
|
_ = db.SaveGameInfo(info)
|
2024-11-28 05:37:01 -05:00
|
|
|
|
} else {
|
|
|
|
|
logger.Info("skip", zap.String("name", info.Name), zap.Int("igdb", info.IGDBID), zap.Int("steam", info.SteamID))
|
2024-09-24 06:17:11 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-11-28 05:37:01 -05:00
|
|
|
|
|
|
|
|
|
func UpdateGameInfo(num int) (chan *model.GameInfo, error) {
|
|
|
|
|
infos, err := db.GetOutdatedGameInfos(num)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
updateChan := make(chan *model.GameInfo)
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
for _, info := range infos {
|
|
|
|
|
if info.IGDBID != 0 {
|
|
|
|
|
newInfo, err := GenerateIGDBGameInfo(info.IGDBID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
db.MergeGameInfo(info, newInfo)
|
|
|
|
|
err = db.SaveGameInfo(newInfo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
updateChan <- newInfo
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return updateChan, nil
|
|
|
|
|
}
|