uas

package module
v0.0.0-...-3244e43 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2014 License: MIT Imports: 6 Imported by: 0

README

go-uasparser

A Go implementation of the user-agent-string.info (UAS.info) User-Agent string analysis tool. See the GoDoc for more detailed usage.

Installation

go get "github.com/signal/go-uasparser"

Running tests

make test
make benchmark

Disclaimer(s)

This package does its best to similarly implement the logic from the PHP implementation found on UAS.info; except where the documentation states that it didn't want to. Should you find glaring mistakes, please do open an issue or a pull request.

License

go-uasparser is released under the MIT license. See MIT LICENSE.

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")
}

In all cases, when an Agent is found it will be cached in a Manifest-specific LRU that can hold 5000 entries. This is not configurable at the moment.

Index

Constants

View Source
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 Browser

type Browser struct {
	TypeId  int    `xml:"type"`
	URL     string `xml:"url"`
	InfoURL string `xml:"browser_info_url"`

	// mapped after parsing
	Type *BrowserType
	Os   *Os
	// contains filtered or unexported fields
}

type BrowserOs

type BrowserOs struct {
	BrowserId int `xml:"browser_id"`
	OsId      int `xml:"os_id"`
}

type BrowserReg

type BrowserReg struct {
	BrowserId int `xml:"browser_id"`
	// contains filtered or unexported fields
}

type BrowserType

type BrowserType struct {
	Id   int    `xml:"id"`
	Name string `xml:"type"` // a purposeful departure from the UAS naming
}

type BrowserVersion

type BrowserVersion struct {
	*Browser
	Version string
}

type Checksum

type Checksum struct {
	Type string `xml:"type,attr"`
	URL  string `xml:",innerxml"`
}

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 Description struct {
	XMLName   xml.Name    `xml:"description"`
	Label     string      `xml:"label"`
	Version   string      `xml:"version"`
	Checksums []*Checksum `xml:"checksum"`
}

type Device

type Device struct {
	Id      int    `xml:"id"`
	Name    string `xml:"name"`
	Icon    string `xml:"icon"`
	InfoURL string `xml:"device_info_url"`
}

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
	// contains filtered or unexported fields
}

func Load

func Load(reader io.Reader) (*Manifest, error)

Creates a new Manifest instance by processing the XML from the provided Reader.

func LoadFile

func LoadFile(path string) (*Manifest, error)

Creates a new Manifest instance by processing the XML from the provided file.

func (*Manifest) FindBrowserTypeByName

func (self *Manifest) FindBrowserTypeByName(name string) (*BrowserType, bool)

FindBrowserTypeByName identifies 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

func (self *Manifest) FindDeviceByName(name string) (*Device, bool)

FindDeviceByName identifies 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

func (self *Manifest) FindOsByName(name string) (*Os, bool)

FindOsByName identifies 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

func (self *Manifest) FindRobot(ua string) (*Robot, bool)

FindRobot identifies a 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

func (self *Manifest) GetBrowser(id int) (*Browser, bool)

GetBrowser retrieves 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)

GetBrowserType retrives 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

func (self *Manifest) GetDevice(id int) (*Device, bool)

GetDevice retrieves 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

func (self *Manifest) GetOs(id int) (*Os, bool)

GetOs retrieves 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

func (self *Manifest) GetOsForBrowser(id int) (*Os, bool)

GetOsForBrowser retrieves 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

func (self *Manifest) IsRobot(ua string) bool

IsRobot returns true if the given user-agent string matches the user-agent for a Robot (bot).

func (*Manifest) Parse

func (self *Manifest) Parse(ua string) *Agent

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. When possible, the returned Agent will be cached in an LRU-cache based on the full user-agent string.

func (*Manifest) ParseBrowserVersion

func (self *Manifest) ParseBrowserVersion(ua string) *BrowserVersion

ParseBrowserVersion 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

func (self *Manifest) ParseDevice(ua string) *Device

ParseDevice identifies 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.

func (*Manifest) ParseOs

func (self *Manifest) ParseOs(ua string) *Os

ParseOs identifies an Os 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.

type Os

type Os struct {
	Family  string `xml:"family"`
	URL     string `xml:"url"`
	InfoURL string `xml:"os_info_url"`
	// contains filtered or unexported fields
}

type OsReg

type OsReg struct {
	OsId int `xml:"os_id"`
	// contains filtered or unexported fields
}

type Robot

type Robot struct {
	Family    string `xml:"family"`
	UserAgent string `xml:"useragent"`
	InfoURL   string `xml:"bot_info_url"`
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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