\r\n

51Degrees Device Detection Python  4.4

Device Detection services for 51Degrees Pipeline

onpremise/gettingstarted_console.py

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 
24 
35 
36 import sys
37 from fiftyone_devicedetection.devicedetection_pipelinebuilder import DeviceDetectionPipelineBuilder
39 from fiftyone_pipeline_core.logger import Logger
40 from fiftyone_devicedetection_shared.example_constants import EVIDENCE_VALUES
41 from fiftyone_devicedetection_shared.example_constants import LITE_DATAFILE_NAME
42 
43 class GettingStartedConsole():
44  def run(self, data_file, logger, output):
45 
46  # In this example, we use the DeviceDetectionPipelineBuilder
47  # and configure it in code. For more information about
48  # pipelines in general see the documentation at
49  # https://51degrees.com/documentation/4.3/_concepts__configuration__builders__index.html
50  pipeline = DeviceDetectionPipelineBuilder(
51  data_file_path = data_file,
52  # We use the low memory profile as its performance is
53  # sufficient for this example. See the documentation for
54  # more detail on this and other configuration options:
55  # https://51degrees.com/documentation/4.3/_device_detection__features__performance_options.html
56  # https://51degrees.com/documentation/4.3/_features__automatic_datafile_updates.html
57  # https://51degrees.com/documentation/4.3/_features__usage_sharing.html
58  performance_profile = "LowMemory",
59  # inhibit sharing usage for this test, usually this
60  # should be set "true"
61  usage_sharing = False,
62  # Inhibit auto-update of the data file for this example
63  auto_update = False,
64  licence_keys = "").add_logger(logger).build()
65 
66  ExampleUtils.check_data_file(pipeline, logger)
67 
68  # carry out some sample detections
69  for values in EVIDENCE_VALUES:
70  self.analyseEvidence(values, pipeline, output)
71 
72  def analyseEvidence(self, evidence, pipeline, output):
73 
74  # FlowData is a data structure that is used to convey
75  # information required for detection and the results of the
76  # detection through the pipeline.
77  # Information required for detection is called "evidence"
78  # and usually consists of a number of HTTP Header field
79  # values, in this case represented by a dictionary of header
80  # name/value entries.
81  data = pipeline.create_flowdata()
82 
83  message = []
84 
85  # List the evidence
86  message.append("Input values:\n")
87  for key in evidence:
88  message.append(f"\t{key}: {evidence[key]}\n")
89 
90  output("".join(message))
91 
92  # Add the evidence values to the flow data
93  data.evidence.add_from_dict(evidence)
94 
95  # Process the flow data.
96  data.process()
97 
98  message = []
99  message.append("Results:\n")
100 
101  # Now that it's been processed, the flow data will have
102  # been populated with the result. In this case, we want
103  # information about the device, which we can get by
104  # asking for a result matching named "device"
105  device = data.device
106 
107  # Display the results of the detection, which are called
108  # device properties. See the property dictionary at
109  # https://51degrees.com/developers/property-dictionary
110  # for details of all available properties.
111  self.outputValue("Mobile Device", device.ismobile, message)
112  self.outputValue("Platform Name", device.platformname, message)
113  self.outputValue("Platform Version", device.platformversion, message)
114  self.outputValue("Browser Name", device.browsername, message)
115  self.outputValue("Browser Version", device.browserversion, message)
116  output("".join(message))
117 
118  def outputValue(self, name, value, message):
119  # Individual result values have a wrapper called
120  # `AspectPropertyValue`. This functions similarly to
121  # a null-able type.
122  # If the value has not been set then trying to access the
123  # `value` method will throw an exception.
124  # `AspectPropertyValue` also includes the `no_value_message`
125  # method, which describes why the value has not been set.
126  message.append(
127  f"\t{name}: {value.value()}\n" if value.has_value()
128  else f"\t{name}: {value.no_value_message()}\n")
129 
130 def main(argv):
131 
132  # In this example, by default, the 51degrees "Lite" file needs to be
133  # somewhere in the project space, or you may specify another file as
134  # a command line parameter.
135  #
136  # Note that the Lite data file is only used for illustration, and has
137  # limited accuracy and capabilities.
138  # Find out about the Enterprise data file on our pricing page:
139  # https://51degrees.com/pricing
140  data_file = argv[0] if len(argv) > 0 else ExampleUtils.find_file(LITE_DATAFILE_NAME)
141 
142  # Configure a logger to output to the console.
143  logger = Logger(min_level="info")
144 
145  if (data_file != None):
146  GettingStartedConsole().run(data_file, logger, print)
147  else:
148  logger.log("error",
149  "Failed to find a device detection " +
150  "data file. Make sure the device-detection-data " +
151  "submodule has been updated by running " +
152  "`git submodule update --recursive`.")
153 
154 if __name__ == "__main__":
155  main(sys.argv[1:])