fakeUserAgent

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2025 License: MIT Imports: 9 Imported by: 3

README

fake-useragent

Up-to-date simple useragent faker with real world database in Golang

Features

  • Data is pre-downloaded & post-processed from Intoli LLC and the data is part of the package itself
  • The most up-to-date database if user-agents
  • The data consists of a wide range of browser agents and various browsers
  • Retrieves user-agent strings (both of type: desktop, tablet and/or mobile UAs)
  • Retrieve user-agent fron JSON file in Go struct, with fields like Useragent, Percent, Type, DeviceBrand, Browser, BrowserVersion, Os, OsVersion and Platform
  • This Golang package has the same functionality as popular Python libray "Fake-useragent" and based on same Json user-agents database
  • Compatible fake user-agent with Colly Scraping Framework and other.
  • Compatible fake user-agent with net/http standard library.
go get github.com/lib4u/fake-useragent
Usage

Simple usage examples

//...
import (
	"fmt"
	app "github.com/lib4u/fake-useragent"
)
//....
ua, err := app.New()
	if err != nil {
		fmt.Println(err)
	}
// Get random user-agent in string
fmt.Println(ua.GetRandom())  // Mozilla/5.0 (iPhone; CPU iPhone OS 18_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Mobile/15E148 Safari/604.1

// Get user-agent string from a specific browser
fmt.Println(ua.Filter().Chrome().Get())
// Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36

fmt.Println(ua.Filter().Firefox().Get())
//Mozilla/5.0 (Android 14; Mobile; rv:133.0) Gecko/133.0 Firefox/133.0

fmt.Println(ua.Filter().Safari().Get())
//Mozilla/5.0 (iPhone; CPU iPhone OS 18_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Mobile/15E148 Safari/604.1

fmt.Println(ua.Filter().Opera().Get())
//Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 OPR/114.0.0.0

fmt.Println(ua.Filter().Edge().Get())
//Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Advanced Use

You can specify additional user-agent filters

Supported next Browsers, OS and platforms in fake-useragent
// Browsers
const (
	Google           = "Google"
	Chrome           = "Chrome"
	Firefox          = "Firefox"
	Edge             = "Edge"
	Opera            = "Opera"
	Safari           = "Safari"
	AndroidBrowser   = "Android"
	YandexBrowser    = "Yandex Browser"
	SamsungInternet  = "Samsung Internet"
	OperaMobile      = "Opera Mobile"
	MobileSafari     = "Mobile Safari"
	FirefoxMobile    = "Firefox Mobile"
	FirefoxiOS       = "Firefox iOS"
	ChromeMobile     = "Chrome Mobile"
	ChromeMobileiOS  = "Chrome Mobile iOS"
	MobileSafariUIWK = "Mobile Safari UI/WKWebView"
	EdgeMobile       = "Edge Mobile"
	DuckDuckGoMobile = "DuckDuckGo Mobile"
	MiuiBrowser      = "MiuiBrowser"
	Whale            = "Whale"
	Twitter          = "Twitter"
	Facebook         = "Facebook"
	AmazonSilk       = "Amazon Silk"
)

// OS
const (
	Windows  = "Windows"
	Linux    = "Linux"
	Ubuntu   = "Ubuntu"
	ChromeOS = "Chrome OS"
	MacOSX   = "Mac OS X"
	Android  = "Android"
	IOS      = "iOS"
)

// Platforms
const (
	Mobile  = "mobile"
	Tablet  = "tablet"
	Desktop = "desktop"
)

If you want to specify your own browser list, you can do that via the browsers argument. This example will only return random user-agents from Firefox and Chrome:

ua, err := app.New()
	if err != nil {
		fmt.Println(err)
	}

// Use filters by browser
fmt.Println(ua.Filter().Browser(app.Firefox, app.Chrome).Get())

Also you can use filter by Platform and OS

ua, err := app.New()
	if err != nil {
		fmt.Println(err)
	}
// Random user-agent from a desktop device
fmt.Println(ua.Filter().Platform(app.Desktop).Get())

// Use filters by platform Tablet and Chrome browser
fmt.Println(ua.Filter().Chrome().Platform(app.Tablet).Get())

// Use filters by OS
fmt.Println(ua.Filter().Chrome().Os(app.IOS).Get())

// Use filters by IOS And Windows
fmt.Println(ua.Filter().Chrome().Os(app.IOS, app.Windows).Get())

If you need, you can access the entire structure, for example if you need to get the BrowserVersion value or Percent

ua, err := app.New()
	if err != nil {
		fmt.Println(err)
	}

// Get Percent display
fmt.Println(ua.Filter().Browser(app.Firefox, app.Chrome).GetRaw().Percent)

// Get all Go Struct
fmt.Println(ua.Filter().Browser(app.Firefox, app.Chrome).GetRaw())

