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

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