\r\n

51Degrees Device Detection Python  4.3

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_cloud.devicedetection_cloud_pipelinebuilder import DeviceDetectionCloudPipelineBuilder
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 # Alternatively, add a resource_key environment variable
46 import os
47 if "resource_key" in os.environ:
48  resource_key = os.environ["resource_key"]
49 else:
50  resource_key = "!!YOUR_RESOURCE_KEY!!"
51 
52 if resource_key == "!!YOUR_RESOURCE_KEY!!":
53  print("""
54  You need to create a resource key at
55  https://configure.51degrees.com and paste it into the code,
56  'replacing !!YOUR_RESOURCE_KEY!!
57  To include the properties used in this example, go to https://configure.51degrees.com/bxXqZhLT
58  """)
59 else:
60 
61  pipeline = DeviceDetectionCloudPipelineBuilder({
62  "resource_key": resource_key
63  }).build()
64 
65  # We create a FlowData object from the pipeline
66  # this is used to add evidence to and then process
67 
68  flowdata1 = pipeline.create_flowdata()
69 
70  # Here we add a User-Agent of a desktop as evidence
71 
72  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"
73 
74  flowdata1.evidence.add("header.user-agent", desktop_ua)
75 
76  # Now we process the FlowData
77 
78  flowdata1.process()
79 
80  # To check whether the User-Agent is a mobile device we look at the ismobile property
81  # inside the Device Detection Engine
82 
83  # first we check if this has a meaningful result
84 
85  print("Is User-Agent " + desktop_ua + " a mobile? ")
86  if flowdata1.device.ismobile.has_value():
87  print(flowdata1.device.ismobile.value())
88  else:
89  # Output why the value isn't meaningful
90  print(flowdata1.device.ismobile.no_value_message())
91 
92  # Now we do the same with a new User-Agent, this time from an iphone
93 
94  flowdata2 = pipeline.create_flowdata()
95 
96  iphone_ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114"
97 
98  flowdata2.evidence.add("header.user-agent", iphone_ua)
99 
100  flowdata2.process()
101 
102  print("Is User-Agent " + iphone_ua + " a mobile device?: ")
103  if flowdata2.device.ismobile.has_value():
104  print(flowdata2.device.ismobile.value())
105  else:
106  # Output why the value isn't meaningful
107  print(flowdata2.device.ismobile.no_value_message())