- Extract shared DeferClose helper, removing 14 duplicate copies - Rename PlayWright-prefixed types to cleaner names (BrowserOptions, BrowserSelection, NewBrowser, etc.) - Rename fields: ServerAddress, RequireServer (was DontLaunchOnConnectFailure) - Extract shared initBrowser/mergeOptions into browser_init.go, deduplicating ~120 lines between NewBrowser and NewInteractiveBrowser - Remove unused locator field from document struct - Add tests for all previously untested packages (archive, aislegopher, wegmans, useragents, powerball) and expand existing test suites - Add MIGRATION.md documenting all breaking API changes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
package aislegopher
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gitea.stevedudenhoeffer.com/steve/go-extractor"
|
|
)
|
|
|
|
type Config struct {
|
|
}
|
|
|
|
var DefaultConfig = Config{}
|
|
|
|
var (
|
|
ErrInvalidURL = errors.New("invalid url")
|
|
)
|
|
|
|
type Item struct {
|
|
ID int
|
|
Name string
|
|
Price float64
|
|
}
|
|
|
|
func GetItemFromURL(ctx context.Context, b extractor.Browser, u *url.URL) (Item, error) {
|
|
return DefaultConfig.GetItemFromURL(ctx, b, u)
|
|
}
|
|
|
|
func (c Config) GetItemFromURL(ctx context.Context, b extractor.Browser, u *url.URL) (Item, error) {
|
|
res := Item{}
|
|
|
|
// the url will be in the format of aislegopher.com/p/slug/id
|
|
// we need to parse the slug and id from the url
|
|
a := strings.Split(u.Path, "/")
|
|
if len(a) != 4 {
|
|
return res, ErrInvalidURL
|
|
}
|
|
|
|
if a[1] != "p" {
|
|
return res, ErrInvalidURL
|
|
}
|
|
|
|
if u.Host != "aislegopher.com" && u.Host != "www.aislegopher.com" {
|
|
return res, ErrInvalidURL
|
|
}
|
|
|
|
res.ID, _ = strconv.Atoi(a[3])
|
|
|
|
doc, err := b.Open(ctx, u.String(), extractor.OpenPageOptions{})
|
|
defer extractor.DeferClose(doc)
|
|
if err != nil {
|
|
return res, fmt.Errorf("failed to open page: %w", err)
|
|
}
|
|
|
|
names := doc.Select(".h4")
|
|
|
|
if len(names) > 0 {
|
|
res.Name, _ = names[0].Text()
|
|
}
|
|
|
|
prices := doc.Select(".h2")
|
|
|
|
if len(prices) > 0 {
|
|
priceStr, _ := prices[0].Text()
|
|
priceStr = strings.ReplaceAll(priceStr, "$", "")
|
|
priceStr = strings.TrimSpace(priceStr)
|
|
res.Price, _ = strconv.ParseFloat(priceStr, 64)
|
|
}
|
|
|
|
return res, nil
|
|
}
|