Getting Started Console Example - On-premise.
Getting Started Console Example - On-premiseThis 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 downloaded with the
scripts in the data GitHub repository, which
this repository includes at cxx/data. Fetch the submodules by running
git submodule update --init --recursive, then run
sh cxx/data/get-data-files.sh 51Degrees-LiteV4.1.hash from the repository root,
which saves the file as cxx/data/device-detection/51Degrees-LiteV4.1.hash.
The Lite data file is only used for illustration, and has limited accuracy and capabilities.
The Enterprise data file has every property.
Contact us about a copy.
import sys
CONTACT_US = "/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-onpremise-gettingstarted_console.py&utm_term=enterprise-data-file"
ENTERPRISE_PROPERTIES = [
("Hardware Vendor", "hardwarevendor"),
("Hardware Name", "hardwarename"),
("Device Type", "devicetype")]
class GettingStartedConsole():
def run(self, data_file, logger, output):
pipeline = DeviceDetectionPipelineBuilder(
data_file_path = data_file,
performance_profile = "MaxPerformance",
usage_sharing = False,
auto_update = False,
licence_keys =
"").add_logger(logger).
build()
with pipeline:
ExampleUtils.check_data_file(pipeline, logger)
needs_enterprise = False
for values in EVIDENCE_VALUES:
needs_enterprise |= self.analyseEvidence(
values, pipeline, output)
if needs_enterprise:
output("\n" + ExampleUtils.get_enterprise_note(CONTACT_US))
def analyseEvidence(self, evidence, pipeline, output):
with pipeline.create_flowdata() as data:
message = []
message.append("Input values:\n")
for key in evidence:
message.append(f"\t{key}: {evidence[key]}\n")
output("".join(message))
data.evidence.add_from_dict(evidence)
data.process()
message = []
message.append("Results:\n")
device = data.device
self.outputValue("Mobile Device", device.ismobile, message)
self.outputValue("Platform Name", device.platformname, message)
self.outputValue("Platform Version", device.platformversion, message)
self.outputValue("Browser Name", device.browsername, message)
self.outputValue("Browser Version", device.browserversion, message)
engine = pipeline.get_element("device")
needs_enterprise = False
for name, property in ENTERPRISE_PROPERTIES:
value = ExampleUtils.get_enterprise_value(
engine, device, property)
needs_enterprise |= value == ExampleUtils.NEEDS_ENTERPRISE
message.append(f"\t{name}: {value}\n")
output("".join(message))
return needs_enterprise
def outputValue(self, name, value, message):
message.append(
f"\t{name}: {value.value()}\n" if value.has_value()
else f"\t{name}: {value.no_value_message()}\n")
def main(argv):
data_file = argv[0] if len(argv) > 0 else ExampleUtils.find_file(LITE_DATAFILE_NAME)
logger = Logger(min_level="info")
if (data_file != None):
GettingStartedConsole().run(data_file, logger, print)
else:
logger.log("error",
"Failed to find a device detection " +
"data file. Make sure the data submodule is there, " +
"by running `git submodule update --init --recursive`, " +
"and run this example again to download the file.")
if __name__ == "__main__":
main(sys.argv[1:])
Definition onpremise/build/__init__.py:1
Definition devicedetection_pipelinebuilder.py:1
Definition example_utils.py:1
Definition example_constants.py:1