51Degrees Device Detection Python  4.1

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.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 
48 resourceKey = "!!YOUR_RESOURCE_KEY!!"
49 
50 if resourceKey == "!!YOUR_RESOURCE_KEY!!":
51  print("""
52  You need to create a resource key at
53  https://configure.51degrees.com and paste it into the code,
54  'replacing !!YOUR_RESOURCE_KEY!!
55  make sure to include the HardwareName, HardwareProfile and HardwareVendor
56  properties used by this example
57  """)
58 else:
59 
60  # Create an instance of the cloud request engine with your resource key
61 
62  requestEngineInstance = CloudRequestEngine({
63  "resourceKey": resourceKey
64  })
65 
66  # Now create an instance of the hardwareprofile cloud engine
67 
68  hardwareProfileEngineInstance = HardwareProfileCloud()
69 
70  # Now create a pipeline and add those two engines
71  # the Cloud Request Engine needs to go first
72 
73  pipeline = PipelineBuilder().add(requestEngineInstance).add(hardwareProfileEngineInstance).build()
74 
75  # we get a native model name to test
76  nativeModelAndroid = 'SC-03L'
77 
78  # We create a FlowData object from the pipeline
79  # this is used to add evidence to and then process
80  flowData = pipeline.create_flowdata()
81 
82  # After creating a flowdata instance, add the native model name as evidence.
83  flowData.evidence.add('query.nativemodel', nativeModelAndroid)
84 
85  # Now we process the FlowData to get results
86 
87  flowData.process()
88 
89  # The result is an array containing the details of any devices that match
90  # the specified native model name.
91  # The code in this example iterates through this array, outputting the
92  # vendor and model of each matching device.
93 
94  print("Which devices are associated with the native model name: " + nativeModelAndroid)
95 
96  for profile in flowData.hardware.profiles:
97  hardwareVendor = profile["hardwarevendor"]
98  hardwareName = profile["hardwarename"]
99  hardwareModel = profile["hardwaremodel"]
100 
101  if (hardwareVendor.has_value and hardwareName.has_value and hardwareModel.has_value):
102  print(hardwareVendor.value() + " " + str(hardwareName.value()) +" "+ hardwareModel.value())
103  else:
104  print(hardwareVendor.no_value_message())
105