\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:

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