\r\n

51Degrees Device Detection Python  4.4

Device Detection services for 51Degrees Pipeline

cloud/nativemodellookup_console.py

This example shows how to use the 51Degrees Cloud service to lookup the details of a device based on a given 'native model name'. Native model name is a string of characters that are returned from a query to the device's OS. There are different mechanisms to get native model names for Android devices and iOS devices

This example is available in full on GitHub.

To run this example, you will need to create a resource key. The resource key is used as shorthand to store the particular set of properties you are interested in as well as any associated license keys that entitle you to increased request limits and/or paid-for properties.

You can create a resource key using the 51Degrees Configurator.

Required PyPi Dependencies:

1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2023 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 
39 
40 import sys
41 # pylint: disable=E0402
42 from ..example_utils import ExampleUtils
43 from fiftyone_pipeline_core.logger import Logger
44 from fiftyone_pipeline_core.pipelinebuilder import PipelineBuilder
45 from fiftyone_pipeline_cloudrequestengine.cloudrequestengine import CloudRequestEngine
47 from fiftyone_devicedetection_cloud.hardwareprofile_cloud import HardwareProfileCloud
48 
49 class NativeModelLookupConsole():
50  def run(self, resource_key, logger, output, cloudEndPoint = ""):
51 
52  output("This example shows the details of devices " +
53  "associated with a given 'native model name'.")
54  output("The native model name can be retrieved by " +
55  "code running on the device (For example, a mobile app).")
56  output("For Android devices, see " +
57  "https://developer.android.com/reference/android/os/Build#MODEL")
58  output("For iOS devices, see " +
59  "https://gist.github.com/soapyigu/c99e1f45553070726f14c1bb0a54053b#file-machinename-swift")
60  output("----------------------------------------")
61 
62  # This example creates the pipeline and engines in code. For a demonstration
63  # of how to do this using a configuration file instead, see the TacLookup example.
64  # For more information about builders in general see the documentation at
65  # https://51degrees.com/documentation/_concepts__configuration__builders__index.html
66  cloudRequestEngineSettings = { "resource_key": resource_key }
67 
68  # If a cloud endpoint has been provided then set the
69  # cloud pipeline endpoint.
70  if cloudEndPoint:
71  cloudRequestEngineSettings["cloud_endpoint"] = cloudEndPoint
72 
73  # Create the cloud request engine.
74  cloudEngine = CloudRequestEngine(cloudRequestEngineSettings)
75  # Create the hardware profile engine to process the response from the
76  # request engine.
77  propertyKeyedEngine = HardwareProfileCloud()
78  # Create the pipeline using the engines.
79  pipeline = PipelineBuilder().add_logger(logger).add(cloudEngine).add(propertyKeyedEngine).build()
80 
81  # Pass a native model into the pipeline and list the matching devices.
82  self.analyseModel(self._nativeModel1, pipeline, output)
83  # Repeat for an alternative native model name.
84  self.analyseModel(self._nativeModel2, pipeline, output)
85 
86  def analyseModel(self, nativemodel, pipeline, output):
87  # Create the FlowData instance.
88  data = pipeline.create_flowdata()
89  # Add the native model key as evidence.
90  data.evidence.add(Constants.EVIDENCE_QUERY_NATIVE_MODEL_KEY, nativemodel)
91  # Process the supplied evidence.
92  data.process()
93  # Get result data from the flow data.
94  result = data.hardware
95  output("Which devices are associated with the " +
96  f"native model name '{nativemodel}'?")
97  # The 'hardware.profiles' object contains one or more devices.
98  # This is the same interface used for standard device detection, so we have
99  # access to all the same properties.
100  for device in result.profiles:
101  vendor = ExampleUtils.get_human_readable(device, "hardwarevendor")
102  name = ExampleUtils.get_human_readable(device, "hardwarename")
103  model = ExampleUtils.get_human_readable(device, "hardwaremodel")
104  output(f"\t{vendor} {name} ({model})")
105 
106  # Example values to use when looking up device details from native model names.
107  _nativeModel1 = "SC-03L"
108  _nativeModel2 = "iPhone11,8"
109 
110 def main(argv):
111  # Use the command line args to get the resource key if present.
112  # Otherwise, get it from the environment variable.
113  resource_key = argv[0] if len(argv) > 0 else ExampleUtils.get_resource_key()
114 
115  # Configure a logger to output to the console.
116  logger = Logger()
117 
118  if (resource_key):
119  NativeModelLookupConsole().run(resource_key, logger, print)
120  else:
121  logger.log("error",
122  "No resource key specified on the command line or in the " +
123  f"environment variable '{ExampleUtils.RESOURCE_KEY_ENV_VAR}'. " +
124  "The 51Degrees cloud service is accessed using a 'ResourceKey'. " +
125  "For more information " +
126  "see https://51degrees.com/documentation/_info__resource_keys.html. " +
127  "Native model lookup is not available as a free service. This means that " +
128  "you will first need a license key, which can be purchased from our " +
129  "pricing page: https://51degrees.com/pricing. Once this is done, a resource " +
130  "key with the properties required by this example can be created at " +
131  "https://configure.51degrees.com/QKyYH5XT. You can now populate the " +
132  "environment variable mentioned at the start of this message with the " +
133  "resource key or pass it as the first argument on the command line.")
134 
135 if __name__ == "__main__":
136  main(sys.argv[1:])