Skip to content

Commit

Permalink
[extapi] added original_price on tprice
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed Jan 1, 2025
1 parent f5149c2 commit ecc2d24
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 120 deletions.
2 changes: 1 addition & 1 deletion cmd/servus-extapi/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.38",
"version": "v0.0.39",
}
c.AbortWithStatusJSON(200, rs)
}
Expand Down
23 changes: 12 additions & 11 deletions pkg/model/prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import (
)

type Price struct {
Id int64 `gorm:"primaryKey" json:"id"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Name string `gorm:"index" json:"name"`
StoreId int64 `gorm:"foreignkey" json:"store_id"`
StoreThumb string `json:"store_thumb"`
Price float64 `json:"price"`
Stock int `json:"stock"`
Url string `json:"url"`
Deleted bool `json:"deleted"`
BoardgameId models.JsonNullInt64 `gorm:"foreignkey" json:"boardgame_id"`
Id int64 `gorm:"primaryKey" json:"id"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Name string `gorm:"index" json:"name"`
StoreId int64 `gorm:"foreignkey" json:"store_id"`
StoreThumb string `json:"store_thumb"`
Price float64 `json:"price"`
OriginalPrice float64 `json:"original_price"`
Stock int `json:"stock"`
Url string `json:"url"`
Deleted bool `json:"deleted"`
BoardgameId models.JsonNullInt64 `gorm:"foreignkey" json:"boardgame_id"`
}

func (Price) TableName() string {
Expand Down
103 changes: 42 additions & 61 deletions pkg/scrape/boardsofmadness.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,65 @@
package scrape

import (
"encoding/xml"
"io/ioutil"
"net/http"
)

type madnessRoot struct {
XMLName xml.Name `xml:"products"`
Products []madnessProduct `xml:"product"`
}

type madnessProduct struct {
XMLName xml.Name `xml:"product"`
SKU string `xml:"id"`
Name string `xml:"title"`
ThumbUrl string `xml:"image_link"`
Price string `xml:"price"`
Stock string `xml:"availability"`
Link string `xml:"link"`
}
"log"

func madnessAvailbilityToStock(s string) int {
switch s {
case "in stock":
return 0
case "on backorder":
return 1
case "out of stock":
return 2
default:
return 2
}
}
"github.com/gocolly/colly/v2"
)

func ScrapeBoardsOfMadness() (map[string]any, []map[string]any, error) {
store_id := int64(16)
rs := []map[string]any{}
detected := 0

link := "https://boardsofmadness.com/wp-content/uploads/woo-product-feed-pro/xml/sVVFMsJLyEEtvbil4fbIOdm8b4ha7ewz.xml"
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return nil, nil, err
}
collector := colly.NewCollector(
colly.AllowedDomains("boardsofmadness.com"),
user_agent,
colly.CacheDir(CacheDir),
)

conn := &http.Client{}
resp, err := conn.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
collector.OnHTML("li.product", func(e *colly.HTMLElement) {
raw_price := e.ChildText(".price > .amount > bdi")
if raw_price == "" {
raw_price = e.ChildText(".price > ins > .amount > bdi")
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
var stock int

root := madnessRoot{}
err = xml.Unmarshal(body, &root)
if err != nil {
return nil, nil, err
}
if hasClass(e, "instock") {
stock = 0
} else if hasClass(e, "onbackorder") {
stock = 1
} else if hasClass(e, "outofstock") {
stock = 2
}

for _, item := range root.Products {
item := map[string]any{
"name": item.Name,
"name": e.ChildText(".woocommerce-loop-product__title"),
"store_id": store_id,
"store_thumb": item.ThumbUrl,
"stock": madnessAvailbilityToStock(item.Stock),
"price": getPrice(item.Price),
"url": item.Link,
"store_thumb": e.ChildAttr(".attachment-woocommerce_thumbnail", "src"),
"stock": stock,
"price": getPrice(raw_price),
"url": e.Request.AbsoluteURL(e.ChildAttr(".woocommerce-LoopProduct-link", "href")),
}

rs = append(rs, item)
detected++
}
})

collector.OnHTML("a.next", func(e *colly.HTMLElement) {
link := e.Request.AbsoluteURL(e.Attr("href"))

if Debug {
log.Println("Visiting: " + link)
}

collector.Visit(link)
})

collector.Visit("https://boardsofmadness.com/product-category/epitrapezia-paixnidia/")
collector.Wait()

return map[string]interface{}{
"name": "Boards of Madness",
"id": store_id,
"scraped": detected,
"scraped": len(rs),
}, rs, nil
}
84 changes: 84 additions & 0 deletions pkg/scrape/boardsofmadnessXML.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package scrape

