\r\n

51Degrees Device Detection Python  4.3

Device Detection services for 51Degrees Pipeline

cloud/nativemodellookup.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. For more details on this, see the example below.

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.

Example output:

1 This example finds the details of devices from the 'native model name'.
2 The native model name can be retrieved by code running on the device (For example, a mobile app).
3 For Android devices, see https://developer.android.com/reference/android/os/Build#MODEL
4 For iOS devices, see https://gist.github.com/soapyigu/c99e1f45553070726f14c1bb0a54053b#file-machinename-swift
5 ----------------------------------------
6 Which devices are associated with the native model name 'SC-03L'?
7  Samsung Galaxy S10 (SC-03L)
1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4 # Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
5 #
6 # This Original Work is licensed under the European Union Public Licence (EUPL)
7 # 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 from fiftyone_pipeline_core.pipelinebuilder import PipelineBuilder
42 from fiftyone_pipeline_cloudrequestengine.cloudrequestengine import CloudRequestEngine
43 from fiftyone_devicedetection_cloud.hardwareprofile_cloud import HardwareProfileCloud
44 
45 # You need to create a resource key at https://configure.51degrees.com
46 # and paste it into the code, replacing !!YOUR_RESOURCE_KEY!! below.
47 # Alternatively, add a resource_key environment variable
48 import os
49 if "resource_key" in os.environ:
50  resource_key = os.environ["resource_key"]
51 else:
52  resource_key = "!!YOUR_RESOURCE_KEY!!"
53 
54 if resource_key == "!!YOUR_RESOURCE_KEY!!":
55  print("""
56  You need to create a resource key at
57  https://configure.51degrees.com and paste it into the code,
58  'replacing !!YOUR_RESOURCE_KEY!!
59  make sure to include the HardwareName, HardwareProfile, HardwareVendor and Profiles
60  properties used by this example
61  """)
62 else:
63 
64  # Create an instance of the cloud request engine with your resource key
65 
66  request_engine_instance = CloudRequestEngine({
67  "resource_key": resource_key
68  })
69 
70  # Now create an instance of the hardwareprofile cloud engine
71 
72  hardware_profile_engine_instance = HardwareProfileCloud()
73 
74  # Now create a pipeline and add those two engines
75  # the Cloud Request Engine needs to go first
76 
77  pipeline = PipelineBuilder().add(request_engine_instance).add(hardware_profile_engine_instance).build()
78 
79  # we get a native model name to test
80  native_model_android = 'SC-03L'
81 
82  # We create a FlowData object from the pipeline
83  # this is used to add evidence to and then process
84  flowdata = pipeline.create_flowdata()
85 
86  # After creating a flowdata instance, add the native model name as evidence.
87  flowdata.evidence.add('query.nativemodel', native_model_android)
88 
89  # Now we process the FlowData to get results
90 
91  flowdata.process()
92 
93  # The result is an array containing the details of any devices that match
94  # the specified native model name.
95  # The code in this example iterates through this array, outputting the
96  # vendor and model of each matching device.
97 
98  print("Which devices are associated with the native model name: " + native_model_android)
99 
100  for profile in flowdata.hardware.profiles:
101  hardwarevendor = profile["hardwarevendor"]
102  hardwarename = profile["hardwarename"]
103  hardwaremodel = profile["hardwaremodel"]
104 
105  if (hardwarevendor.has_value and hardwarename.has_value and hardwaremodel.has_value):
106  print(hardwarevendor.value() + " " + str(hardwarename.value()) +" "+ hardwaremodel.value())
107  else:
108  print(hardwarevendor.no_value_message())
109