Skip to content
Branch: master
Find file History
ben51degrees Merged PR 1174: RELEASE: Version 3.2.19.3 released.
* OPTIM: Improved Node.JS auto update functionality
* BUILD: Node.JS Lite data file packages are now dev dependencies rather than full dependencies. This means that they are not installed in a production environment unless explicitely included.
* BUG: The case where a single, unterminated, header was passed to the fiftyoneDegreesMatchForHttpHeaders method was not properly handled.
Latest commit a7c98d2 Feb 4, 2019
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
core
examples
pattern
tests
trie
.gitignore Initial commit Jun 27, 2017
README.md Initial commit Jun 27, 2017

README.md

51Degrees Device Detection in C Python wrapper

Supported Databases | Developer Documention | Available Properties

Need .NET | Java | PHP Script?

Introduction

In Python, use like...

>>> from FiftyOneDegrees import fiftyone_degrees_mobile_detector_v3_wrapper
>>> from fiftyone_degrees.mobile_detector.conf import settings
>>> provider = fiftyone_degrees_mobile_detector_v3_wrapper.Provider(settings.V3_WRAPPER_DATABASE,settings.PROPERTIES,settings.CACHE_SIZE, settings.POOL_SIZE)
>>> device = provider.getMatch(httpHeaders)
>>> print device.getValue('IsMobile')
>>> print device.getValue('DeviceType')

... to turn User-Agent HTTP headers into useful information about physical screen size, device price and type of device.

Use this project to detect device properties using HTTP browser User-Agents as input. It can be used to process server web log files, or for real time device detection to support web optimisation.

Two detection methods are supported.

Pattern: Searches for device signatures in a useragent returning metrics about the validity of the results. Does NOT use regular expressions.

Trie: A large binary Trie (pronounced Try) populated with User-Agent signatures. Very fast.

All methods use an external data file which can easilly be updated.

Dependencies

Essential

  • python
  • python-setuptools
  • python-dev
  • python-pip
  • g++

For Ubuntu based distributions these can be found on apt, use

$ sudo apt-get install python python-setuptools python-dev python-pip g++

If building from this git repository you will also need

  • make
  • SWIG

For Ubuntu based distributions these can be found on apt, use

$ sudo apt-get install make swig

Optional

To use the Django example you will need

  • django

For Ubuntu based distributions these can be found on apt, use

$ sudo pip install django

Install

Linux

From pip

This is the quickest and easiest way to install 51Degrees python detector. Just use

$ sudo pip install 51degrees-mobile-detector-v3-wrapper

or

$ sudo pip install 51degrees-mobile-detector-v3-trie-wrapper

Note: 51degrees-mobile-detector will be installed as a dependency.

From git

However, if you want to build the package yourself and use the examples too. First clone 51Degrees/Device-Detection repository with

$ git clone https://github.com/51Degrees/Device-Detection.git

Move to the Core directory with

$ cd Device-Detection/python/core

and install with

$ sudo make install

Now move to the directory for you chosen detection method (i.e. Pattern or Trie) with

$ cd ../[package name]

Then install with

$ sudo make install

When you're done installing, check it's set up correctly by recreating the output in the Usage section below. Or if you've cloned this repository, try the examples.

Configure

Before start matching user agents, you may wish to configure the solution to use a different datadase for example. You can easily generate a sample settings file running the following command

$ 51degrees-mobile-detector settings > ~/51degrees-mobile-detector.settings.py

Edit the generated settings and set your preferences. Ensure you set DETECTION_METHOD and V3_WRAPPER_DATABASE, orTRIE_V3_WRAPPER_DATABASE (if using the trie-based detection method).

Finally, link your settings file from the FIFTYONE_DEGREES_MOBILE_DETECTOR_SETTINGS environment variable

$ export FIFTYONE_DEGREES_MOBILE_DETECTOR_SETTINGS=~/51degrees-mobile-detector.settings.py

This can be permanently set by adding this line to your ~/.profile file.

Settings

General Settings

  • DETECTION_METHOD (defaults to 'v3-wrapper'). Sets the preferred mobile device detection method. Available options are v3-wrapper (requires 51degrees-mobile-detector-v3-wrapper package), v3-trie-wrapper (requires 51degrees-mobile-detector-v3-trie-wrapper package).

  • PROPERTIES (defaults to ''). List of case-sensitive property names to be fetched on every device detection. Leave empty to fetch all available properties.

  • LICENSE. Your 51Degrees license key. This is required if you want to set up the automatic 51degrees-mobile-detector-premium-pattern-wrapper package updates.

Trie Detector Settings

  • V3_TRIE_WRAPPER_DATABASE. Location of the Trie database file.

Pattern Detector Settings

  • V3_WRAPPER_DATABASE. Location of the Pattern database file.

  • CACHE_SIZE (defaults to 10000). Sets the size of the workset cache.

  • POOL_SIZE (defaults to 20). Sets the size of the workset pool.

Usage Sharer Settings

  • USAGE_SHARER_ENABLED (defaults to True). Indicates if usage data should be shared with 51Degrees.com. We recommended leaving this value unchanged to ensure we're improving the performance and accuracy of the solution.

  • Adavanced usage sharer settings are detailed in your settings file.

Automatic updates

If you want to set up automatic updates, add your license key to your settings and add the 51degrees-mobile-detector update-premium-pattern-wrapper command to your cron.

Usage

Core

By executing the following a useful help page will be displayed explaining basic usage.

$ 51degrees-mobile-detector

To check everything is set up , try fetching a match with

$ 51degrees-mobile-detector match "Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176"

Alternatively, open a Python console, load the module and start matching

>>> from fiftyone_degrees import mobile_detector

>>> device = mobile_detector.match("Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176")

>>> device.Id
'15767-18117-17776-18092'

>>> device.BrowserName
'MobileSafari'

>>> device.ScreenPixelsWidth
'1024'

>>> device.method
'v3-wrapper'

Wrapper

To use your chosen wrapper without going through the core, open a Python console, load the module and start matching

>>> from FiftyOneDegrees import fiftyone_degrees_mobile_detector_v3_wrapper

>>> from fiftyone_degrees.mobile_detector.conf import settings

>>> provider = fiftyone_degrees_mobile_detector_v3_wrapper.Provider(settings.V3_WRAPPER_DATABASE,settings.PROPERTIES,settings.CACHE_SIZE, settings.POOL_SIZE)

>>> device = provider.getMatch("Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176")

>>> device.getDeviceId()
'15767-18117-17776-18092'

>>> device.getValue('BrowserName')
'Mobile Safari'

>>> device.getValue('ScreenPixelsWidth')
'1024'

Examples

In the examples folder, you can find examples of various functionalities that the 51Degrees detector has such as:

  • Matching with a User-Agent
  • Matching with a device id
  • Evaluating match metrics
  • Offline processing
  • Strongly typed variables
  • Finding profiles

A full explanation of these can be found within the files or at Python Tutorials.

Both Pattern and Trie packages also include the following examples

Note: If they are not already, be sure to mark these as executable first with

$ chmod +x console.py server.py

console.py which takes no input and returns matches from predefined User-Agent and HTTP headers seperately. Is run with

$ ./console.py

server.py which runs a server at localhost:8080 by default and returns matches from the web browsers User-Agent and all relevant HTTP headers seperately. Is run with

$ ./server.py

Django

The core installation includes a Django middleware. A primative example can be found in core/django-example. To run use

$ python manage.py runserver
You can’t perform that action at this time.