Skip to content
/ uap-lua Public

A simple lua package for parsing user agent strings with the help of BrowserScope's UA database

License

Notifications You must be signed in to change notification settings

dukex/uap-lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

uap-lua

A simple lua package for parsing user agent strings with the help of BrowserScope's UA database

This package is tested on +17500 user agents string, we have check to more than +300 operation systems, +1200 browsers and +16000 devices.

Pre-installation

  • libpcre2 to compile the regexes in regexes.yml

On Arch:

$ pacman -S pcre2

On Ubuntu:

$ apt-get install libpcre2-dev

Installation

Install package uap-lua using luarocks

$ luarocks install uap-lua

Usage

Require the uap in your file

local uap = require('uap')

.parse

The .parse function expect the user agent as string and returns a table with user agent data

local user_agent = uap.parse 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0;)'

print(user_agent.ua.family)
-- IE
print(user_agent.ua.major)
--> 9
print(user_agent.ua.minor)
--> 0
print(user_agent.os.family)
--> Windows
print(user_agent.os.major)
--> Vista

Device information can also be determined from some devices

user_agent = uap.parse "Mozilla/5.0 (Linux; Android 7.0; SAMSUNG SM-G930T Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/5.0 Chrome/51.0.2704.106 Mobile Safari/537.36"

print(user_agent.device.family)
--> Samsung SM-G930T
print(user_agent.device.brand)
--> Samsung
print(user_agent.device.model)
--> "SM-G930T"

user_agent = uap.parse "Mozilla/5.0 (iPad; CPU OS 10_2_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) GSA/23.1.148956103 Mobile/14D27 Safari/600.1.4"

print(user_agent.device.family)
--> iPad
print(user_agent.device.brand)
--> Apple
print(user_agent.device.model)
--> iPad

Take a look the entire table of the previous examples

{
  device = {
    family = "Other"
  },
  os = {
    family = "Windows",
    major = "Vista"
  },
  string = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0;)",
  ua = {
    family = "IE",
    major = "9",
    minor = "0"
  }
}

--

{
  device = {
    brand = "Samsung",
    family = "Samsung SM-G930T",
    model = "SM-G930T"
  },
  os = {
    family = "Android",
    major = "7",
    minor = "0"
  },
  string = "Mozilla/5.0 (Linux; Android 7.0; SAMSUNG SM-G930T Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/5.0 Chrome/51.0.2704.106 Mobile Safari/537.36",
  ua = {
    family = "Samsung Internet",
    major = "5",
    minor = "0"
  }
}

--

{
  device = {
    brand = "Apple",
    family = "iPad",
    model = "iPad"
  },
  os = {
    family = "iOS",
    major = "10",
    minor = "2",
    patch = "1"
  },
  string = "Mozilla/5.0 (iPad; CPU OS 10_2_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) GSA/23.1.148956103 Mobile/14D27 Safari/600.1.4",
  ua = {
    family = "Mobile Safari",
    major = "10",
    minor = "2",
    patch = "1"
  }
}

The table follow the uap-core specification

Contributing

  • luarocks install lyaml && luarocks install inspect
  • Fork
  • Code
  • luarocks make --local && lua test/device_test.lua && lua test/os_test.lua && lua test/ua_test.lua
  • Send a pull request

Publishing

  1. Create ./rockspecs/ file

  2. Run:

$ luarocks install dkjson
$ luarocks upload rockspecs/uap-lua-1.3-1.rockspec --api-key=...

About

A simple lua package for parsing user agent strings with the help of BrowserScope's UA database

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages