2024-11-22 10:50:36 -05:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"pcgamedb/crawler"
|
|
|
|
"pcgamedb/db"
|
|
|
|
"pcgamedb/model"
|
2024-11-28 05:37:01 -05:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-11-22 10:50:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type GetPopularGamesResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
Games []*model.GameInfo `json:"games"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPopularGameInfosHandler Get popular games
|
|
|
|
// @Summary Get popular games
|
|
|
|
// @Description Get popular games based on a specified type
|
|
|
|
// @Tags popular
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2024-11-28 05:37:01 -05:00
|
|
|
// @Param type path string true "Type(igdb-most-visited, igdb-most-wanted-to-play, igdb-most-playing, igdb-most-played, steam-top, steam-week-top, steam-month-top, steam-best-of-the-year, steam-most-played)"
|
2024-11-22 10:50:36 -05:00
|
|
|
// @Success 200 {object} GetPopularGamesResponse
|
|
|
|
// @Failure 400 {object} GetPopularGamesResponse
|
|
|
|
// @Failure 500 {object} GetPopularGamesResponse
|
|
|
|
// @Router /popular/{type} [get]
|
|
|
|
func GetPopularGameInfosHandler(c *gin.Context) {
|
|
|
|
rankingType, exist := c.Params.Get("type")
|
|
|
|
if !exist {
|
|
|
|
c.JSON(http.StatusBadRequest, GetPopularGamesResponse{
|
|
|
|
Status: "error",
|
|
|
|
Message: "Missing ranking type",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
popularityType := 0
|
|
|
|
var steam250Func func() ([]*model.GameInfo, error) = nil
|
|
|
|
switch rankingType {
|
|
|
|
case "igdb-most-visited":
|
|
|
|
popularityType = 1
|
|
|
|
case "igdb-most-wanted-to-play":
|
|
|
|
popularityType = 2
|
|
|
|
case "igdb-most-playing":
|
|
|
|
popularityType = 3
|
|
|
|
case "igdb-most-played":
|
|
|
|
popularityType = 4
|
|
|
|
case "steam-top":
|
|
|
|
steam250Func = crawler.GetSteam250Top250
|
|
|
|
case "steam-week-top":
|
|
|
|
steam250Func = crawler.GetSteam250WeekTop50
|
|
|
|
case "steam-best-of-the-year":
|
|
|
|
steam250Func = crawler.GetSteam250BestOfTheYear
|
|
|
|
case "steam-most-played":
|
|
|
|
steam250Func = crawler.GetSteam250MostPlayed
|
2024-11-28 05:37:01 -05:00
|
|
|
case "steam-month-top":
|
|
|
|
steam250Func = crawler.GetSteam250MonthTop50
|
2024-11-22 10:50:36 -05:00
|
|
|
default:
|
|
|
|
c.JSON(http.StatusBadRequest, GetPopularGamesResponse{
|
|
|
|
Status: "error",
|
|
|
|
Message: "Invalid ranking type",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
var infos []*model.GameInfo
|
|
|
|
var err error
|
|
|
|
if steam250Func != nil {
|
|
|
|
infos, err = steam250Func()
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, GetPopularGamesResponse{
|
|
|
|
Status: "error",
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
2024-11-22 13:47:06 -05:00
|
|
|
if len(infos) > 10 {
|
|
|
|
infos = infos[:10]
|
|
|
|
}
|
2024-11-22 10:50:36 -05:00
|
|
|
} else {
|
|
|
|
offset := 0
|
|
|
|
for len(infos) < 10 {
|
|
|
|
ids, err := crawler.GetIGDBPopularGameIDs(popularityType, offset, 20)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, GetPopularGamesResponse{
|
|
|
|
Status: "error",
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
offset += 20
|
|
|
|
pids := make([]int, 20)
|
|
|
|
for _, id := range ids {
|
2024-12-04 12:36:55 -05:00
|
|
|
pid, err := crawler.GetIGDBAppParent(id)
|
2024-11-22 10:50:36 -05:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pids = append(pids, pid)
|
|
|
|
}
|
|
|
|
newInfos, err := db.GetGameInfosByPlatformIDs("igdb", pids)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, GetPopularGamesResponse{
|
|
|
|
Status: "error",
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
infos = append(infos, newInfos...)
|
|
|
|
}
|
2024-11-22 13:47:06 -05:00
|
|
|
if len(infos) > 10 {
|
|
|
|
infos = infos[:10]
|
|
|
|
}
|
2024-11-22 10:50:36 -05:00
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, GetPopularGamesResponse{
|
|
|
|
Status: "ok",
|
|
|
|
Games: infos,
|
|
|
|
})
|
|
|
|
}
|