This example shows how to use the 51Degrees Cloud service to determine details about a
device based on its User-Agent and User-Agent Client Hint HTTP header values.
36 from pathlib
import Path
38 from fiftyone_devicedetection.devicedetection_pipelinebuilder
import DeviceDetectionPipelineBuilder
39 from fiftyone_pipeline_core.logger
import Logger
40 from fiftyone_pipeline_core.pipelinebuilder
import PipelineBuilder
41 from ..example_utils
import ExampleUtils
43 class GettingStartedConsole():
44 def run(self, config, logger, output):
51 pipeline = PipelineBuilder().add_logger(logger).build_from_configuration(config)
54 for values
in self.EvidenceValues:
55 self.analyseEvidence(values, pipeline, output)
57 def analyseEvidence(self, evidence, pipeline, output):
66 data = pipeline.create_flowdata()
71 message.append(
"Input values:\n")
73 message.append(f
"\t{key}: {evidence[key]}\n")
75 output(
"".join(message))
78 data.evidence.add_from_dict(evidence)
84 message.append(
"Results:\n")
96 self.outputValue(
"Mobile Device", device.ismobile, message)
97 self.outputValue(
"Platform Name", device.platformname, message)
98 self.outputValue(
"Platform Version", device.platformversion, message)
99 self.outputValue(
"Browser Name", device.browsername, message)
100 self.outputValue(
"Browser Version", device.browserversion, message)
101 output(
"".join(message))
103 def outputValue(self, name, value, message):
112 f
"\t{name}: {value.value()}\n" if value.has_value()
113 else f
"\t{name}: {value.no_value_message()}\n")
120 {
"header.user-agent":
121 "Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) " 122 "AppleWebKit/537.36 (KHTML, like Gecko) " 123 "SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36" },
125 {
"header.user-agent":
126 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " 127 "AppleWebKit/537.36 (KHTML, like Gecko) " 128 "Chrome/78.0.3904.108 Safari/537.36" },
131 {
"header.user-agent":
132 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " 133 "AppleWebKit/537.36 (KHTML, like Gecko) " 134 "Chrome/98.0.4758.102 Safari/537.36",
135 "header.sec-ch-ua-mobile":
"?0",
137 "\" Not A; Brand\";v=\"99\", \"Chromium\";v=\"98\", " 138 "\"Google Chrome\";v=\"98\"",
139 "header.sec-ch-ua-platform":
"\"Windows\"",
140 "header.sec-ch-ua-platform-version":
"\"14.0.0\"" }
146 resource_key = argv[0]
if len(argv) > 0
else ExampleUtils.get_resource_key()
149 logger = Logger(min_level=
"info")
152 configFile = Path(__file__).resolve().parent.joinpath(
"gettingstarted_console.json").read_text()
153 config = json5.loads(configFile)
156 resourceKeyFromConfig = ExampleUtils.get_resource_key_from_config(config)
157 configHasKey = resourceKeyFromConfig
and resourceKeyFromConfig.startswith(
"!!") ==
False 161 if configHasKey ==
False:
162 ExampleUtils.set_resource_key_in_config(config, resource_key)
165 if not ExampleUtils.get_resource_key_from_config(config):
167 "No resource key specified in the configuration file " +
168 "'gettingstarted_console.json' or the environment variable " +
169 f
"'{ExampleUtils.RESOURCE_KEY_ENV_VAR}'. The 51Degrees cloud " +
170 "service is accessed using a 'ResourceKey'. For more information " +
172 "http://51degrees.com/documentation/_info__resource_keys.html. " +
173 "A resource key with the properties required by this example can be " +
174 "created for free at https://configure.51degrees.com/1QWJwHxl. " +
175 "Once complete, populate the config file or environment variable " +
176 "mentioned at the start of this message with the key.")
178 GettingStartedConsole().run(config, logger,
print)
180 if __name__ ==
"__main__":