\r\n

51Degrees Device Detection Python  4.3

Device Detection services for 51Degrees Pipeline

cloud/useragentclienthints.py

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 ---------------------------------------
2 This example demonstrates detection using user-agent client hints.
3 The sec-ch-ua value can be used to determine the browser of the connecting device, but not other components such as the hardware.
4 We show this by first performing detection with sec-ch-ua only.
5 We then repeat with the user-agent header set as well. Note that the client hint takes priority over the user-agent.
6 Finally, we use both sec-ch-ua and user-agent.Note that sec-ch-ua takes priority over the user-agent for detection of the browser.
7 ---------------------------------------
8 Sec-CH-UA = '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"'
9 User-Agent = 'NOT_SET'
10  Browser = Chrome 89
11  IsMobile = No matching profiles could be found for the supplied evidence.A 'best guess' can be returned by configuring more lenient matching rules.See https://51degrees.com/documentation/_device_detection__features__false_positive_control.html
12 
13 Sec-CH-UA = 'NOT_SET'
14 User-Agent = 'Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36'
15  Browser = Samsung Browser 10.1
16  IsMobile = True
17 
18 Sec-CH-UA = '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"'
19 User-Agent = 'Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36'
20  Browser = Chrome 89
21  IsMobile = 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 
55 
56 from fiftyone_devicedetection_cloud.devicedetection_cloud_pipelinebuilder import DeviceDetectionCloudPipelineBuilder
57 
58 # You need to create a resource key at https://configure.51degrees.com
59 # and paste it into the code, replacing !!YOUR_RESOURCE_KEY!! below.
60 # Alternatively, add a resource_key environment variable
61 import os
62 if "resource_key" in os.environ:
63  resource_key = os.environ["resource_key"]
64 else:
65  resource_key = "!!YOUR_RESOURCE_KEY!!"
66 
67 if resource_key == "!!YOUR_RESOURCE_KEY!!":
68  print("""
69  You need to create a resource key at
70  https://configure.51degrees.com and paste it into the code,
71  'replacing !!YOUR_RESOURCE_KEY!!
72  To include the properties used in this example, go to https://configure.51degrees.com/
73  """)
74 else:
75 
76  # First create the device detection pipeline with the desired settings.
77 
78  print("---------------------------------------");
79  print("This example demonstrates detection " +
80  "using user-agent client hints.\n")
81  print("The sec-ch-ua value can be used to " +
82  "determine the browser of the connecting device, " +
83  "but not other components such as the hardware.\n")
84  print("We show this by first performing " +
85  "detection with sec-ch-ua only.\n")
86  print("We then repeat with the user-agent " +
87  "header set as well. Note that the client hint takes " +
88  "priority over the user-agent.\n")
89  print("Finally, we use both sec-ch-ua and " +
90  "user-agent. Note that sec-ch-ua takes priority " +
91  "over the user-agent for detection of the browser.\n")
92  print("---------------------------------------\n")
93 
94  mobile_ua = "Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) " + \
95  "AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/10.1 " + \
96  "Chrome/71.0.3578.99 Mobile Safari/537.36"
97  secchua_value = '\"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"'
98 
99  pipeline = DeviceDetectionCloudPipelineBuilder({
100  "resource_key": resource_key
101  }).build()
102 
103  # Define function to analyze user-agent/client hints
104 
105  def analyze_client_hints(pipeline, setUserAgent, setSecChUa):
106 
107  # We create a FlowData object from the pipeline
108  # this is used to add evidence to and then process
109  flowdata = pipeline.create_flowdata()
110 
111  # Add a value for the user-agent client hints header
112  # sec-ch-ua as evidence
113  if setSecChUa:
114  flowdata.evidence.add("query.sec-ch-ua", secchua_value)
115 
116  # Also add a user-agent if requested
117  if setUserAgent:
118  flowdata.evidence.add("query.user-agent", mobile_ua)
119 
120  flowdata.process()
121 
122  device = flowdata.device
123 
124  browser_name = device.browsername
125  browser_version = device.browserversion
126  ismobile = device.ismobile
127 
128  # Output evidence
129  secchua = "NOT_SET"
130  if setSecChUa:
131  secchua = secchua_value
132  print("Sec-CH-UA = '{}'\n".format(secchua))
133 
134  ua = "NOT_SET"
135  if setUserAgent:
136  ua = mobile_ua
137  print("User-Agent = '{}'\n".format(ua))
138 
139  # Output the Browser
140  if (browser_name.has_value() and browser_version.has_value()):
141  print("\tBrowser = {} {}\n".format(browser_name.value(), browser_version.value()))
142  elif (browser_name.has_value()):
143  print("\tBrowser = {} (version unknown)\n".format(browser_name.value()))
144  else:
145  print("\tBrowser = {}\n".format(browser_name.no_value_message()))
146 
147  # Output the value of the 'IsMobile' property.
148  if (ismobile.has_value()):
149  print("\tIsMobile = {}\n\n".format(ismobile.value()))
150  else:
151  print("\tIsMobile = {}\n\n".format(ismobile.no_value_message()))
152 
153  # first try with just sec-ch-ua.
154  analyze_client_hints(pipeline, False, True)
155 
156  # Now with just user-agent.
157  analyze_client_hints(pipeline, True, False)
158 
159  # Finally, perform detection with both.
160  analyze_client_hints(pipeline, True, True)
161