Last active
September 16, 2023 01:02
-
-
Save mike-weiner/3ea4654ccfec57234c0520e773f78225 to your computer and use it in GitHub Desktop.
A Python script to serve as a template for WeatherLink API calls.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# API Tutorial: https://weatherlink.github.io/v2-api/tutorial | |
# Before using this script, I would read this tutorial. It is very well written and explains why we have to do certain things in this script. | |
# Import the required dependencies. | |
# You may need to run `pip3 install hashlib hmac json requests time` on your local machine to install them. | |
import hashlib | |
import hmac | |
import json | |
import requests | |
import time | |
# API v2 Base URL | |
BASE_URL = "https://api.weatherlink.com/v2/" | |
# Your WeatherLink Account API Credentials. | |
# Generate at https://www.weatherlink.com/account. | |
API_KEY = "<your-api-key>" | |
API_SECRET = "<your-api-secret>" | |
# The value of this variable should match one of the API Routes found here: https://weatherlink.github.io/v2-api/api-reference | |
# An example value for this variable might be "stations/" if you want to get the stations that your account has access to. | |
# Another example value for this variable might be "current/<your-station-id>" to get the current weather data for a weather station. | |
endpoint = "<your-api-endpoint-value>" | |
# API Signature parameters | |
# Add the additional parameters needed to build the API signature value | |
signature_parameters = [ | |
("api-key", API_KEY), | |
("t", str(int(time.time()))) | |
] | |
# Sort API parameters alphabetically (per WeatherLink Live specification) | |
signature_parameters = sorted(signature_parameters) | |
# API URI parameters | |
# Add the additional parameters needed for your API call (api-key and t are required for every call) | |
uri_parameters = [ | |
("api-key", API_KEY), | |
("t", str(int(time.time()))) | |
] | |
# Sort API parameters alphabetically (per WeatherLink Live specification) | |
uri_parameters = sorted(uri_parameters) | |
# Create API Signature using constants and credentials above. | |
api_signature = "" | |
for (key, value) in parameters: | |
api_signature = api_signature + key + value | |
# Hash the API signature. | |
hash = hmac.new(API_SECRET.encode('UTF-8'), api_signature.encode('UTF-8'), hashlib.sha256).hexdigest() | |
# Create final API URL. | |
api_url = BASE_URL + endpoint + "?" + ''.join("&" + str(x) + "=" + str(y) for (x,y) in parameters)[1:] + "&api-signature=" + hash | |
# Make call to API and print formatted results. | |
api_results = requests.get(api_url, verify=True) | |
print(json.dumps(json.loads(api_results.text), indent=4)) |
A v2 of this script that uses the new, simpler header authentication method can be found at: https://gist.github.com/mike-weiner/1f6d9dc424b2ad684e56b3af6866e82f.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Specifics of the WeatherLink v2 API can be found here: https://weatherlink.github.io/v2-api/