65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
|
package useragents
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"math/rand"
|
||
|
"time"
|
||
|
|
||
|
"github.com/gocolly/colly"
|
||
|
)
|
||
|
|
||
|
type userAgentEntry struct {
|
||
|
UserAgent string `json:"ua"`
|
||
|
Percentage float64 `json:"pct"`
|
||
|
}
|
||
|
|
||
|
var defaultUserAgent string = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
|
||
|
var userAgents []string = getFreshUserAgents()
|
||
|
|
||
|
func GetRandomUserAgent() string {
|
||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||
|
return userAgents[r.Intn(len(userAgents))]
|
||
|
}
|
||
|
|
||
|
func getFreshUserAgents() []string {
|
||
|
var freshUserAgents []string = []string{}
|
||
|
|
||
|
c := colly.NewCollector(
|
||
|
colly.MaxDepth(1),
|
||
|
colly.AllowedDomains("www.useragents.me"),
|
||
|
colly.UserAgent(defaultUserAgent),
|
||
|
)
|
||
|
|
||
|
c.OnRequest(func(r *colly.Request) {
|
||
|
r.Headers.Set("Accept-Language", "de,en-US;q=0.7,en;q=0.3")
|
||
|
})
|
||
|
|
||
|
c.OnHTML("#most-common-desktop-useragents-json-csv", func(e *colly.HTMLElement) {
|
||
|
e.ForEach("textarea", func(n int, textarea *colly.HTMLElement) {
|
||
|
if n > 0 {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var rawData []userAgentEntry
|
||
|
err := json.Unmarshal([]byte(textarea.Text), &rawData)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
for _, e := range rawData {
|
||
|
for i := 0; i < (int(e.Percentage) + 1); i++ {
|
||
|
freshUserAgents = append(freshUserAgents, e.UserAgent)
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
|
||
|
c.Visit("https://www.useragents.me/")
|
||
|
|
||
|
if len(freshUserAgents) == 0 {
|
||
|
return []string{defaultUserAgent}
|
||
|
}
|
||
|
|
||
|
return freshUserAgents
|
||
|
}
|