\r\n

51Degrees Device Detection Python  4.5

Device Detection services for 51Degrees Pipeline

onpremise/gettingstarted_console.py

Getting Started Console Example - On-premise 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.

You will learn:

  1. How to create a Pipeline that uses 51Degrees On-premise device detection
  2. How to pass input data (evidence) to the Pipeline
  3. How to retrieve the results

This example is available in full on GitHub.

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

Required PyPi Dependencies:

1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2025 51 Degrees Mobile Experts Limited, Davidson House,
4 # Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5 #
6 # This Original Work is licensed under the European Union Public Licence
7 # (EUPL) v.1.2 and is subject to its terms as set out below.
8 #
9 # If a copy of the EUPL was not distributed with this file, You can obtain
10 # one at https://opensource.org/licenses/EUPL-1.2.
11 #
12 # The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 # amended by the European Commission) shall be deemed incompatible for
14 # the purposes of the Work and the provisions of the compatibility
15 # clause in Article 5 of the EUPL shall not apply.
16 #
17 # If using the Work as, or as part of, a network application, by
18 # including the attribution notice(s) required under Article 5 of the EUPL
19 # in the end user terms of the application under an appropriate heading,
20 # such notice(s) shall fulfill the requirements of that article.
21 # *********************************************************************
22 
23 
40 
41 import sys
42 from fiftyone_devicedetection.devicedetection_pipelinebuilder import DeviceDetectionPipelineBuilder
44 from fiftyone_pipeline_core.logger import Logger
45 from fiftyone_devicedetection_shared.example_constants import EVIDENCE_VALUES
46 from fiftyone_devicedetection_shared.example_constants import LITE_DATAFILE_NAME
47 
48 class GettingStartedConsole():
49  def run(self, data_file, logger, output):
50 
51  # In this example, we use the DeviceDetectionPipelineBuilder
52  # and configure it in code. For more information about
53  # pipelines in general see the documentation at
54  # https://51degrees.com/documentation/4.3/_concepts__configuration__builders__index.html
55  pipeline = DeviceDetectionPipelineBuilder(
56  data_file_path = data_file,
57  # We use the low memory profile as its performance is
58  # sufficient for this example. See the documentation for
59  # more detail on this and other configuration options:
60  # https://51degrees.com/documentation/4.3/_device_detection__features__performance_options.html
61  # https://51degrees.com/documentation/4.3/_features__automatic_datafile_updates.html
62  # https://51degrees.com/documentation/4.3/_features__usage_sharing.html
63  performance_profile = "MaxPerformance",
64  # inhibit sharing usage for this test, usually this
65  # should be set "true"
66  usage_sharing = False,
67  # Inhibit auto-update of the data file for this example
68  auto_update = False,
69  licence_keys = "").add_logger(logger).build()
70 
71  ExampleUtils.check_data_file(pipeline, logger)
72 
73  # carry out some sample detections
74  for values in EVIDENCE_VALUES:
75  self.analyseEvidence(values, pipeline, output)
76 
77  def analyseEvidence(self, evidence, pipeline, output):
78 
79  # FlowData is a data structure that is used to convey
80  # information required for detection and the results of the
81  # detection through the pipeline.
82  # Information required for detection is called "evidence"
83  # and usually consists of a number of HTTP Header field
84  # values, in this case represented by a dictionary of header
85  # name/value entries.
86  data = pipeline.create_flowdata()
87 
88  message = []
89 
90  # List the evidence
91  message.append("Input values:\n")
92  for key in evidence:
93  message.append(f"\t{key}: {evidence[key]}\n")
94 
95  output("".join(message))
96 
97  # Add the evidence values to the flow data
98  data.evidence.add_from_dict(evidence)
99 
100  # Process the flow data.
101  data.process()
102 
103  message = []
104  message.append("Results:\n")
105 
106  # Now that it's been processed, the flow data will have
107  # been populated with the result. In this case, we want
108  # information about the device, which we can get by
109  # asking for a result matching named "device"
110  device = data.device
111 
112  # Display the results of the detection, which are called
113  # device properties. See the property dictionary at
114  # https://51degrees.com/developers/property-dictionary
115  # for details of all available properties.
116  self.outputValue("Mobile Device", device.ismobile, message)
117  self.outputValue("Platform Name", device.platformname, message)
118  self.outputValue("Platform Version", device.platformversion, message)
119  self.outputValue("Browser Name", device.browsername, message)
120  self.outputValue("Browser Version", device.browserversion, message)
121  output("".join(message))
122 
123  def outputValue(self, name, value, message):
124  # Individual result values have a wrapper called
125  # `AspectPropertyValue`. This functions similarly to
126  # a null-able type.
127  # If the value has not been set then trying to access the
128  # `value` method will throw an exception.
129  # `AspectPropertyValue` also includes the `no_value_message`
130  # method, which describes why the value has not been set.
131  message.append(
132  f"\t{name}: {value.value()}\n" if value.has_value()
133  else f"\t{name}: {value.no_value_message()}\n")
134 
135 def main(argv):
136 
137  # In this example, by default, the 51degrees "Lite" file needs to be
138  # somewhere in the project space, or you may specify another file as
139  # a command line parameter.
140  #
141  # Note that the Lite data file is only used for illustration, and has
142  # limited accuracy and capabilities.
143  # Find out about the Enterprise data file on our pricing page:
144  # https://51degrees.com/pricing
145  data_file = argv[0] if len(argv) > 0 else ExampleUtils.find_file(LITE_DATAFILE_NAME)
146 
147  # Configure a logger to output to the console.
148  logger = Logger(min_level="info")
149 
150  if (data_file != None):
151  GettingStartedConsole().run(data_file, logger, print)
152  else:
153  logger.log("error",
154  "Failed to find a device detection " +
155  "data file. Make sure the device-detection-data " +
156  "submodule has been updated by running " +
157  "`git submodule update --recursive`.")
158 
159 if __name__ == "__main__":
160  main(sys.argv[1:])