ua

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2021 License: MIT Imports: 3 Imported by: 0

README

Go/Golang package for parsing user agent strings GoDoc

Package ua.Parse(userAgent string) function parses browser's and bot's user agents strings and determins:

  • User agent name and version (Chrome, Firefox, Googlebot, etc.)
  • Operating system name and version (Windows, Android, iOS etc.)
  • Device type (mobile, desktop, tablet, bot)
  • Device name if available (iPhone, iPad, Huawei VNS-L21)
  • URL provided by the bot (http://www.google.com/bot.html etc.)

Status

Still need some work on detecting Andorid device names.

Fill free to report an issue for any User-Agent string not recognized or misinterpreted.

Installation

go get github.com/mileusna/useragent

Example

package main

import (
    "fmt"
    "strings"

    "github.com/mileusna/useragent"
)

func main() {
    userAgents := []string{
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
        "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1",	
        "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) FxiOS/8.1.1b4948 Mobile/14F89 Safari/603.2.4",
        "Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1",
        "Mozilla/5.0 (Linux; Android 4.3; GT-I9300 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36",
        "Mozilla/5.0 (Android 4.3; Mobile; rv:54.0) Gecko/54.0 Firefox/54.0",
        "Mozilla/5.0 (Linux; Android 4.3; GT-I9300 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36 OPR/42.9.2246.119956",
        "Opera/9.80 (Android; Opera Mini/28.0.2254/66.318; U; en) Presto/2.12.423 Version/12.16",
    }

    for _, s := range userAgents {
        ua := ua.Parse(s)
        fmt.Println()
        fmt.Println(ua.String)
        fmt.Println(strings.Repeat("=", len(ua.String)))
        fmt.Println("Name:", ua.Name, "v", ua.Version)
        fmt.Println("OS:", ua.OS, "v", ua.OSVersion)
        fmt.Println("Device:", ua.Device)
        if ua.Mobile {
            fmt.Println("(Mobile)")
        }
        if ua.Tablet {
            fmt.Println("(Tablet)")
        }
        if ua.Desktop {
            fmt.Println("(Desktop)")
        }
        if ua.Bot {
            fmt.Println("(Bot)")
        }
        if ua.URL != "" {
            fmt.Println(ua.URL)
        }
    }
}


Shorthand functions

Beside UserAgent{} struct and its properties returned by ua.Parse(), there is a bunch of shorthand functions for most popular browsers and operating systems, so this code:

    ua := ua.Parse(userAgentString)
    if ua.OS == "Android" && ua.Name == "Chrome" {
        // do something
    }

can be also written on this way:

    ua := ua.Parse(userAgentString)
    if ua.IsAndroid() && ua.IsChrome() {
        // do something
    }

Notice

  • Opera and Opera Mini are two browsers, since they operate on very different ways.
  • If Googlebot (or any other bot) is detected and it is using its mobile crawler, both bot and mobile flags will be set to true.

Documentation

Index

Examples

Constants

View Source
const (
	Windows      = "Windows"
	WindowsPhone = "Windows Phone"
	Android      = "Android"
	MacOS        = "macOS"
	IOS          = "iOS"
	Linux        = "Linux"
	ChromeOS     = "CrOS"

	Opera            = "Opera"
	OperaMini        = "Opera Mini"
	OperaTouch       = "Opera Touch"
	Chrome           = "Chrome"
	Firefox          = "Firefox"
	InternetExplorer = "Internet Explorer"
	Safari           = "Safari"
	Edge             = "Edge"
	Vivaldi          = "Vivaldi"

	Googlebot           = "Googlebot"
	Twitterbot          = "Twitterbot"
	FacebookExternalHit = "facebookexternalhit"
	Applebot            = "Applebot"
)

Constants for browsers and operating systems for easier comparison

Variables

This section is empty.

Functions

This section is empty.

Types

type UserAgent

type UserAgent struct {
	Name      string
	Version   string
	OS        string
	OSVersion string
	Device    string
	Mobile    bool
	Tablet    bool
	Desktop   bool
	Bot       bool
	URL       string
	String    string
}

UserAgent struct containing all data extracted from parsed user-agent string

func Parse

func Parse(userAgent string) UserAgent

Parse user agent string returning UserAgent struct

Example
package main

import (
	"fmt"
	"strings"

	ua "github.com/mileusna/useragent"
)

func main() {
	userAgents := []string{
		// Mac
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8",
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36",
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:54.0) Gecko/20100101 Firefox/54.0",
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36 OPR/46.0.2597.57",
		"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.91 Safari/537.36 Vivaldi/1.92.917.39",

		// Windows
		"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
		"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2; GWX:RED)",
		"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) NS8/0.9.6",

		// iPhone
		"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1",
		"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) CriOS/60.0.3112.89 Mobile/14F89 Safari/602.1",
		"Mozilla/5.0 (iPhone; CPU iPhone OS 9_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) OPiOS/14.0.0.104835 Mobile/13E233 Safari/9537.53",
		"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) FxiOS/8.1.1b4948 Mobile/14F89 Safari/603.2.4",

		// iPad
		"Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1",
		"Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/58.0.3029.113 Mobile/14F89 Safari/602.1",
		"Mozilla/5.0 (iPad; CPU OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) FxiOS/8.1.1b4948 Mobile/14F89 Safari/603.2.4",

		// Andorid
		"Mozilla/5.0 (Linux; Android 4.3; GT-I9300 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36",
		"Mozilla/5.0 (Android 4.3; Mobile; rv:54.0) Gecko/54.0 Firefox/54.0",
		"Mozilla/5.0 (Linux; Android 4.3; GT-I9300 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36 OPR/42.9.2246.119956",
		"Opera/9.80 (Android; Opera Mini/28.0.2254/66.318; U; en) Presto/2.12.423 Version/12.16",
		"Mozilla/5.0 (Linux; U; Android 4.3; en-us; GT-I9300 Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",

		"Mozilla/5.0 (Linux; Android 6.0.1; SAMSUNG SM-A310F/A310FXXU2BQB1 Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/5.4 Chrome/51.0.2704.106 Mobile Safari/537.36",
	}

	for _, s := range userAgents {
		ua := ua.Parse(s)
		fmt.Println()
		fmt.Println(ua.String)
		fmt.Println(strings.Repeat("=", len(ua.String)))
		fmt.Println("Name:", ua.Name, "v", ua.Version)
		fmt.Println("OS:", ua.OS, "v", ua.OSVersion)
		fmt.Println("Device:", ua.Device)
		if ua.Mobile {
			fmt.Println("(Mobile)")
		}
		if ua.Tablet {
			fmt.Println("(Tablet)")
		}
		if ua.Desktop {
			fmt.Println("(Desktop)")
		}
		if ua.Bot {
			fmt.Println("(Bot)")
		}
		if ua.URL != "" {
			fmt.Println(ua.URL)
		}

	}

}
Output:

