Skip to content

Commit

Permalink
[boardgames] added scrape info from bgg
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed Dec 23, 2023
1 parent 8979230 commit 57c780c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
76 changes: 76 additions & 0 deletions cmd/servus-boardgames/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"

"github.com/DictumMortuum/servus-extapi/pkg/model"
)

var (
re_json = regexp.MustCompile(`GEEK.geekitemPreload = (.*);`)
)

func GetBoardgameInfo(req *model.Map, res *model.Map) error {
id, err := req.GetInt64("id")
if err != nil {
return err
}

url := fmt.Sprintf("https://boardgamegeek.com/boardgame/%d", id)
resp, err := http.Get(url)
if err != nil {
return err
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}

res.SetInternal(nil)
sb := string(body)
refs := re_json.FindAllStringSubmatch(sb, -1)
if len(refs) > 0 {
var rs map[string]any
err = json.Unmarshal([]byte(refs[0][1]), &rs)
if err != nil {
return err
}

if val, ok := rs["item"]; ok {
raw, err := json.Marshal(val)
if err != nil {
return err
}

var inner map[string]any
err = json.Unmarshal([]byte(raw), &inner)
if err != nil {
return err
}

ignore := []string{
"wiki",
"description",
"itemdata",
"linkedforum_types",
"summary_video",
"promoted_ad",
"special_user",
"walmart_price",
}

for _, item := range ignore {
delete(inner, item)
}

res.SetInternal(inner)
}
}

return nil
}
9 changes: 8 additions & 1 deletion cmd/servus-boardgames/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func Version(c *gin.Context) {
rs := map[string]any{
"version": "v0.0.4",
"version": "v0.0.5",
}
c.AbortWithStatusJSON(200, rs)
}
Expand Down Expand Up @@ -49,5 +49,12 @@ func main() {
middleware.Result,
)

g.GET(
"/info/:id",
middleware.Id,
adapter.A(GetBoardgameInfo),
middleware.Result,
)

r.Run(":10002")
}
4 changes: 4 additions & 0 deletions pkg/model/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (m *Map) Set(key string, val any) {
m.Internal[key] = val
}

func (m *Map) SetInternal(val map[string]any) {
m.Internal = val
}

func (m *Map) GetString(key string) (string, error) {
return cast.ToStringE(m.Internal[key])
}
Expand Down

0 comments on commit 57c780c

Please sign in to comment.