What's Covered
This tutorial illustrates how to append a CSV file containing User-Agent strings with IsMobile, PlatformName and PlatformVersion properties. The following aspects of the API are covered:
- How to perform a User-Agent match.
- How to reuse resources to perform subsequent matching.
- How to retrieve match results for a specific property.
- How to append a property value to a CSV file.
Code and Explanation
Offline processing example of using 51Degrees device detection. The example
shows how to:
dataFile = settings.V3_WRAPPER_DATABASE
properties = settings.PROPERTIES
cacheSize = settings.CACHE_SIZE
poolSize = settings.POOL_SIZE
provider = fiftyone_degrees_mobile_detector_v3_wrapper.Provider(dataFile, properties, cacheSize, poolSize)
fin = open(inputFile, 'r') fout = open(outputFile, 'w')
fout.write('User-Agent') for name in properties.split(','): fout.write('|' + name) fout.write('\n')
while i < 20 : userAgent = fin.readline().rstrip('\n') fout.write(userAgent) device = provider.getMatch(userAgent) for name in properties.split(','): value = device.getValue(name) fout.write('|' + value) fout.write('\n') i = i + 1This example can be run in any directory, but assumes your settings file contains a valid dataFile location and has the IsMobile property selected.
from FiftyOneDegrees import fiftyone_degrees_mobile_detector_v3_wrapper
from fiftyone_degrees.mobile_detector.conf import settings
import sys
'''
Imports settings from the settings file. The Default settings file, and
details on how to change it can be output by running the command
<p><pre class="prettyprint lang-py">
51degrees-mobile-detector settings
</p></pre>
'''
dataFile = settings.V3_WRAPPER_DATABASE
properties = settings.PROPERTIES
cacheSize = settings.CACHE_SIZE
poolSize = settings.POOL_SIZE
inputFile = '../../data/20000 User Agents.csv'
outputFile = 'offlineProcessingOutput.csv'
'''
Initialises the device detection provider with settings from the settings
file. By default this will use the included Lite data file For more info
see:
<a href="https://51degrees.com/compare-data-options">compare data options
</a>
'''
provider = fiftyone_degrees_mobile_detector_v3_wrapper.Provider(
dataFile,
properties,
cacheSize,
poolSize)
# Carries out match for first 20 User-Agents and prints results to
# output file.
def output_offline_processing():
fin = open(inputFile, 'r')
fout = open(outputFile, 'w')
fout.write('User-Agent')
for name in properties.split(','):
fout.write('|' + name)
fout.write('\n')
i = 0
while i < 20 :
userAgent = fin.readline().rstrip('\n')
fout.write(userAgent)
match = provider.getMatch(userAgent)
for name in properties.split(','):
value = match.getValue(name)
fout.write('|' + value)
fout.write('\n')
i += 1
fin.close()
fout.close()
def main():
sys.stdout.write('Starting Offline Processing\n')
output_offline_processing()
sys.stdout.write('Output Written to %s\n' % outputFile)
if __name__ == '__main__':
main()
Summary
Offline device detection is frequently required for a variety of reasons such as generating reports. The example is based on an actual support request where several properties had to be added to the CSV file before it could be passed on for another department to use.
This tutorial covered how to use the detector offline to append the first 20 lines of a CSV file with Lite properties: IsMobile , PlatformName and PlatformVersion . Using a Premium or an Enterprise data file gives you access to a far greater number of properties including HardwareVendor , PriceBand , ScreenInchesWidth , IsCrawler and more. A full list of properties and the data file version they are present in can be viewed in the Property Dictionary .