51Degrees Device Detection Python  4.1

Device Detection services for 51Degrees Pipeline

cloud/metadata.py

This example shows how to retrieve property meta data from the 51Degrees cloud service. This feature can be used to get information such as the category that a property belongs to or the possible values a property can have. (At the time of the request. If the data file being used by the cloud service is updated, the possible values for a property can change)

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.

Expected output:

1 [List of properties with names and categories]
2 
3 Does user agent Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114 support svg? :
4 true
5 Does user agent Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114 support video? :
6 true
7 Does user agent Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114 support supportstls/ssl? :
8 true
9 Does user agent Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114 support supportswebgl? :
10 true
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 
43 
44 
45 from fiftyone_devicedetection.devicedetection_pipelinebuilder import DeviceDetectionPipelineBuilder
46 
47 # First create the device detection pipeline with the desired settings.
48 
49 # You need to create a resource key at https://configure.51degrees.com
50 # and paste it into the code, replacing !!YOUR_RESOURCE_KEY!! below.
51 
52 resourceKey = "!!YOUR_RESOURCE_KEY!!"
53 
54 if resourceKey == "!!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  To include the properties used by this example go to https://configure.51degrees.com/CfLML6rg
60  """)
61 else:
62 
63  pipeline = DeviceDetectionPipelineBuilder({"resourceKey": resourceKey}).build()
64 
65  # Now we see what properties are available in the pipeline
66 
67  properties = pipeline.get_properties()
68 
69  # Now we find out the details of the properties in the device engine
70 
71  for propertyKey, propertyMeta in properties["device"].items():
72  print(propertyKey + " of category " + propertyMeta["category"])
73 
74  # Now we can take a User-Agent, run it through this pipeline and check
75  # the supported media properties against it
76 
77  # First we create a FlowData object from the pipeline
78  flowData = pipeline.create_flowdata()
79 
80  # Then we add the User-Agent we are interested in as evidence
81  iphoneUA = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114"
82 
83  flowData.evidence.add("header.user-agent", iphoneUA)
84 
85  # Now we process the FlowData using the engines in the Pipeline
86 
87  flowData.process()
88 
89  # To get all properties of a specific category, we can use the "getWhere" function
90 
91  mediaSupport = flowData.get_where("category", "Supported Media")
92 
93  for supportedMediaProperty, supportedValue in mediaSupport.items():
94  print("Does User-Agent " + iphoneUA + " support " + supportedMediaProperty + "?")
95  if supportedValue.has_value():
96  print(supportedValue.value())
97  else:
98  print(supportedValue.no_value_message())