You can set the fallback string using the SetFallback function, in much rare cases something failed:

ua, err := app.New()
	if err != nil {
		fmt.Println(err)
	}
 // Set Fallback string
 ua.SetFallback("user favorite user-agent")
// Use filters by browser
fmt.Println(ua.Filter().Browser(app.Firefox, app.Chrome).Get())

Documentation

Index

Constants

View Source
const (
	Google           = "Google"
	Chrome           = "Chrome"
	Firefox          = "Firefox"
	Edge             = "Edge"
	Opera            = "Opera"
	Safari           = "Safari"
	AndroidBrowser   = "Android"
	YandexBrowser    = "Yandex Browser"
	SamsungInternet  = "Samsung Internet"
	OperaMobile      = "Opera Mobile"
	MobileSafari     = "Mobile Safari"
	FirefoxMobile    = "Firefox Mobile"
	FirefoxiOS       = "Firefox iOS"
	ChromeMobile     = "Chrome Mobile"
	ChromeMobileiOS  = "Chrome Mobile iOS"
	MobileSafariUIWK = "Mobile Safari UI/WKWebView"
	EdgeMobile       = "Edge Mobile"
	DuckDuckGoMobile = "DuckDuckGo Mobile"
	MiuiBrowser      = "MiuiBrowser"
	Whale            = "Whale"
	Twitter          = "Twitter"
	Facebook         = "Facebook"
	AmazonSilk       = "Amazon Silk"
)

Browsers

View Source
const (
	Windows  = "Windows"
	Linux    = "Linux"
	Ubuntu   = "Ubuntu"
	ChromeOS = "Chrome OS"
	MacOSX   = "Mac OS X"
	Android  = "Android"
	IOS      = "iOS"
)

OS

View Source
const (
	Mobile  = "mobile"
	Tablet  = "tablet"
	Desktop = "desktop"
)

Platforms

Variables

This section is empty.

Functions

func ExtractMajorVersion

func ExtractMajorVersion(version string) int

func Filter

func Filter[E any](s *[]E, f func(E) bool) []E

Types

type FilterBy

type FilterBy struct {
	UserAgent *UserAgent
	// contains filtered or unexported fields
}

func (*FilterBy) Browser

func (c *FilterBy) Browser(f ...string) *FilterBy

Filter by: Browser

func (*FilterBy) Chrome

func (c *FilterBy) Chrome() *FilterBy

Filter by: Chrome browserss

func (*FilterBy) Edge

func (c *FilterBy) Edge() *FilterBy

Filter by: Edge browser

func (*FilterBy) Firefox

func (c *FilterBy) Firefox() *FilterBy

Filter by: Firefox browser

func (*FilterBy) Get

func (c *FilterBy) Get() string

Gets from filters Method return userAgent string only or empty string if nothing found

func (*FilterBy) GetRaw

func (c *FilterBy) GetRaw() UserAgents

Gets from filters Method return all struct UserAgents or empty UserAgents struct if nothing found

func (*FilterBy) Opera

func (c *FilterBy) Opera() *FilterBy

Filter by: Opera browser

func (*FilterBy) Os

func (c *FilterBy) Os(f ...string) *FilterBy

Filter by: Os

func (*FilterBy) OsVer

func (c *FilterBy) OsVer(f int) *FilterBy

Filter by: Os Version

func (*FilterBy) Platform

func (c *FilterBy) Platform(f ...string) *FilterBy

Filter by: Platform

func (*FilterBy) Safari

func (c *FilterBy) Safari() *FilterBy

Filter by: Safari browser

type UserAgent

type UserAgent struct {
	List     *[]UserAgents
	Filtered []UserAgents
	// contains filtered or unexported fields
}

func New

func New() (*UserAgent, error)

Init UserAgent client and load user-Agents from JSON file to memory cache

func (*UserAgent) Filter

func (c *UserAgent) Filter() *FilterBy

Init filters for user-Agent

func (*UserAgent) GetRandom

func (c *UserAgent) GetRandom() string

Gets random user-Agent without filters This method don't init filter method

func (*UserAgent) SetFallback

func (c *UserAgent) SetFallback(fallback string) *UserAgent

Set fallback user_agent

type UserAgents

type UserAgents struct {
	Useragent                string  `json:"useragent"`
	Percent                  float64 `json:"percent"`
	Type                     string  `json:"type"`
	DeviceBrand              string  `json:"device_brand"`
	Browser                  string  `json:"browser"`
	BrowserVersion           string  `json:"browser_version"`
	BrowserVersionMajorMinor float64 `json:"browser_version_major_minor"`
	Os                       string  `json:"os"`
	OsVersion                string  `json:"os_version"`
	Platform                 string  `json:"platform"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL