\r\n

51Degrees Device Detection Python  4.2Newer Version 4.3

Device Detection services for 51Degrees Pipeline

hash/offline_processing.py

This example shows how to use 51Degrees device detection to process a file containing many User-Agents.

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 
27 
28 import csv
29 import time
30 import multiprocessing as mp
31 
32 # This example goes through a CSV of 20000 user agents and processes them,
33 # saving whether each one is a mobile, not, or unknown to a csv file
34 
35 from fiftyone_devicedetection_onpremise.devicedetection_onpremise_pipelinebuilder import DeviceDetectionOnPremisePipelineBuilder
36 
37 # First we create the device detection pipeline with the desired settings.
38 
39 data_file = "fiftyone_devicedetection_onpremise/device-detection-cxx/device-detection-data/51Degrees-LiteV4.1.hash"
40 
41 pipeline = DeviceDetectionOnPremisePipelineBuilder(
42  data_file_path=data_file,
43  licence_keys="",
44  performance_profile='MaxPerformance',
45  add_javascript_builder = False,
46  restricted_properties = ["ismobile"],
47  usage_sharing=False,
48  auto_update=False).build()
49 
50 # Here we make a function that processes a user agent
51 # And returns if it is a mobile device
52 
53 def process_user_agent(user_agent):
54 
55  # First we create the flowdata using the global pipeline
56  flowdata = pipeline.create_flowdata()
57 
58  # Here we add the user agent as evidence
59  flowdata.evidence.add("header.user-agent", user_agent)
60 
61  # We process the flowdata to get the results
62  flowdata.process()
63 
64  # To check whether the User-Agent is a mobile device we look at the
65  # ismobile property inside the Device Detection Engine
66 
67  # first we check if this has a meaningful result
68 
69  if flowdata.device.ismobile.has_value():
70  return flowdata.device.ismobile.value()
71  else:
72  return None
73 
74 # First we read the contents of the 2000 user agents file
75 # Converting it to a list
76 
77 with open('fiftyone_devicedetection_onpremise/device-detection-cxx/device-detection-data/20000 User Agents.csv', newline='') as file:
78  reader = csv.reader(file)
79  user_agents = list(reader)
80 
81 number_of_user_agents = len(user_agents)
82 
83 print("Processing " + str(number_of_user_agents) + " user agents")
84 
85 # Now we write to a csv file the user agent and the result if ismobile
86 
87 output_path = "output.csv"
88 count = 0
89 
90 with open(output_path,'w') as file:
91  for user_agent in user_agents:
92  result = process_user_agent(user_agent[0])
93  file.write(user_agent[0])
94  file.write(",")
95  file.write(str(result))
96  file.write('\n')
97  count += 1
98 
99 print("Processed " + str(count) + " user agents")