51Degrees Device Detection Python  4.1

Device Detection services for 51Degrees Pipeline

cloud/gettingstarted.py

This example shows how to use the 51Degrees Cloud service to determine whether a given User-Agent corresponds to a mobile device or not.

To run this example, you will need to create a resource key. The resource key is used as shorthand to store the particular set of properties you are interested in as well as any associated license keys that entitle you to increased request limits and/or paid-for properties.

You can create a resource key using the 51Degrees Configurator.

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 
38 
39 from fiftyone_devicedetection.devicedetection_pipelinebuilder import DeviceDetectionPipelineBuilder
40 
41 # First create the device detection pipeline with the desired settings.
42 
43 # You need to create a resource key at https://configure.51degrees.com
44 # and paste it into the code, replacing !!YOUR_RESOURCE_KEY!! below.
45 
46 resourceKey = "!!YOUR_RESOURCE_KEY!!"
47 
48 if resourceKey == "!!YOUR_RESOURCE_KEY!!":
49  print("""
50  You need to create a resource key at
51  https://configure.51degrees.com and paste it into the code,
52  'replacing !!YOUR_RESOURCE_KEY!!
53  To include the properties used in this example, go to https://configure.51degrees.com/bxXqZhLT
54  """)
55 else:
56 
57  pipeline = DeviceDetectionPipelineBuilder({"resourceKey": resourceKey}).build()
58 
59  # We create a FlowData object from the pipeline
60  # this is used to add evidence to and then process
61 
62  flowData1 = pipeline.create_flowdata()
63 
64  # Here we add a User-Agent of a desktop as evidence
65 
66  desktopUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"
67 
68  flowData1.evidence.add("header.user-agent", desktopUA)
69 
70  # Now we process the FlowData
71 
72  flowData1.process()
73 
74  # To check whether the User-Agent is a mobile device we look at the ismobile property
75  # inside the Device Detection Engine
76 
77  # first we check if this has a meaningful result
78 
79  print("Is User-Agent " + desktopUA + " a mobile? ")
80  if flowData1.device.ismobile.has_value():
81  print(flowData1.device.ismobile.value())
82  else:
83  # Output why the value isn't meaningful
84  print(flowData1.device.ismobile.no_value_message())
85 
86  # Now we do the same with a new User-Agent, this time from an iphone
87 
88  flowData2 = pipeline.create_flowdata()
89 
90  iphoneUA = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114"
91 
92  flowData2.evidence.add("header.user-agent", iphoneUA)
93 
94  flowData2.process()
95 
96  print("Is User-Agent " + iphoneUA + " a mobile device?: ")
97  if flowData2.device.ismobile.has_value():
98  print(flowData2.device.ismobile.value())
99  else:
100  # Output why the value isn't meaningful
101  print(flowData2.device.ismobile.no_value_message())