2024-09-24 06:17:11 -04:00
|
|
|
package crawler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-11-28 17:04:28 -05:00
|
|
|
"encoding/json"
|
2024-09-24 06:17:11 -04:00
|
|
|
"fmt"
|
2024-12-04 12:36:55 -05:00
|
|
|
"net/url"
|
2024-11-28 17:04:28 -05:00
|
|
|
"pcgamedb/cache"
|
2024-11-22 10:50:36 -05:00
|
|
|
"pcgamedb/db"
|
2024-09-24 06:17:11 -04:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2024-11-20 06:09:04 -05:00
|
|
|
"pcgamedb/constant"
|
|
|
|
"pcgamedb/model"
|
|
|
|
"pcgamedb/utils"
|
2024-11-15 02:02:45 -05:00
|
|
|
|
2024-09-24 06:17:11 -04:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2024-12-02 03:17:01 -05:00
|
|
|
func GetSteam250(URL string) ([]*model.GameInfo, error) {
|
2024-12-04 12:36:55 -05:00
|
|
|
key := "steam250:" + url.QueryEscape(URL)
|
|
|
|
if val, ok := cache.Get(key); ok {
|
|
|
|
var infos []*model.GameInfo
|
|
|
|
err := json.Unmarshal([]byte(val), &infos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return infos, nil
|
|
|
|
}
|
|
|
|
|
2024-12-02 03:17:01 -05:00
|
|
|
resp, err := utils.Request().Get(URL)
|
2024-09-24 06:17:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-02 03:17:01 -05:00
|
|
|
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
|
2024-09-24 06:17:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var rank []model.Steam250Item
|
|
|
|
var item model.Steam250Item
|
|
|
|
steamIDs := make([]int, 0)
|
|
|
|
doc.Find(".appline").Each(func(i int, s *goquery.Selection) {
|
|
|
|
item.Name = s.Find(".title>a").First().Text()
|
|
|
|
idStr := s.Find(".store").AttrOr("href", "")
|
|
|
|
idSlice := regexp.MustCompile(`app/(\d+)/`).FindStringSubmatch(idStr)
|
|
|
|
if len(idSlice) < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item.SteamID, _ = strconv.Atoi(idSlice[1])
|
|
|
|
rank = append(rank, item)
|
|
|
|
steamIDs = append(steamIDs, item.SteamID)
|
|
|
|
})
|
2024-11-22 10:50:36 -05:00
|
|
|
infos, err := db.GetGameInfosByPlatformIDs("steam", steamIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
2024-11-22 14:16:49 -05:00
|
|
|
if len(infos) > 10 {
|
|
|
|
return infos[:10], nil
|
|
|
|
}
|
2024-12-04 12:36:55 -05:00
|
|
|
|
|
|
|
jsonBytes, err := json.Marshal(infos)
|
|
|
|
if err == nil {
|
|
|
|
_ = cache.SetWithExpire(key, string(jsonBytes), 12*time.Hour)
|
|
|
|
}
|
|
|
|
|
2024-11-22 14:16:49 -05:00
|
|
|
return infos, nil
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetSteam250Top250() ([]*model.GameInfo, error) {
|
|
|
|
return GetSteam250(constant.Steam250Top250URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSteam250BestOfTheYear() ([]*model.GameInfo, error) {
|
|
|
|
return GetSteam250(fmt.Sprintf(constant.Steam250BestOfTheYearURL, time.Now().UTC().Year()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSteam250WeekTop50() ([]*model.GameInfo, error) {
|
|
|
|
return GetSteam250(constant.Steam250WeekTop50URL)
|
|
|
|
}
|
|
|
|
|
2024-11-28 05:37:01 -05:00
|
|
|
func GetSteam250MonthTop50() ([]*model.GameInfo, error) {
|
|
|
|
return GetSteam250(constant.Steam250MonthTop50URL)
|
|
|
|
}
|
|
|
|
|
2024-09-24 06:17:11 -04:00
|
|
|
func GetSteam250MostPlayed() ([]*model.GameInfo, error) {
|
|
|
|
return GetSteam250(constant.Steam250MostPlayedURL)
|
|
|
|
}
|