Documentation
¶
Overview ¶
Package uas provides a go implementation of the http://user-agent-string.info/ processor. Standard usage is to provide a user-agent string to the Parse method of a Manifest instance and retrieve an Agent instance in return. From the Agent, you can obtain: browser details, operating system details, and device details.
You must create a Manifest instance by providing an XML file from the UAS.info site (http://user-agent-string.info/rpc/get_data.php?key=free&format=xml&download=y) to the LoadFile function; or you can provide a Reader of similar ilk to the Load function. This package currently doesn't support downloading Manifests automatically, but you can also easily create new instances of different Manifests; i.e. a Manifest is not a global object. This package also does not yet support the .ini format; mostly this was to make processing easier by using the built-in XML unmarshalling capabilities of Go.
import ( "fmt" "os" "github.com/signal/go-uasparser" ) var manifest, err := uas.LoadFile("/tmp/uas_YYYYMMDD-01.xml") if err != nil { fmt.Println(err) os.Exit(1) }
Given a Manifest, you can now easily parse an Agent like so:
var agent := manifest.Parse("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) ...") if agent != nil { fmt.Println("Agent type:", agent.Type) fmt.Println("Browser name:", agent.BrowserVersion.Name) fmt.Println("Browser Version:", agent.BrowserVersion.Version) fmt.Println("OS name:", agent.Os.Name) fmt.Println("Device name:", agent.Device.Name) }
You can check out the model structure to figure out what other values are available. Unlike other implementations, the values are not simply returned as a flat map.
Currently, robots are treated differently in that any agent recognized as one is returned from Parse as a nil value. You can check to see if the agent is indeed a robot by asking if it's so:
if manifest.IsRobot("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)") { fmt.Println("I AM A ROBOT") }
Index ¶
- Constants
- type Agent
- type Browser
- type BrowserOs
- type BrowserReg
- type BrowserType
- type BrowserVersion
- type Checksum
- type Data
- type Description
- type Device
- type DeviceReg
- type Manifest
- func (self *Manifest) FindBrowserTypeByName(name string) (*BrowserType, bool)
- func (self *Manifest) FindDeviceByName(name string) (*Device, bool)
- func (self *Manifest) FindOsByName(name string) (*Os, bool)
- func (self *Manifest) FindRobot(ua string) (*Robot, bool)
- func (self *Manifest) GetBrowser(id int) (*Browser, bool)
- func (self *Manifest) GetBrowserType(id int) (*BrowserType, bool)
- func (self *Manifest) GetDevice(id int) (*Device, bool)
- func (self *Manifest) GetOs(id int) (*Os, bool)
- func (self *Manifest) GetOsForBrowser(id int) (*Os, bool)
- func (self *Manifest) IsRobot(ua string) bool
- func (self *Manifest) Parse(ua string) *Agent
- func (self *Manifest) ParseBrowserVersion(ua string) *BrowserVersion
- func (self *Manifest) ParseDevice(ua string) *Device
- func (self *Manifest) ParseOs(ua string) *Os
- type Os
- type OsReg
- type Robot
Constants ¶
const ( UnknownBrowserName = "unknown" OtherBrowserTypeName = "Other" UnknownBrowserTypeName = "unknown" UnknownOsName = "unknown" OtherDeviceName = "Other" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Agent ¶
type Agent struct { String string Type string BrowserVersion *BrowserVersion Os *Os Device *Device }
type BrowserReg ¶
type BrowserReg struct { BrowserId int `xml:"browser_id"` // contains filtered or unexported fields }
type BrowserType ¶
type BrowserVersion ¶
type Data ¶
type Data struct { XMLName xml.Name `xml:"data"` Robots []*Robot `xml:"robots>robot"` OperatingSystems []*Os `xml:"operating_systems>os"` Browsers []*Browser `xml:"browsers>browser"` BrowserTypes []*BrowserType `xml:"browser_types>browser_type"` BrowsersReg []*BrowserReg `xml:"browsers_reg>browser_reg"` BrowsersOs []*BrowserOs `xml:"browsers_os>browser_os"` OperatingSystemsReg []*OsReg `xml:"operating_systems_reg>operating_system_reg"` Devices []*Device `xml:"devices>device"` DevicesReg []*DeviceReg `xml:"devices_reg>device_reg"` }
type Description ¶
type DeviceReg ¶
type DeviceReg struct { DeviceId int `xml:"device_id"` // contains filtered or unexported fields }
type Manifest ¶
type Manifest struct { XMLName xml.Name `xml:"uasdata"` Description *Description Data *Data // UnknownBrowserVersion is a BrowserVersion that represents an unknown match. UnknownBrowserVersion *BrowserVersion // UnknownBrowser is a Browser that represents an unknown browser. UnknownBrowser *Browser // OtherBrowserType is a Browser that represents the Other type. OtherBrowserType *BrowserType // UnknownOs is an Os that represents the Unknown type. UnknownOs *Os // OtherDevice is a Device that represents the Other type. OtherDevice *Device }
func (*Manifest) FindBrowserTypeByName ¶
func (self *Manifest) FindBrowserTypeByName(name string) (*BrowserType, bool)
Finds a specific BrowserType by its listed name in the manifest. Returns nil,false if no BrowserType has the given name; otherwise it returns a BrowserType instance and true.
func (*Manifest) FindDeviceByName ¶
Finds a specific Device by its listed name in the manifest. Returns nil,false if no device has the given name; otherwise it returns a Device instance and true.
func (*Manifest) FindOsByName ¶
Finds a specific Os by its listed name in the manifest. Returns nil,false if no OS has the given name; otherwise it returns an Os instance and true.
func (*Manifest) FindRobot ¶
Returns the Robot instance (along with a true value indicating success) matching the provided user-agent string. Otherwise a nil and false are returned.
func (*Manifest) GetBrowser ¶
Finds a specific Browser by its listed ID in the manifest. Returns nil,false if no Browser has the given ID; otherwise it returns a Browser instance and true.
func (*Manifest) GetBrowserType ¶
func (self *Manifest) GetBrowserType(id int) (*BrowserType, bool)
Finds a specific BrowserType by its listed ID in the manifest. Returns nil,false if no BrowserType has the given ID; otherwise it returns a BrowserType instance and true.
func (*Manifest) GetDevice ¶
Finds a specific Device by its ID in the manifest. Returns nil,false if no Device has the given ID; otherwise it returns a Device instance and true.
func (*Manifest) GetOs ¶
Finds a specific Os by its listed ID in the manifest. Returns nil,false if no OS has the given ID; otherwise it returns an Os instance and true.
func (*Manifest) GetOsForBrowser ¶
Finds the Os tied to a Browser given by the Browser's ID in the manifest. Returns nil,false if no Browser has the given ID or if no Os exists for the ID listed with the Browser (unlikely). Otherwise it returns an Os instance and true.
func (*Manifest) IsRobot ¶
Returns true if the given user-agent string matches the user-agent for a Robot (bot).
func (*Manifest) Parse ¶
Parses a provided user-agent string and returns an Agent instance. If the user-agent matches that of a robot, nil is returned. If no browser is matched, it will be listed as unknown, but the OS and device may still be matched.
func (*Manifest) ParseBrowserVersion ¶
func (self *Manifest) ParseBrowserVersion(ua string) *BrowserVersion
Parses out a BrowserVersion instance from a user-agent string. That is, it finds a matching Browser and extracts a version string if it can. Returns nil if no match could be found.
func (*Manifest) ParseDevice ¶
Parses a Device from the provider user-agent string. You may get different results over what you might get from calling Parse as this is a less deductive function. Returns nil if no Device is matched.