Skip to main content

Python3 port of matomo's Device Detector

Project description

UA-Extract

UA-Extract is a precise and fast user agent parser and device detector written in Python, backed by the largest and most up-to-date user agent database, built on top of existing library device_detector.

UA-Extract will parse any user agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model. DeviceDetector detects thousands of user agent strings, even from rare and obscure browsers and devices.

The UA-Extract is optimized for speed of detection, by providing optimized code and in-memory caching.

This project originated as a Python port of the Universal Device Detection library. You can find the original code https://github.com/pranavagrawal321/UA-Extract.

Disclaimer

This port is not an exact copy of the original code; some Pythonic adaptations were used. However, it uses the original regex yaml files, to benefit from updates and pull request to both the original and the ported versions.

Installation

pip install ua_extract

Performance Options

CSafeLoader is used if pyyaml is configured --with-libyaml.

The mrab regex module is preferred if installed.

Usage

The regexes can outdated and may not provide correct output for newly released devices, so it's recommended to update regexes every once in a while.

from ua_extract import Regexes

Regexes().update_regexes()

This can also be done using CLI

ua_extract update_regexes

Note:

This requires Git to be installed and accessible as in to pull the updated regexes.

To get all info about the useragent.

from ua_extract import DeviceDetector

ua = 'Mozilla/5.0 (Linux; Android 4.3; C5502 Build/10.4.1.B.0.101)  google/1.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.136 Mobile Safari/537.36'

device = DeviceDetector(ua).parse()
print(device)

To get specific information.

from ua_extract import DeviceDetector

ua = 'Mozilla/5.0 (Linux; Android 4.3; C5502 Build/10.4.1.B.0.101) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.136 Mobile Safari/537.36'

device = DeviceDetector(ua).parse()
device.is_bot()
device.os_name()
device.os_version()
device.engine()
device.device_brand_name()
device.device_brand()
device.device_model()
device.device_type()

For much faster performance, skip Bot and Device Hardware Detection and extract get OS / App details only.

from ua_extract import SoftwareDetector

ua = 'Mozilla/5.0 (Linux; Android 6.0; 4Good Light A103 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Mobile Safari/537.36'
device = SoftwareDetector(ua).parse()

device.client_name()
device.client_short_name()
device.client_type()
device.client_version()
device.os_name()
device.os_version()
device.engine()
device.device_brand_name()
device.device_brand()
device.device_model()
device.device_type()

Many mobile browser UA strings contain the app info of the APP that's using the browser

ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 EtsyInc/5.22 rv:52200.62.0'
device = DeviceDetector(ua).parse()

device.secondary_client_name()
device.secondary_client_type()
device.secondary_client_version()

Updating from Matomo project

  1. Clone the Matomo project.
  2. Iterate through the various commits and copy the fixture files with the commands below.
  3. Review logic changes in the PHP files and implement them in the Python code.
  4. Run the tests and fix the ones that fail.
export upstream=/path/to/cloned/matomo/device-detector
export pdd=/path/to/python/ported/device_detector

cp $upstream/regexes/device/*.yml $pdd/device_detector/regexes/upstream/device/
cp $upstream/regexes/client/*.yml $pdd/device_detector/regexes/upstream/client/
cp $upstream/regexes/*.yml $pdd/device_detector/regexes/upstream/
cp $upstream/Tests/fixtures/* $pdd/device_detector/tests/fixtures/upstream/
cp $upstream/Tests/Parser/Client/fixtures/* $pdd/device_detector/tests/parser/fixtures/upstream/client/
cp $upstream/Tests/Parser/Device/fixtures/* $pdd/device_detector/tests/parser/fixtures/upstream/device/

NOTE:

This project is the clone of the Device Detector project by thinkwelltwd. The base code is same as before with some minor modifications as per requirements.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ua_extract-1.0.2.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ua_extract-1.0.2-py3-none-any.whl (1.3 MB view details)

Uploaded Python 3

File details

Details for the file ua_extract-1.0.2.tar.gz.

File metadata

  • Download URL: ua_extract-1.0.2.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ua_extract-1.0.2.tar.gz
Algorithm Hash digest
SHA256 78bb76279f44ee9c3d8ca46464a5f94b2c25f604a5e5fda538f1dc37df821ae9
MD5 ae5aa6ad8c4656e30f7bdffa21b4de99
BLAKE2b-256 e8e45b60b5822f9907bdfe15e1cd6c061b13d0ff90a5b13b78330cdd31eb3515

See more details on using hashes here.

File details

Details for the file ua_extract-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: ua_extract-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ua_extract-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ba9d49446f61069d2861aea59a2e5d5ff960e61b502050199d109bdb5144be34
MD5 0b4c5a2b864465cc3267de26b2e279b3
BLAKE2b-256 70b1a67697f7413892f380274cacb18dc06aad5f9dec828a7fb8b2aedbb8e091

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page