No description
Find a file
2022-03-10 11:55:25 +07:00
src Use format instead multiReplace 2022-03-10 11:28:15 +07:00
tests Rewrok tests 2022-03-08 17:34:09 +07:00
tools Style yaml2json 2022-03-07 00:34:15 +07:00
uap-core@bbd43aed9a Update uap-core 2022-01-31 02:05:08 +07:00
.gitignore Add entries to .gitignore 2022-02-03 08:30:35 +07:00
.gitlab-ci.yml Add libpcre to CI 2022-01-31 12:43:28 +07:00
.gitmodules Add tests. Add uap-core submodule 2021-01-23 02:36:16 +07:00
LICENSE Add LICENSE 2022-02-11 06:52:06 +00:00
nimdoc.cfg Add CI 2021-01-23 14:40:21 +07:00
README.md Fix coverage report link in README 2022-03-07 00:33:01 +07:00
uap.nimble Update nimble version 2022-03-10 11:55:25 +07:00

User agent parser

GitLab CI Build Status coverage report License

Nim implementation of user-agent parser. Based on the regexes provided by the uap-core project.

installation

nimble install https://gitlab.com/artemklevtsov/nim-uap

Usage

Cli

TEST_CASES_URL="https://raw.githubusercontent.com/ua-parser/uap-core/master/tests/test_ua.yaml"
# yq link: https://github.com/stedolan/jq/releases
curl -sS "${TEST_CASES_URL}" | yq eval '.test_cases | .[].user_agent_string' > /tmp/ua.txt

# install package
export PATH=$PATH:$HOME/.nimble/bin/
nimble install -y https://gitlab.com/artemklevtsov/nim-uap
# getting help
uap --help
# works with stdin and stdout by default
head /tmp/ua.txt | uap --format json --json-pretty
# writes to csv
uap --input /tmp/ua.txt --output /tmp/ua.parsed.csv --format csv
uap --input /tmp/ua.txt --output /tmp/ua.parsed.tsv --format csv -s '\t'
# writes to json
uap --input /tmp/ua.txt --output /tmp/ua.parsed.json --format json

Library

import uap

const s = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3"
# parse user agent
let ua = parseUserAgent(s)
echo($ua.browser)            # Mobile Safari 5.1.0
echo($ua.os)                 # iOS 5.1.1
echo($ua.device)             # iPhone
echo(ua.browser.version)     # 5.1.0
echo(ua.os.version)          # 5.1.1
echo($ua.device.isSpider)    # false
echo($ua.isSpider)           # false
# detect device type
let dt = parseDeviceType(s)
echo($dt)                    # Mobile
# parse browser
echo($parseBrowser(s))       # Mobile Safari 5.1.0
# parse os
echo($parseOS(s))            # iOS 5.1.1
# parse device
echo($parseDevice(s))        # iPhone