Last active
December 11, 2024 20:06
-
-
Save mike-weiner/1f6d9dc424b2ad684e56b3af6866e82f to your computer and use it in GitHub Desktop.
A Python script to serve as a template for WeatherLink API calls using the newer authentication method.
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
import json | |
import requests | |
# API v2 Base URL | |
BASE_URL = "https://api.weatherlink.com/v2/" | |
# Your WeatherLink Account API Credentials | |
API_KEY = "<your-api-key>" | |
API_SECRET = "<your-api-secret>" | |
# API v2 Endpoint URL | |
# https://weatherlink.github.io/v2-api/api-reference | |
endpoint = "stations" | |
# API Path Parameters | |
# Add the necessary _path_ parameters necessary for the API endpoint that you are querying | |
pathParameters = [ | |
] | |
# API Query String Parameters | |
# Add the necessary _query string_ parameters necessary for the API endpoint that you are querying | |
queryParameters = [ | |
] | |
# Create final API URL | |
api_url = BASE_URL + endpoint | |
api_url += ''.join("/" + str(param) for param in pathParameters)[0:] | |
api_url += "?api-key=" + API_KEY | |
api_url += ''.join("&" + str(x) + "=" + str(y) for (x,y) in queryParameters)[0:] | |
# Make call to API and pretty-print returned data | |
api_results = requests.get( | |
headers= { | |
"X-Api-Secret": API_SECRET | |
}, | |
url=api_url, | |
verify=True, | |
) | |
print(json.dumps(json.loads(api_results.text), indent=4)) |
Using the above template, this is how you could get a historic weather conditions record for a station.
import json
import requests
# API v2 Base URL
BASE_URL = "https://api.weatherlink.com/v2/"
# Your WeatherLink Account API Credentials
API_KEY = "<your-api-key>"
API_SECRET = "<your-api-secret>"
# API v2 Endpoint URL
# https://weatherlink.github.io/v2-api/api-reference
endpoint = "historic"
# API Path Parameters
# Add the necessary _path_ parameters necessary for the API endpoint that you are querying
pathParameters = [
<your-station-id>
]
# API Query String Parameters
# Add the necessary _query string_ parameters necessary for the API endpoint that you are querying
queryParameters = [
("start-timestamp", str(int(time.time())-10000)),
("end-timestamp", str(int(time.time())))
]
# Create final API URL
api_url = BASE_URL + endpoint
api_url += ''.join("/" + str(param) for param in pathParameters)[0:]
api_url += "?api-key=" + API_KEY
api_url += ''.join("&" + str(x) + "=" + str(y) for (x,y) in queryParameters)[0:]
# Make call to API and pretty-print returned data
api_results = requests.get(
headers= {
"X-Api-Secret": API_SECRET
},
url=api_url,
verify=True,
)
print(json.dumps(json.loads(api_results.text), indent=4))
If you are looking for more documentation, there are some great docs at https://weatherlink.github.io/v2-api/tutorial!
The "older" signature authentication method is still supported. A template script for that authentication method is found here: https://gist.github.com/mike-weiner/3ea4654ccfec57234c0520e773f78225
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using the above template, this is how you could get the current weather conditions for a station.