\r\n

51Degrees Device Detection Python  4.2Newer Version 4.3

Device Detection services for 51Degrees Pipeline

hash/gettingstarted.py

This example shows how to use 51Degrees on-premise device detection to determine whether a given User-Agent corresponds to a mobile device or not.

Expected output:

1 Is User-Agent 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114' a mobile?
2 true
3 
4 Is User-Agent 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36' a mobile?
5 false
1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4 # Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
5 #
6 # This Original Work is licensed under the European Union Public Licence (EUPL)
7 # v.1.2 and is subject to its terms as set out below.
8 #
9 # If a copy of the EUPL was not distributed with this file, You can obtain
10 # one at https://opensource.org/licenses/EUPL-1.2.
11 #
12 # The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 # amended by the European Commission) shall be deemed incompatible for
14 # the purposes of the Work and the provisions of the compatibility
15 # clause in Article 5 of the EUPL shall not apply.
16 #
17 # If using the Work as, or as part of, a network application, by
18 # including the attribution notice(s) required under Article 5 of the EUPL
19 # in the end user terms of the application under an appropriate heading,
20 # such notice(s) shall fulfill the requirements of that article.
21 # ********************************************************************
22 
23 
37 
38 from fiftyone_devicedetection_onpremise.devicedetection_onpremise_pipelinebuilder import DeviceDetectionOnPremisePipelineBuilder
39 
40 # First create the device detection pipeline with the desired settings.
41 
42 data_file = "fiftyone_devicedetection_onpremise/device-detection-cxx/device-detection-data/51Degrees-LiteV4.1.hash"
43 
44 pipeline = DeviceDetectionOnPremisePipelineBuilder(
45  data_file_path = data_file,
46  licence_keys = "",
47  performance_profile = 'MaxPerformance',
48  auto_update=False).build()
49 
50 # We create a FlowData object from the pipeline
51 # this is used to add evidence to and then process
52 
53 flowdata1 = pipeline.create_flowdata()
54 
55 # Here we add a User-Agent of a desktop as evidence
56 
57 desktop_ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"
58 
59 flowdata1.evidence.add("header.user-agent", desktop_ua)
60 
61 # Now we process the FlowData
62 
63 flowdata1.process()
64 
65 # To check whether the User-Agent is a mobile device we look at the ismobile property
66 # inside the Device Detection Engine
67 
68 # first we check if this has a meaningful result
69 
70 print("Is User-Agent " + desktop_ua + " a mobile? ")
71 if flowdata1.device.ismobile.has_value():
72  print(flowdata1.device.ismobile.value())
73 else:
74  # Output why the value isn't meaningful
75  print(flowdata1.device.ismobile.no_value_message())
76 
77 # Now we do the same with a new User-Agent, this time from an iphone
78 
79 flowdata2 = pipeline.create_flowdata()
80 
81 iphone_ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114"
82 
83 flowdata2.evidence.add("header.user-agent", iphone_ua)
84 
85 flowdata2.process()
86 
87 print("Is User-Agent " + iphone_ua + " a mobile device?: ")
88 if flowdata2.device.ismobile.has_value():
89  print(flowdata2.device.ismobile.value())
90 else:
91  # Output why the value isn't meaningful
92  print(flowdata2.device.ismobile.no_value_message())