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
38 from fiftyone_devicedetection.devicedetection_pipelinebuilder
import DeviceDetectionPipelineBuilder
39 from fiftyone_devicedetection_examples.example_utils
import ExampleUtils
49 LITE_V_4_1_HASH =
"51Degrees-LiteV4.1.hash";
51 class GettingStartedConsole():
52 def run(self, data_file, logger):
58 pipeline = DeviceDetectionPipelineBuilder(
59 data_file_path = data_file,
66 performance_profile =
"LowMemory",
70 licence_keys =
"").build()
72 ExampleUtils.check_data_file(pipeline, logger);
75 for values
in self.EvidenceValues:
76 self.analyseEvidence(values, pipeline, logger)
78 def analyseEvidence(self, evidence, pipeline, logger):
87 data = pipeline.create_flowdata()
92 message.append(
"Input values:\n")
94 message.append(f
"\t{key}: {evidence[key]}\n")
96 logger.info(
"".join(message))
99 data.evidence.add_from_dict(evidence)
105 message.append(
"Results:\n")
117 self.outputValue(
"Mobile Device", device.ismobile, message);
118 self.outputValue(
"Platform Name", device.platformname, message);
119 self.outputValue(
"Platform Version", device.platformversion, message);
120 self.outputValue(
"Browser Name", device.browsername, message);
121 self.outputValue(
"Browser Version", device.browserversion, message);
122 logger.info(
"".join(message));
124 def outputValue(self, name, value, message):
133 f
"\t{name}: {value.value()}\n" if value.has_value()
134 else f
"\t{name}: {value.no_value_message()}\n")
141 {
"header.user-agent":
142 "Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) " 143 "AppleWebKit/537.36 (KHTML, like Gecko) " 144 "SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36" },
146 {
"header.user-agent":
147 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " 148 "AppleWebKit/537.36 (KHTML, like Gecko) " 149 "Chrome/78.0.3904.108 Safari/537.36" },
152 {
"header.user-agent":
153 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " 154 "AppleWebKit/537.36 (KHTML, like Gecko) " 155 "Chrome/98.0.4758.102 Safari/537.36",
156 "header.sec-ch-ua-mobile":
"?0",
158 "\" Not A; Brand\";v=\"99\", \"Chromium\";v=\"98\", " 159 "\"Google Chrome\";v=\"98\"",
160 "header.sec-ch-ua-platform":
"\"Windows\"",
161 "header.sec-ch-ua-platform-version":
"\"14.0.0\"" }
167 data_file = argv[0]
if len(argv) > 0
else ExampleUtils.find_file(LITE_V_4_1_HASH)
170 logger = logging.getLogger(
"Getting Started")
171 logger.setLevel(logging.INFO)
173 if (data_file !=
None):
174 GettingStartedConsole().run(data_file, logger)
176 logger.error(
"Failed to find a device detection " +
177 "data file. Make sure the device-detection-data " +
178 "submodule has been updated by running " +
179 "`git submodule update --recursive`.")
181 if __name__ ==
"__main__":