\r\n

51Degrees Device Detection Python  4.3

Device Detection services for 51Degrees Pipeline

hash/useragentclienthints.py

This example requires a local data file. Free data files can be acquired by pulling the submodules under this repository or from the device-detection-data GitHub repository.

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_onpremise.devicedetection_onpremise_pipelinebuilder import DeviceDetectionOnPremisePipelineBuilder
57 
58 # First create the device detection pipeline with the desired settings.
59 
60 data_file = "./fiftyone_devicedetection_onpremise/device-detection-cxx/device-detection-data/51Degrees-LiteV4.1.hash"
61 
62 print("Using data file at '{}'\n".format(data_file));
63 print("---------------------------------------")
64 print("This example demonstrates detection " +
65  "using user-agent client hints.\n")
66 print("The sec-ch-ua value can be used to " +
67  "determine the browser of the connecting device, " +
68  "but not other components such as the hardware.\n")
69 print("We show this by first performing " +
70  "detection with sec-ch-ua only.\n")
71 print("We then repeat with the user-agent " +
72  "header set as well. Note that the client hint takes " +
73  "priority over the user-agent.\n")
74 print("Finally, we use both sec-ch-ua and " +
75  "user-agent. Note that sec-ch-ua takes priority " +
76  "over the user-agent for detection of the browser.\n")
77 print("---------------------------------------\n")
78 
79 mobile_ua = "Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) " + \
80  "AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/10.1 " + \
81  "Chrome/71.0.3578.99 Mobile Safari/537.36"
82 secchua_value = "\"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\""
83 
84 pipeline = DeviceDetectionOnPremisePipelineBuilder(
85  data_file_path = data_file,
86  licence_keys = "",
87  performance_profile = 'MaxPerformance',
88  auto_update=False).build()
89 
90 # Define function to analyze user-agent/client hints
91 
92 def analyze_client_hints(pipeline, setUserAgent, setSecChUa):
93 
94  # We create a FlowData object from the pipeline
95  # this is used to add evidence to and then process
96  flowdata = pipeline.create_flowdata()
97 
98  # Add a value for the user-agent client hints header
99  # sec-ch-ua as evidence
100  if setSecChUa:
101  flowdata.evidence.add("query.sec-ch-ua", secchua_value)
102 
103  # Also add a standard user-agent if requested
104  if setUserAgent:
105  flowdata.evidence.add("query.user-agent", mobile_ua)
106 
107  flowdata.process()
108 
109  device = flowdata.device
110 
111  browser_name = device.browsername
112  browser_version = device.browserversion
113  ismobile = device.ismobile
114 
115  # Output evidence
116  secchua = "NOT_SET"
117  if setSecChUa:
118  secchua = secchua_value
119  print("Sec-CH-UA = '{}'\n".format(secchua))
120 
121  ua = "NOT_SET"
122  if setUserAgent:
123  ua = mobile_ua
124  print("User-Agent = '{}'\n".format(ua))
125 
126  # Output the Browser
127  if (browser_name.has_value() and browser_version.has_value()):
128  print("\tBrowser = {} {}\n".format(browser_name.value(), browser_version.value()))
129  elif (browser_name.has_value()):
130  print("\tBrowser = {} (version unknown)\n".format(browser_name.value()))
131  else:
132  print("\tBrowser = {}\n".format(browser_name.no_value_message()))
133 
134  # Output the value of the 'IsMobile' property.
135  if (ismobile.has_value()):
136  print("\tIsMobile = {}\n\n".format(ismobile.value()))
137  else:
138  print("\tIsMobile = {}\n\n".format(ismobile.no_value_message()))
139 
140 # first try with just sec-ch-ua.
141 analyze_client_hints(pipeline, False, True)
142 
143 # Now with just user-agent.
144 analyze_client_hints(pipeline, True, False)
145 
146 # Finally, perform detection with both.
147 analyze_client_hints(pipeline, True, True)
148