added tapology link getter to scraper

This commit is contained in:
2025-02-16 20:50:40 -06:00
parent 50c4133525
commit c8184d5a71
7 changed files with 209 additions and 14 deletions
+34 -8
View File
@@ -5,6 +5,7 @@ import (
_ "embed"
"fmt"
"testing"
"net/http"
"github.com/PuerkitoBio/goquery"
)
@@ -51,6 +52,9 @@ var ONEFN29 []byte
//go:embed testdata/one-172.html
var ONE172 []byte
//go:embed testdata/tapology-index.html
var TapologyIndex []byte
var HTMLContent map[string][]byte = map[string][]byte{
"https://www.ufc.com/events": UFCEventList,
"https://www.ufc.com/event/ufc-312": UFC312,
@@ -66,11 +70,21 @@ var HTMLContent map[string][]byte = map[string][]byte{
"https://www.onefc.com/events/one171/": ONE171,
"https://www.onefc.com/events/onefightnight29/": ONEFN29,
"https://www.onefc.com/events/one172/": ONE172,
"https://www.tapology.com": TapologyIndex,
}
type DummyClient struct{}
type DummyClient struct {
headers map[string]string
}
func (c *DummyClient) Get(url string, options ...RequestOption) (*goquery.Selection, error) {
req, err := http.NewRequest("GET", url, nil)
for _, option := range options {
if option != nil {
option(req)
}
}
url = req.URL.String()
htmlstring, exists := HTMLContent[url]
if !exists {
return nil, fmt.Errorf("Invalid URL: %s", url)
@@ -82,11 +96,23 @@ func (c *DummyClient) Get(url string, options ...RequestOption) (*goquery.Select
return document.Find("html").First(), nil
}
func (c *DummyClient) AddHeader(key string, value string) {}
func (c *DummyClient) AddHeader(key string, value string) {
c.headers[key] = value
}
func (c *DummyClient) HasHeader(key string) bool {
_, exists := c.headers[key]
return exists
}
func NewDummyClient() *DummyClient {
return &DummyClient{
headers: map[string]string{},
}
}
func TestUFC(t *testing.T) {
client := DummyClient{}
events, err := ScrapeUFC(&client)
client := NewDummyClient()
events, err := ScrapeUFC(client)
if err != nil {
t.Errorf("Scraping UFC failed: %s", err)
}
@@ -100,8 +126,8 @@ func TestUFC(t *testing.T) {
}
func TestONE(t *testing.T) {
client := DummyClient{}
events, err := ScrapeONE(&client)
client := NewDummyClient()
events, err := ScrapeONE(client)
if err != nil {
t.Errorf("Scraping ONE failed: %s", err)
}
@@ -112,10 +138,10 @@ func TestONE(t *testing.T) {
}
func TestScrapeEvents(t *testing.T) {
client := DummyClient{}
client := NewDummyClient()
q, err := InitDb("test-db.sqlite")
if err != nil {
t.Error("Error initializing database: ", err)
}
ScrapeEvents(q, &client)
ScrapeEvents(q, client)
}