Skip to content

Commit

Permalink
[player] added ratings to wishlist
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed Jul 14, 2024
1 parent 9e7e6df commit a4524d1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/servus-player/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func main() {
g.GET(
"/finderuser/:id",
middleware.Id,
middleware.Force,
adapter.A(GetFinderUserWishlist),
middleware.Result,
)
Expand Down
30 changes: 26 additions & 4 deletions cmd/servus-player/wishlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func GetFinderUserWishlist(req *model.Map, res *model.Map) error {
return err
}

force, err := req.GetBool("force")
if err != nil {
return err
}

if id == "" {
return errors.New("username is not valid")
}
Expand Down Expand Up @@ -120,8 +125,25 @@ func GetFinderUserWishlist(req *model.Map, res *model.Map) error {
wishlist = rs
}

if len(wishlist) > 0 {
raw, err := json.Marshal(wishlist)
if len(wishlist) > 0 || force {
ratings, err := bgg.GetRatingsFromBgg(id)
if err != nil {
return err
}

rs := []bgg.WishlistItem{}
for _, item := range wishlist {
for _, rating := range ratings {
if rating.Id == int(item.ObjectId) {
item.UserRating = rating.Rating
break
}
}

rs = append(rs, item)
}

raw, err := json.Marshal(rs)
if err != nil {
return err
}
Expand All @@ -137,11 +159,11 @@ func GetFinderUserWishlist(req *model.Map, res *model.Map) error {
}

res.Set("synced", true)
res.Set("data", rs)
} else {
res.Set("synced", false)
res.Set("data", wishlist)
}

res.Set("data", wishlist)

return nil
}
63 changes: 63 additions & 0 deletions pkg/bgg/ratings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package bgg

import (
"fmt"
"regexp"
"strings"

"github.com/DictumMortuum/servus-extapi/pkg/util"
"github.com/gocolly/colly/v2"
)

type Rating struct {
Id int
Name string
Rating float64
}

func GetRatingsFromBgg(name string) ([]Rating, error) {
rs := map[string]Rating{}
retval := []Rating{}

collector := colly.NewCollector(
colly.AllowedDomains("boardgamegeek.com"),
colly.UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"),
colly.CacheDir("/tmp/scrape"),
)

collector.OnHTML("#collectionitems tr", func(e *colly.HTMLElement) {
name := e.ChildText(".primary")
href := e.ChildAttr(".primary", "href")
re := regexp.MustCompile("[0-9]+")
id := re.FindAllString(href, -1)
rating := util.Atof(e.ChildText(".rating"))

if len(id) == 1 {
rs[name] = Rating{
Name: name,
Rating: rating,
Id: util.Atoi(id[0]),
}
}
})

collector.OnHTML(".geekpages", func(e *colly.HTMLElement) {
raw := e.ChildText("a:last-child")
n := util.Atoi(raw)

for i := 2; i <= n; i++ {
url := fmt.Sprintf("https://boardgamegeek.com/collection/user/%s?pageID=%d&rated=1", strings.Replace(name, " ", "%20", -1), i)
collector.Visit(url)
}
})

url := fmt.Sprintf("https://boardgamegeek.com/collection/user/%s?rated=1", strings.Replace(name, " ", "%20", -1))
collector.Visit(url)
collector.Wait()

for _, val := range rs {
retval = append(retval, val)
}

return retval, nil
}
9 changes: 5 additions & 4 deletions pkg/bgg/wishlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ type Status struct {

type WishlistItem struct {
// XMLName xml.Name `xml:"item" json:"-"`
ObjectId int64 `xml:"objectid,attr" json:"id"`
Name string `xml:"name" json:"name"`
Status Status `xml:"status"`
Stats Info `json:"stats"`
ObjectId int64 `xml:"objectid,attr" json:"id"`
Name string `xml:"name" json:"name"`
Status Status `xml:"status"`
Stats Info `json:"stats"`
UserRating float64 `json:"user_rating"`
}

type WishlistRs struct {
Expand Down
12 changes: 12 additions & 0 deletions pkg/middleware/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ func Id(c *gin.Context) {
m.Set("id", id)
}

func Force(c *gin.Context) {
f := c.Query("force")

m, err := model.ToMap(c, "req")
if err != nil {
c.Error(err)
return
}

m.Set("force", f == "true")
}

func Url(c *gin.Context) {
m, err := model.ToMap(c, "req")
if err != nil {
Expand Down

0 comments on commit a4524d1

Please sign in to comment.