GithubHelp home page GithubHelp logo

umutbasal / device-detector-go Goto Github PK

View Code? Open in Web Editor NEW
13.0 4.0 2.0 228 KB

The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model. https://pkg.go.dev/github.com/umutbasal/device-detector-go

License: GNU Lesser General Public License v3.0

Go 100.00%
detection go user-agent bot v8 bot-detection detection-library mobile-detection

device-detector-go's Introduction

device-detector-go

GitHub issues GitHub stars GitHub license

Device-detector-go is a precise user agent parser and device detector written in Golang, backed by the largest and most up-to-date open-source user agent database and library written js.

Device-detector-go will parse any user agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model. Works with go and in the browser.

This library uses [device-detector-js](https://github.com/etienne-martin/device-detector-js) and originial library maintainer says;
This library is heavily tested and relies on over 10,000 tests to detect thousands of user agent strings, even from rare and obscure browsers and devices.

This is a go port of device-detector-js (uses this library in v8 engine directly and exports logics) and relatively based on Matomo device-detector rules

Why this project

There is too many go user agent parsers, but none of them has powerful rules as Matomo device-detector and there is only one golang port of matomo (detector gamebtc/devicedetector), but this one works heavy like 40ms. This is too much. This projects aim is using speed of the nodejs repo. And not implement the go version from scratch.

Getting Started

Installation

To use device-detector-go in your project, run:

go get github.com/umutbasal/device-detector-go

Usage

GO import

import (
 device detector "github.com/umutbasal/device-detector-go"
)

Example - user agent detection:

import (
  devicedetector "github.com/umutbasal/device-detector-go"
)

func main() {
  userAgent := "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36"
  detector, err := devicedetector.NewDeviceDetector(devicedetector.DeviceDetectorOptions{})
  if err != nil {
    panic(err)
  }
  result, err := detector.Parse(user)
  if err != nil {
    panic(err)
  }

  fmt.Printf("%s\n", result)

}

Output:

{
 "client": {
  "type": "browser",
  "name": "Chrome Mobile",
  "version": "41.0",
  "engine": "Blink",
  "engineVersion": ""
 },
 "os": {
  "name": "Android",
  "version": "6.0",
  "platform": ""
 },
 "device": {
  "type": "smartphone",
  "brand": "Google",
  "model": "Nexus 5X"
 },
 "bot": null
}

Example - bot detection:

import (
 devicedetector "github.com/umutbasal/device-detector-go"
)

func main() {
 userAgent := "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36"
 detector, err := devicedetector.NewBotDetector(devicedetector.DeviceDetectorOptions{})
 if err != nil {
  panic(err)
 }
 result, err := detector.Parse(user)
 if err != nil {
  panic(err)
 }

 fmt.Printf("%s\n", result)
}

Output:

{
 "name": "Googlebot",
 "category": "Search bot",
 "url": "http://www.google.com/bot.html",
 "producer": {
  "name": "Google Inc.",
  "url": "http://www.google.com"
 }
}

What device-detector-go is able to detect

There is a list in original js repo

Built with

Contributing

When contributing to this project, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.

Update the README.md with details of changes to the library.

Execute go test -v and update the tests if needed.

License

This is a free/libre library under license LGPL v3 or later.

device-detector-go's People

Contributors

umutbasal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

device-detector-go's Issues

use example NewBotDetector panic

userAgent := "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36"
	detector, err := devicedetector.NewBotDetector(devicedetector.DeviceDetectorOptions{})
	if err != nil {
		panic(err)
	}
	result, err := detector.Parse(userAgent)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s\n", result)

panic: TypeError: Cannot convert undefined or null to object

goroutine 1 [running]:
rogchap.com/v8go.(*Value).Object(0xc000098110)
/Users/-/go/pkg/mod/rogchap.com/[email protected]/value.go:235 +0xc6
github.com/umutbasal/device-detector-go.NewBotDetector.func1({0x100f4e553, 0x89})
/Users/-/go/pkg/mod/github.com/umutbasal/[email protected]/device-detector.go:89 +0xfb
main.main()
/Users/.../main.go:16 +0x3d

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.