import (
"encoding/xml"
"io"
"net/http"
)

type madnessRoot struct {
XMLName xml.Name `xml:"products"`
Products []madnessProduct `xml:"product"`
}

type madnessProduct struct {
XMLName xml.Name `xml:"product"`
SKU string `xml:"id"`
Name string `xml:"title"`
ThumbUrl string `xml:"image_link"`
Price string `xml:"price"`
Stock string `xml:"availability"`
Link string `xml:"link"`
}

func madnessAvailbilityToStock(s string) int {
switch s {
case "in stock":
return 0
case "on backorder":
return 1
case "out of stock":
return 2
default:
return 2
}
}

func ScrapeBoardsOfMadnessXML() (map[string]any, []map[string]any, error) {
store_id := int64(16)
rs := []map[string]any{}
detected := 0

link := "https://boardsofmadness.com/wp-content/uploads/woo-product-feed-pro/xml/sVVFMsJLyEEtvbil4fbIOdm8b4ha7ewz.xml"
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return nil, nil, err
}

conn := &http.Client{}
resp, err := conn.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()

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

root := madnessRoot{}
err = xml.Unmarshal(body, &root)
if err != nil {
return nil, nil, err
}

for _, item := range root.Products {
item := map[string]any{
"name": item.Name,
"store_id": store_id,
"store_thumb": item.ThumbUrl,
"stock": madnessAvailbilityToStock(item.Stock),
"price": getPrice(item.Price),
"url": item.Link,
}
rs = append(rs, item)
detected++
}

