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.
37 from pathlib
import Path
40 from fiftyone_pipeline_core.logger
import Logger
41 from fiftyone_pipeline_core.pipelinebuilder
import PipelineBuilder
43 from ..example_utils
import ExampleUtils
45 class GettingStartedConsole():
46 def run(self, config, logger, output):
53 pipeline = PipelineBuilder().add_logger(logger).build_from_configuration(config)
56 for values
in self.EvidenceValues:
57 self.analyseEvidence(values, pipeline, output)
59 def analyseEvidence(self, evidence, pipeline, output):
68 data = pipeline.create_flowdata()
73 message.append(
"Input values:\n")
75 message.append(f
"\t{key}: {evidence[key]}\n")
77 output(
"".join(message))
80 data.evidence.add_from_dict(evidence)
86 message.append(
"Results:\n")
98 self.outputValue(
"Mobile Device", device.ismobile, message)
99 self.outputValue(
"Platform Name", device.platformname, message)
100 self.outputValue(
"Platform Version", device.platformversion, message)
101 self.outputValue(
"Browser Name", device.browsername, message)
102 self.outputValue(
"Browser Version", device.browserversion, message)
103 output(
"".join(message))
105 def outputValue(self, name, value, message):
114 f
"\t{name}: {value.value()}\n" if value.has_value()
115 else f
"\t{name}: {value.no_value_message()}\n")
122 {
"header.user-agent":
123 "Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) " 124 "AppleWebKit/537.36 (KHTML, like Gecko) " 125 "SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36" },
127 {
"header.user-agent":
128 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " 129 "AppleWebKit/537.36 (KHTML, like Gecko) " 130 "Chrome/78.0.3904.108 Safari/537.36" },
133 {
"header.user-agent":
134 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " 135 "AppleWebKit/537.36 (KHTML, like Gecko) " 136 "Chrome/98.0.4758.102 Safari/537.36",
137 "header.sec-ch-ua-mobile":
"?0",
139 "\" Not A; Brand\";v=\"99\", \"Chromium\";v=\"98\", " 140 "\"Google Chrome\";v=\"98\"",
141 "header.sec-ch-ua-platform":
"\"Windows\"",
142 "header.sec-ch-ua-platform-version":
"\"14.0.0\"" }
148 resource_key = argv[0]
if len(argv) > 0
else ExampleUtils.get_resource_key()
151 logger = Logger(min_level=
"info")
154 configFile = Path(__file__).resolve().parent.joinpath(
"gettingstarted_console.json").read_text()
155 config = json5.loads(configFile)
158 resourceKeyFromConfig = ExampleUtils.get_resource_key_from_config(config)
159 configHasKey = resourceKeyFromConfig
and resourceKeyFromConfig.startswith(
"!!") ==
False 163 if configHasKey ==
False:
164 ExampleUtils.set_resource_key_in_config(config, resource_key)
167 if not ExampleUtils.get_resource_key_from_config(config):
169 "No resource key specified in the configuration file " +
170 "'gettingstarted_console.json' or the environment variable " +
171 f
"'{ExampleUtils.RESOURCE_KEY_ENV_VAR}'. The 51Degrees cloud " +
172 "service is accessed using a 'ResourceKey'. For more information " +
174 "https://51degrees.com/documentation/_info__resource_keys.html. " +
175 "A resource key with the properties required by this example can be " +
176 "created for free at https://configure.51degrees.com/1QWJwHxl. " +
177 "Once complete, populate the config file or environment variable " +
178 "mentioned at the start of this message with the key.")
180 GettingStartedConsole().
run(config, logger,
print)
182 if __name__ ==
"__main__":