Files
go-extractor/sites/useragents/useragents.go
Steve Dudenhoeffer e7b7e78796
Some checks failed
CI / vet (push) Failing after 15s
CI / build (push) Failing after 30s
CI / test (push) Failing after 36s
fix: bug fixes, test coverage, and CI workflow
- Fix Nodes.First() panic on empty slice (return nil)
- Fix ticker leak in archive.go (create once, defer Stop)
- Fix cookie path matching for empty and root paths
- Fix lost query params in google.go (u.Query().Set was discarded)
- Fix type assertion panic in useragents.go
- Fix dropped date parse error in powerball.go
- Remove unreachable dead code in megamillions.go and powerball.go
- Simplify document.go WaitForNetworkIdle, remove unused root field
- Remove debug fmt.Println calls across codebase
- Replace panic(err) with stderr+exit in all cmd/ programs
- Fix duckduckgo cmd: remove useless defer, return error on bad safesearch
- Fix archive cmd: ToConfig returns error instead of panicking
- Add 39+ unit tests across 6 new test files
- Add Gitea Actions CI workflow (build, test, vet in parallel)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-09 11:14:19 -05:00

75 lines
1.3 KiB
Go

package useragents
import (
"context"
"encoding/json"
"fmt"
"io"
"gitea.stevedudenhoeffer.com/steve/go-extractor"
)
type Config struct{}
var DefaultConfig = Config{}
func deferClose(cl io.Closer) {
if cl != nil {
_ = cl.Close()
}
}
func GetMostCommonDesktopUserAgent(ctx context.Context, b extractor.Browser) (string, error) {
return DefaultConfig.GetMostCommonDesktopUserAgent(ctx, b)
}
func (c Config) GetMostCommonDesktopUserAgent(ctx context.Context, b extractor.Browser) (string, error) {
doc, err := b.Open(ctx, "https://www.useragents.me/", extractor.OpenPageOptions{})
if err != nil {
return "", fmt.Errorf("failed to open useragents.me: %w", err)
}
defer deferClose(doc)
s := doc.Select("#most-common-desktop-useragents-json-csv > div:nth-child(1) > textarea:nth-child(4)")
text := ""
for _, el := range s {
t, err := el.Content()
if err != nil {
return "", fmt.Errorf("failed to get text: %w", err)
}
text += t
}
data := []map[string]any{}
err = json.Unmarshal([]byte(text), &data)
if err != nil {
return "", err
}
highestAgent := ""
highestPct := 0.0
for _, agent := range data {
pct, ok := agent["pct"].(float64)
if !ok {
continue
}
if pct > highestPct {
ua, ok := agent["ua"].(string)
if !ok {
continue
}
highestPct = pct
highestAgent = ua
}
}
return highestAgent, nil
}