Skip to content

Commit fe29624

Browse files
Initial commit of Python 3 sample code
1 parent a61149d commit fe29624

File tree

6 files changed

+552
-0
lines changed

6 files changed

+552
-0
lines changed

code/software_version_numbers.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Sample code for the WhatIsMyBrowser.com API - Version 2
4+
#
5+
# Software Version Numbers
6+
# This sample code provides an example of querying the API
7+
# to get all the latest version numbers for software (ie. Browsers)
8+
# operating systems and plugins.
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+
# Where will the request be sent to
30+
api_url = "https://api.whatismybrowser.com/api/v2/software_version_numbers/all"
31+
32+
# -- Set up HTTP Headers
33+
headers = {
34+
'X-API-KEY': api_key,
35+
}
36+
37+
# -- Make the request
38+
result = requests.get(api_url, headers=headers)
39+
40+
# -- Try to decode the api response as json
41+
result_json = {}
42+
try:
43+
result_json = result.json()
44+
except Exception as e:
45+
print(result.text)
46+
print("Couldn't decode the response as JSON:", e)
47+
exit()
48+
49+
# -- Check that the server responded with a "200/Success" code
50+
if result.status_code != 200:
51+
print("ERROR: not a 200 result. instead got: %s." % result.status_code)
52+
print(json.dumps(result_json, indent=2))
53+
exit()
54+
55+
# -- Check the API request was successful
56+
if result_json.get('result', {}).get('code') != "success":
57+
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')))
58+
#print(json.dumps(result_json, indent=2))
59+
exit()
60+
61+
# Now you have "result_json" and can store, display or process any part of the response.
62+
63+
# -- Print the entire json dump for reference
64+
print(json.dumps(result_json, indent=2))
65+
66+
# -- Copy the `version_data` data to a variable for easier use
67+
version_data = result_json.get('version_data')
68+
69+
# -- Loop over all the different software version data elements
70+
for software_key in version_data:
71+
72+
print("Version data for %s" % software_key)
73+
74+
software_version_data = version_data.get(software_key)
75+
76+
for stream_code_key in software_version_data:
77+
78+
#print(json.dumps(software_version_data.get(stream_code_key), indent=2))
79+
80+
print(" Stream: %s" % stream_code_key)
81+
82+
print("\tThe latest version number for %s [%s] is %s" % (software_key, stream_code_key, ".".join(software_version_data.get(stream_code_key).get("latest_version"))))
83+
84+
if software_version_data.get(stream_code_key).get("update"):
85+
print("\tUpdate no: %s" % software_version_data.get(stream_code_key).get("update"))
86+
87+
if software_version_data.get(stream_code_key).get("update_url"):
88+
print("\tUpdate URL: %s" % software_version_data.get(stream_code_key).get("update_url"))
89+
90+
if software_version_data.get(stream_code_key).get("download_url"):
91+
print("\tDownload URL: %s" % software_version_data.get(stream_code_key).get("download_url"))
92+
93+
if software_version_data.get(stream_code_key).get("release_date"):
94+
print("\tIt was released: %s" % software_version_data.get(stream_code_key).get("release_date"))
95+
96+
print(" Some sample user agents with the latest version numbers:")
97+
98+
# if there are sample user agents (eg. Flash and Java can't have sample user agents..), display them
99+
if software_version_data.get(stream_code_key).get("sample_user_agents") is not None:
100+
for sample_user_agent_group in software_version_data.get(stream_code_key).get("sample_user_agents"):
101+
print("\tUser agents for %s on %s [%s]" % (sample_user_agent_group, software_key, stream_code_key))
102+
for sample_user_agent in software_version_data.get(stream_code_key).get("sample_user_agents").get(sample_user_agent_group):
103+
print("\t\t%s" % sample_user_agent)
104+
105+
print("-------------------------------")
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Sample code for the WhatIsMyBrowser.com API - Version 2
4+
#
5+
# Database Dump Url
6+
# This sample code provides an example of querying the API
7+
# to get the URLs of the latest user agent database dump.
8+
# The database is not for decoding/parsing user agents,
9+
# you should use the User Agent Parse API Endpoint instead.
10+
#
11+
# It should be used as an example only, to help you get started
12+
# using the API. This code is in the public domain, feel free to
13+
# take it an integrate it with your system as you require.
14+
# Refer to the "LICENSE" file in this repository for legal information.
15+
#
16+
# For further documentation, please refer to the Integration Guide:
17+
# https://developers.whatismybrowser.com/api/docs/v2/integration-guide/
18+
#
19+
# For support, please refer to our Support section:
20+
# https://developers.whatismybrowser.com/api/support/
21+
22+
import requests
23+
import json
24+
25+
# Your API Key
26+
# You can get your API Key by following these instructions:
27+
# https://developers.whatismybrowser.com/api/docs/v2/integration-guide/#introduction-api-key
28+
api_key = ""
29+
30+
# choose the format you want to download by uncommenting it
31+
file_format = "mysql"
32+
#file_format = "csv"
33+
#file_format = "txt"
34+
35+
36+
# Where will the request be sent to
37+
api_url = "https://api.whatismybrowser.com/api/v2/user_agent_database_dump_url?file_format=%s" % file_format
38+
39+
# -- Set up HTTP Headers
40+
headers = {
41+
'X-API-KEY': api_key,
42+
}
43+
44+
# -- Make the request
45+
result = requests.get(api_url, headers=headers)
46+
47+
48+
# -- Try to decode the api response as json
49+
result_json = {}
50+
try:
51+
result_json = result.json()
52+
except Exception as e:
53+
print(result.text)
54+
print("Couldn't decode the response as JSON:", e)
55+
exit()
56+
57+
# -- Check that the server responded with a "200/Success" code
58+
if result.status_code != 200:
59+
print("ERROR: not a 200 result. instead got: %s." % result.status_code)
60+
print(json.dumps(result_json, indent=2))
61+
exit()
62+
63+
# -- Check the API request was successful
64+
if result_json.get('result', {}).get('code') != "success":
65+
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')))
66+
#print(json.dumps(result_json, indent=2))
67+
exit()
68+
69+
# Now you have "result_json" and can store, display or process any part of the response.
70+
71+
# -- Print the entire json dump for reference
72+
print(json.dumps(result_json, indent=2))
73+
74+
# -- Copy the `user_agent_database_dump` data to a variable for easier use
75+
user_agent_database_dump = result_json.get("user_agent_database_dump")
76+
77+
print("You requested the %s data format." % file_format)
78+
print("The latest data file contains %s user agents" % user_agent_database_dump.get("num_of_useragents"))
79+
print("You can download it from: %s" % user_agent_database_dump.get("url"))

code/user_agent_database_search.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)