return map[string]interface{}{
"name": "Boards of Madness",
"id": store_id,
"scraped": detected,
}, rs, nil
}
90 changes: 45 additions & 45 deletions pkg/scrape/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,38 @@ var (
"efantasy": ScrapeEfantasy,
"epitrapezio": ScrapeEpitrapezio,
// "fantasyshop": ScrapeFantasyShop,
"gameexplorers": ScrapeGameExplorers,
"gamerules": ScrapeGameRules,
"gamesuniverse": ScrapeGamesUniverse,
"genx": ScrapeGenx,
"hobbytheory": ScrapeHobbyTheory,
"kaissaeu": ScrapeKaissaEu,
"kaissagames": ScrapeKaissaGames,
"meepleonboard": ScrapeMeepleOnBoard,
"meepleplanet": ScrapeMeeplePlanet,
"mysterybay": ScrapeMysteryBay,
"ozon": ScrapeOzon,
"politeia": ScrapePoliteia,
"rollnplay": ScrapeRollnplay,
"vgames": ScrapeVgames,
"xrysoftero": ScrapeXrysoFtero,
"innkeeper": ScrapeInnkeeper,
"kaissapagkrati": ScrapeKaissaPagkrati,
"fantasygate": ScrapeFantasyGate,
"gamescom": ScrapeGamescom,
"nolabelx": ScrapeNoLabelX,
"efantasycrete": ScrapeEfantasyCrete,
"gameexplorers": ScrapeGameExplorers,
"gamerules": ScrapeGameRules,
"gamesuniverse": ScrapeGamesUniverse,
"genx": ScrapeGenx,
"hobbytheory": ScrapeHobbyTheory,
"kaissaeu": ScrapeKaissaEu,
"kaissagames": ScrapeKaissaGames,
"meepleonboard": ScrapeMeepleOnBoard,
"meepleplanet": ScrapeMeeplePlanet,
"mysterybay": ScrapeMysteryBay,
"ozon": ScrapeOzon,
"politeia": ScrapePoliteia,
"rollnplay": ScrapeRollnplay,
// "vgames": ScrapeVgames,
"xrysoftero": ScrapeXrysoFtero,
"innkeeper": ScrapeInnkeeper,
// "kaissapagkrati": ScrapeKaissaPagkrati,
"fantasygate": ScrapeFantasyGate,
"gamescom": ScrapeGamescom,
"nolabelx": ScrapeNoLabelX,
// "efantasycrete": ScrapeEfantasyCrete,
"dragonseye": ScrapeDragonsEye,
"playce": ScrapePlayce,
"rollntrade": ScrapeRollntrade,
"mythicvault": ScrapeMythicVault,
"kaissaioannina": ScrapeKaissaIoannina,
"kaissachania": ScrapeKaissaChania,
"gametheory": ScrapeGameTheory,
"philibert": ScrapeCOINPhilibertnet,
"fanen": ScrapeCOINFanen,
"gamershq": ScrapeCOINGamersHQ,
"hexasim": ScrapeCOINHexasim,
// "gametheory": ScrapeGameTheory,
"philibert": ScrapeCOINPhilibertnet,
"fanen": ScrapeCOINFanen,
"gamershq": ScrapeCOINGamersHQ,
"hexasim": ScrapeCOINHexasim,
// "udogrebe": ScrapeCOINUdo,
"myfriendsgames": ScrapeMyFriendsGames,
"milan": ScrapeCOINMilan,
Expand All @@ -78,25 +78,25 @@ var (
"ozon": 17,
"politeia": 12,
"rollnplay": 26,
"vgames": 5,
"xrysoftero": 21,
"innkeeper": 30,
"kaissapagkrati": 31,
"fantasygate": 2,
"gamescom": 18,
"nolabelx": 32,
"efantasycrete": 33,
"dragonseye": 34,
"playce": 35,
"rollntrade": 36,
"mythicvault": 37,
"kaissaioannina": 38,
"kaissachania": 39,
"gametheory": 40,
"philibert": 41,
"fanen": 42,
"gamershq": 43,
"hexasim": 44,
// "vgames": 5,
"xrysoftero": 21,
"innkeeper": 30,
// "kaissapagkrati": 31,
"fantasygate": 2,
"gamescom": 18,
"nolabelx": 32,
// "efantasycrete": 33,
"dragonseye": 34,
"playce": 35,
"rollntrade": 36,
"mythicvault": 37,
"kaissaioannina": 38,
"kaissachania": 39,
// "gametheory": 40,
"philibert": 41,
"fanen": 42,
"gamershq": 43,
"hexasim": 44,
// "udogrebe": 45,
"myfriendsgames": 46,
"milan": 47,
Expand Down
2 changes: 1 addition & 1 deletion pkg/scrape/mysterybay.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ScrapeMysteryBay() (map[string]any, []map[string]any, error) {
}

item := map[string]any{
"name": e.ChildText("h3"),
"name": e.ChildText("p[data-hook=product-item-name]"),
"store_id": store_id,
"store_thumb": url,
"stock": stock,
Expand Down
2 changes: 1 addition & 1 deletion pkg/scrape/xrysoftero.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func ScrapeXrysoFtero() (map[string]any, []map[string]any, error) {
colly.CacheDir(CacheDir),
)

collector.OnHTML(".thumbnail-container", func(e *colly.HTMLElement) {
collector.OnHTML("article.product_item", func(e *colly.HTMLElement) {
url := e.ChildAttr(".cover-image", "src")
if url == "" {
return
Expand Down

0 comments on commit ecc2d24

Please sign in to comment.