|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Sample code for the WhatIsMyBrowser.com API - Version 2 |
| 4 | +# |
| 5 | +# User Agent Database Search |
| 6 | +# This sample code shows you how to search the database for useragents |
| 7 | +# which match your query. It's not for decoding/parsing user agents, |
| 8 | +# you should use the User Agent Parse API Endpoint instead. |
| 9 | +# |
| 10 | +# It should be used as an example only, to help you get started |
| 11 | +# using the API. This code is in the public domain, feel free to |
| 12 | +# take it an integrate it with your system as you require. |
| 13 | +# Refer to the "LICENSE" file in this repository for legal information. |
| 14 | +# |
| 15 | +# For further documentation, please refer to the Integration Guide: |
| 16 | +# https://developers.whatismybrowser.com/api/docs/v2/integration-guide/ |
| 17 | +# |
| 18 | +# For support, please refer to our Support section: |
| 19 | +# https://developers.whatismybrowser.com/api/support/ |
| 20 | + |
| 21 | +import requests |
| 22 | +import json |
| 23 | + |
| 24 | +# Your API Key |
| 25 | +# You can get your API Key by following these instructions: |
| 26 | +# https://developers.whatismybrowser.com/api/docs/v2/integration-guide/#introduction-api-key |
| 27 | +api_key = "" |
| 28 | + |
| 29 | +# The various search parameters |
| 30 | +# This is a basic search for Safari user agents... but it includes |
| 31 | +# other sample parameters which have been commented out. Change the |
| 32 | +# parameters which get sent to fetch the results you need. |
| 33 | +# |
| 34 | +# You can also use the Web Based form to experiment and see which |
| 35 | +# parameter values are valid: |
| 36 | +# https://developers.whatismybrowser.com/api/docs/v2/sample-code/database-search |
| 37 | + |
| 38 | +search_params = { |
| 39 | + "software_name": "Safari", # "Internet Explorer" "Chrome" "Firefox" |
| 40 | + #"software_version": 71, |
| 41 | + #"software_version_min": 64, |
| 42 | + #"software_version_max": 79, |
| 43 | + |
| 44 | + #"operating_system_name": "macOS", # "OS X", "Windows", "Linux", "Android" etc |
| 45 | + #"operating_system_version": "Snow Leopard", # Vista, 8.2 |
| 46 | + |
| 47 | + #"operating_platform": "iPhone", #"iPad", "iPhone 5", "Galaxy Gio", "Galaxy Note", "Galaxy S4" |
| 48 | + #"operating_platform_code": "GT-S5660", |
| 49 | + |
| 50 | + #"software_type": "browser", # "bot" "application" |
| 51 | + #"software_type_specific": "web-browser", # "in-app-browser", "analyser" "application" "bot" "crawler" etc |
| 52 | + |
| 53 | + #"hardware_type": "computer", # "computer" "mobile" "server" |
| 54 | + #"hardware_type_specific": "computer", # "phone", "tablet", "mobile", "ebook-reader", "game-console" etc |
| 55 | + |
| 56 | + #"layout_engine_name": "NetFront", # Blink, Trident, EdgeHTML, Gecko, NetFront, Presto |
| 57 | + |
| 58 | + #"order_by": "times_seen desc", # "times_seen asc" "first_seen_at asc" "first_seen_at desc" "last_seen_at desc" "last_seen_at asc" "software_version desc" |
| 59 | + #"times_seen_min": 100, |
| 60 | + #"times_seen_max": 1000, |
| 61 | + #"limit": 250, |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +# Where will the request be sent to |
| 66 | +api_url = "https://api.whatismybrowser.com/api/v2/user_agent_database_search" |
| 67 | + |
| 68 | +# -- Set up HTTP Headers |
| 69 | +headers = { |
| 70 | + 'X-API-KEY': api_key, |
| 71 | +} |
| 72 | + |
| 73 | +# -- Make the request |
| 74 | +result = requests.get(api_url, params=search_params, headers=headers) |
| 75 | + |
| 76 | +# -- Try to decode the api response as json |
| 77 | +result_json = {} |
| 78 | +try: |
| 79 | + result_json = result.json() |
| 80 | +except Exception as e: |
| 81 | + print(result.text) |
| 82 | + print("Couldn't decode the response as JSON:", e) |
| 83 | + exit() |
| 84 | + |
| 85 | +# -- Check that the server responded with a "200/Success" code |
| 86 | +if result.status_code != 200: |
| 87 | + print("ERROR: not a 200 result. instead got: %s." % result.status_code) |
| 88 | + print(json.dumps(result_json, indent=2)) |
| 89 | + exit() |
| 90 | + |
| 91 | +# -- Check the API request was successful |
| 92 | +if result_json.get('result', {}).get('code') != "success": |
| 93 | + print("The API did not return a 'success' response. It said: result code: %s, message_code: %s, message: %s" % (result_json.get('result', {}).get('code'), result_json.get('result', {}).get('message_code'), result_json.get('result', {}).get('message'))) |
| 94 | + #print(json.dumps(result_json, indent=2)) |
| 95 | + exit() |
| 96 | + |
| 97 | +# Now you have "result_json" and can store, display or process any part of the response. |
| 98 | + |
| 99 | +# -- Print the entire json dump for reference |
| 100 | +print(json.dumps(result_json, indent=2)) |
| 101 | + |
| 102 | +# -- Display the user agent and times seen for each parse result in the list |
| 103 | +# Don't forget that all the parse data is included in each user agent record as well. |
| 104 | +for user_agent_record in result_json.get("search_results").get("user_agents"): |
| 105 | + print("%s - seen: %s times" % (user_agent_record.get("user_agent"), user_agent_record.get("user_agent_meta_data").get("times_seen"))) |
0 commit comments