\r\n

51Degrees Device Detection Python  4.3Newer Version 4.4

Device Detection services for 51Degrees Pipeline

onpremise/failuretomatch.py

This example shows how the 51Degrees device detection engine deals with evidence that does not match any known device. In this scenario, 'HasValue' can be used to check if the property has been populated by the engine. If it hasn't then 'NoValueMessage' can be used to get the reason why.

Expected output:

1 Is user agent '--' a mobile?
2 The results contained a null profile for the component which the required property belongs to.
3 
4 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?
5 true
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 from fiftyone_devicedetection_examples.example_utils import ExampleUtils
40 
41 # First create the device detection pipeline with the desired settings.
42 
43 data_file = ExampleUtils.find_file("51Degrees-LiteV4.1.hash")
44 
45 pipeline = DeviceDetectionOnPremisePipelineBuilder(
46  data_file_path = data_file,
47  licence_keys = "",
48  performance_profile = 'MaxPerformance',
49  auto_update=False).build()
50 
51 # We create a FlowData object from the pipeline
52 # this is used to add evidence to and then process
53 
54 flowdata1 = pipeline.create_flowdata()
55 
56 # Here we add a User-Agent of an iphone as evidence
57 
58 iphone_ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114"
59 
60 flowdata1.evidence.add("header.user-agent", iphone_ua)
61 
62 # Now we process the FlowData
63 
64 flowdata1.process()
65 
66 # To check whether the User-Agent is a mobile device we look at the ismobile property
67 # inside the Device Detection Engine
68 
69 # first we check if this has a meaningful result
70 
71 print("Is User-Agent " + iphone_ua + " a mobile device?: ")
72 if flowdata1.device.ismobile.has_value():
73  print(flowdata1.device.ismobile.value())
74 else:
75  # Output why the value isn't meaningful
76  print(flowdata1.device.ismobile.no_value_message())
77 
78 # Now we do the same with a new User-Agent, this time a corrupted one
79 
80 badUA = "--"
81 
82 flowdata2 = pipeline.create_flowdata()
83 
84 flowdata2.evidence.add("header.user-agent", badUA)
85 
86 flowdata2.process()
87 
88 print("Is User-Agent " + badUA + " a mobile device?: ")
89 if flowdata2.device.ismobile.has_value():
90  print(flowdata2.device.ismobile.value())
91 else:
92  # Output why the value isn't meaningful
93  print(flowdata2.device.ismobile.no_value_message())