func (UserAgent) IsAndroid

func (ua UserAgent) IsAndroid() bool

IsAndroid shorthand function to check if OS == Android

func (UserAgent) IsChrome

func (ua UserAgent) IsChrome() bool

IsChrome shorthand function to check if Name == Chrome

func (UserAgent) IsEdge

func (ua UserAgent) IsEdge() bool

IsEdge shorthand function to check if Name == Edge

func (UserAgent) IsFacebookbot

func (ua UserAgent) IsFacebookbot() bool

IsFacebookbot shorthand function to check if Name == FacebookExternalHit

func (UserAgent) IsFirefox

func (ua UserAgent) IsFirefox() bool

IsFirefox shorthand function to check if Name == Firefox

func (UserAgent) IsGooglebot

func (ua UserAgent) IsGooglebot() bool

IsGooglebot shorthand function to check if Name == Googlebot

func (UserAgent) IsIOS

func (ua UserAgent) IsIOS() bool

IsIOS shorthand function to check if OS == IOS

func (UserAgent) IsInternetExplorer

func (ua UserAgent) IsInternetExplorer() bool

IsInternetExplorer shorthand function to check if Name == Internet Explorer

func (UserAgent) IsLinux

func (ua UserAgent) IsLinux() bool

IsLinux shorthand function to check if OS == Linux

func (UserAgent) IsMacOS

func (ua UserAgent) IsMacOS() bool

IsMacOS shorthand function to check if OS == MacOS

func (UserAgent) IsOpera

func (ua UserAgent) IsOpera() bool

IsOpera shorthand function to check if Name == Opera

func (UserAgent) IsOperaMini

func (ua UserAgent) IsOperaMini() bool

IsOperaMini shorthand function to check if Name == Opera Mini

func (UserAgent) IsSafari

func (ua UserAgent) IsSafari() bool

IsSafari shorthand function to check if Name == Safari

func (UserAgent) IsTwitterbot

func (ua UserAgent) IsTwitterbot() bool

IsTwitterbot shorthand function to check if Name == Twitterbot

func (UserAgent) IsWindows

func (ua UserAgent) IsWindows() bool

IsWindows shorthand function to check if OS == Windows

Jump to

Keyboard shortcuts

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