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

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