ua

package module
v0.0.0-...-c4395d4 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2022 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

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

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