\r\n

51Degrees Device Detection Python  4.4

Device Detection services for 51Degrees Pipeline

cloud/taclookup_console.py

This example shows how to use the 51Degrees Cloud service to lookup the details of a device based on a given 'TAC'. More background information on TACs can be found through various online sources such as Wikipedia.

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 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 
36 
37 import json5
38 from pathlib import Path
39 import sys
40 from ..example_utils import ExampleUtils
41 from fiftyone_pipeline_core.logger import Logger
42 from fiftyone_pipeline_core.pipelinebuilder import PipelineBuilder
43 from fiftyone_devicedetection_shared.constants import Constants
44 
45 class TacLookupConsole():
46  def run(self, config, logger, output):
47 
48  output("This example shows the details of devices " +
49  "associated with a given 'Type Allocation Code' or 'TAC'.")
50  output("More background information on TACs can be " +
51  "found through various online sources such as Wikipedia: " +
52  "https://en.wikipedia.org/wiki/Type_Allocation_Code")
53  output("----------------------------------------")
54 
55  # In this example, we use the PipelineBuilder and configure it from a file.
56  # For a demonstration of how to do this in code instead, see the
57  # NativeModelLookup example.
58  # For more information about builders in general see the documentation at
59  # http://51degrees.com/documentation/_concepts__configuration__builders__index.html
60 
61  # Create the pipeline using the service provider and the configured options.
62  pipeline = PipelineBuilder().add_logger(logger).build_from_configuration(config)
63 
64  # Pass a TAC into the pipeline and list the matching devices.
65  self.analyseTac(self._tac1, pipeline, output)
66  # Repeat for an alternative TAC.
67  self.analyseTac(self._tac2, pipeline, output)
68 
69  def analyseTac(self, tac, pipeline, output):
70  # Create the FlowData instance.
71  data = pipeline.create_flowdata()
72  # Add the TAC as evidence.
73  data.evidence.add(Constants.EVIDENCE_QUERY_TAC_KEY, tac)
74  # Process the supplied evidence.
75  data.process()
76  # Get result data from the flow data.
77  result = data.hardware
78  output(f"Which devices are associated with the TAC '{tac}'?")
79  # The 'hardware.profiles' object contains one or more devices.
80  # This is the same interface used for standard device detection, so we have
81  # access to all the same properties.
82  for device in result.profiles:
83  vendor = ExampleUtils.get_human_readable(device, "hardwarevendor")
84  name = ExampleUtils.get_human_readable(device, "hardwarename")
85  model = ExampleUtils.get_human_readable(device, "hardwaremodel")
86  output(f"\t{vendor} {name} ({model})")
87 
88  # Example values to use when looking up device details from TACs.
89  _tac1 = "35925406"
90  _tac2 = "86386802"
91 
92 def main(argv):
93  # Use the command line args to get the resource key if present.
94  # Otherwise, get it from the environment variable.
95  resource_key = argv[0] if len(argv) > 0 else ExampleUtils.get_resource_key()
96 
97  # Configure a logger to output to the console.
98  logger = Logger()
99 
100  # Load the configuration file
101  configFile = Path(__file__).resolve().parent.joinpath("taclookup_console.json").read_text()
102  config = json5.loads(configFile)
103 
104  # Get the resource key setting from the config file.
105  resourceKeyFromConfig = ExampleUtils.get_resource_key_from_config(config)
106  configHasKey = resourceKeyFromConfig and resourceKeyFromConfig.startswith("!!") == False
107 
108  # If no resource key is specified in the config file then override it with the key
109  # from the environment variable / command line.
110  if configHasKey == False:
111  ExampleUtils.set_resource_key_in_config(config, resource_key)
112 
113  # If we don't have a resource key then log an error.
114  if not ExampleUtils.get_resource_key_from_config(config):
115  logger.log("error",
116  "No resource key specified on the command line or in " +
117  f"the environment variable '{ExampleUtils.RESOURCE_KEY_ENV_VAR}'. " +
118  "The 51Degrees cloud service is accessed using a 'ResourceKey'. " +
119  "For more information see " +
120  "http://51degrees.com/documentation/_info__resource_keys.html. " +
121  "TAC lookup is not available as a free service. This means " +
122  "that you will first need a license key, which can be purchased " +
123  "from our pricing page: http://51degrees.com/pricing. Once this is " +
124  "done, a resource key with the properties required by this example " +
125  "can be created at https://configure.51degrees.com/QKyYH5XT. You " +
126  "can now populate the environment variable mentioned at the start " +
127  "of this message with the resource key or pass it as the first " +
128  "argument on the command line.")
129  else:
130  TacLookupConsole().run(config, logger, print)
131 
132 if __name__ == "__main__":
133  main(sys.argv[1:])