+ RenewUseragents()

This commit is contained in:
Andreas Schulte 2024-01-17 22:33:57 +01:00
parent cd96a9440f
commit 98dbbf54f3
Signed by: andreas
GPG Key ID: E123DA7BD8F9C8AB
2 changed files with 7 additions and 2 deletions

View File

@ -14,7 +14,7 @@ type useragentEntry struct {
Percentage float64 `json:"pct"` 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 defaultUseragent string = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.3"
var useragents []string = getFreshUseragents() var useragents []string = getFreshUseragents()
// GetAllUseragents returns all available useragents // GetAllUseragents returns all available useragents
@ -31,6 +31,11 @@ func GetRandomUseragent() string {
return useragents[r.Intn(len(useragents))] return useragents[r.Intn(len(useragents))]
} }
// RenewUseragents scrapes a fresh list of useragents
func RenewUseragents() {
useragents = getFreshUseragents()
}
func getFreshUseragents() []string { func getFreshUseragents() []string {
var freshUserAgents []string = []string{} var freshUserAgents []string = []string{}

View File

@ -116,7 +116,7 @@ func TestShouldBeValidUseragent(t *testing.T) {
} }
func TestGetFreshUserAgents(t *testing.T) { func TestGetFreshUserAgents(t *testing.T) {
expectedMinimumAmountEntries := 75 expectedMinimumAmountEntries := 10
if len(useragents) < expectedMinimumAmountEntries { if len(useragents) < expectedMinimumAmountEntries {
t.Errorf("\ngot: %d\nwanted at least: %d\nfor: len(userAgents)", len(useragents), expectedMinimumAmountEntries) t.Errorf("\ngot: %d\nwanted at least: %d\nfor: len(userAgents)", len(useragents), expectedMinimumAmountEntries)
} }