TheMathemagicians/sevenkeys/mtggoldfish/decksearch.go

99 lines
3.1 KiB
Go
Raw Normal View History

2024-04-28 18:58:48 +00:00
package mtggoldfish
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/geziyor/geziyor"
"github.com/geziyor/geziyor/client"
"github.com/geziyor/geziyor/export"
)
var DECK_SEARCH_URL string = "https://www.mtggoldfish.com/deck_searches/create"
const (
TypeMaindeck = "maindeck"
TypeSideboard = "sideboard"
TypeCommander = "commander"
)
type DeckSearchIncludedCard struct {
Name string
Quantity int
Type string
}
type DeckSearchParameters struct {
Name string
Format string
IncludeTournamentDecks bool
IncludeUserDecks bool
Player string
StartDate string // TODO: Date type?
EndDate string // TODO: Date type?
IncludedCards []DeckSearchCard
}
func (p *DeckSearchParameters) String() string {
searchUrl := DECK_SEARCH_URL + "?utf8=✓" +
"&deck_search[name]=" + p.Name +
"&deck_search[format]=" + p.Format +
"&deck_search[types][]="
if p.IncludeTournamentDecks {
searchUrl = searchUrl + "&deck_search[types][]=tournament"
}
if p.IncludeUserDecks {
searchUrl = searchUrl + "&deck_search[types][]=user"
}
searchUrl = searchUrl + "&deck_search[player]=" + p.Player
//"&deck_search[date_range]=" + p.StartDate + " - " + p.EndDate
return searchUrl
}
type DeckSearchCard struct {
Name string
Quantity int
Type string
}
type DeckSearchDecklist struct {
MtgGoldfishId int
Date string // TODO: Date type?
Name string
Source string
Format string
Author string
Cards []DeckSearchCard
}
type DeckSearchResults struct {
Decklists []DeckSearchDecklist
DeckCount int
PageCount int
}
func parseSearchResults(g *geziyor.Geziyor, r *client.Response) {
r.HTMLDoc.Find("div.table-responsive").Each(func(i int, s *goquery.Selection) {
g.Exports <- map[string]interface{}{
"test": s.Find("table>thead>tr>th").Text(),
}
})
}
func DeckSearch(params DeckSearchParameters) (DeckSearchResults, error) {
searchUrl := `https://www.mtggoldfish.com/deck_searches/create?utf8=✓&deck_search[name]=Burn&deck_search[format]=pauper&deck_search[types][]=&deck_search[types][]=tournament&deck_search[types][]=user&deck_search[player]=Jirach1&deck_search[date_range]=04%2F14%2F2024+-+04%2F28%2F2024&deck_search[deck_search_card_filters_attributes][0][card]=Kuldotha+Rebirth&deck_search[deck_search_card_filters_attributes][0][quantity]=4&deck_search[deck_search_card_filters_attributes][0][type]=maindeck&deck_search[deck_search_card_filters_attributes][1][card]=&deck_search[deck_search_card_filters_attributes][1][quantity]=1&deck_search[deck_search_card_filters_attributes][1][type]=maindeck&deck_search[deck_search_card_filters_attributes][2][card]=&deck_search[deck_search_card_filters_attributes][2][quantity]=1&deck_search[deck_search_card_filters_attributes][2][type]=maindeck&counter=3&commit=Search`
fmt.Println(searchUrl)
geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{searchUrl},
ParseFunc: parseSearchResults,
Exporters: []export.Exporter{&export.JSON{}},
}).Start()
return DeckSearchResults{}, nil
}