This example shows how to use 51Degrees on-premise device detection to determine details about a
device based on its User-Agent and User-Agent Client Hint HTTP header values.
This example requires a local data file. The free 'Lite' data file can be acquired by
pulling the git submodules under this repository (run `git submodule update --recursive`)
or from the device-detection-data
GitHub repository.
The Lite data file is only used for illustration, and has limited accuracy and capabilities.
Find out about the more capable data files that are available on our
pricing page
36 from fiftyone_devicedetection.devicedetection_pipelinebuilder
import DeviceDetectionPipelineBuilder
37 from fiftyone_devicedetection_examples.example_utils
import ExampleUtils
38 from fiftyone_pipeline_core.logger
import Logger
48 LITE_V_4_1_HASH =
"51Degrees-LiteV4.1.hash" 50 class GettingStartedConsole():
51 def run(self, data_file, logger, output):
57 pipeline = DeviceDetectionPipelineBuilder(
58 data_file_path = data_file,
65 performance_profile =
"LowMemory",
68 usage_sharing =
False,
71 licence_keys =
"").add_logger(logger).build()
73 ExampleUtils.check_data_file(pipeline, logger);
76 for values
in self.EvidenceValues:
77 self.analyseEvidence(values, pipeline, output)
79 def analyseEvidence(self, evidence, pipeline, output):
88 data = pipeline.create_flowdata()
93 message.append(
"Input values:\n")
95 message.append(f
"\t{key}: {evidence[key]}\n")
97 output(
"".join(message))
100 data.evidence.add_from_dict(evidence)
106 message.append(
"Results:\n")
118 self.outputValue(
"Mobile Device", device.ismobile, message)
119 self.outputValue(
"Platform Name", device.platformname, message)
120 self.outputValue(
"Platform Version", device.platformversion, message)
121 self.outputValue(
"Browser Name", device.browsername, message)
122 self.outputValue(
"Browser Version", device.browserversion, message)
123 output(
"".join(message))
125 def outputValue(self, name, value, message):
134 f
"\t{name}: {value.value()}\n" if value.has_value()
135 else f
"\t{name}: {value.no_value_message()}\n")
142 {
"header.user-agent":
143 "Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) " 144 "AppleWebKit/537.36 (KHTML, like Gecko) " 145 "SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36" },
147 {
"header.user-agent":
148 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " 149 "AppleWebKit/537.36 (KHTML, like Gecko) " 150 "Chrome/78.0.3904.108 Safari/537.36" },
153 {
"header.user-agent":
154 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " 155 "AppleWebKit/537.36 (KHTML, like Gecko) " 156 "Chrome/98.0.4758.102 Safari/537.36",
157 "header.sec-ch-ua-mobile":
"?0",
159 "\" Not A; Brand\";v=\"99\", \"Chromium\";v=\"98\", " 160 "\"Google Chrome\";v=\"98\"",
161 "header.sec-ch-ua-platform":
"\"Windows\"",
162 "header.sec-ch-ua-platform-version":
"\"14.0.0\"" }
168 data_file = argv[0]
if len(argv) > 0
else ExampleUtils.find_file(LITE_V_4_1_HASH)
171 logger = Logger(min_level=
"info")
173 if (data_file !=
None):
174 GettingStartedConsole().run(data_file, logger,
print)
177 "Failed to find a device detection " +
178 "data file. Make sure the device-detection-data " +
179 "submodule has been updated by running " +
180 "`git submodule update --recursive`.")
182 if __name__ ==
"__main__":