Automatic Data Download
Better Weather Story℠ uses standard HTTP requests and responses to deliver data to you, so no special tools are required to obtain this data. You may wish to bookmark the URLs built by the Query Builder in your web browser to download frequently-used data sets on demand, but you can also use built-in operating system tools like Windows Task Scheduler and PowerShell to automatically download your data on a schedule.
Downloading using PowerShell
PowerShell is bundled with every modern version of Windows and includes a command called Invoke-WebRequest
that can save responses from the Better Weather Story API directly to a folder on your computer without having to go through a web browser.
The command to use in PowerShell looks like this:
Invoke-WebRequest -UseBasicParsing -Uri 'https://a.bwssvc.net/astro/sun?lat=39.09914360720657&lng=-84.49881076812744&format=csv&apikey=00000000-0000-0000-0000-000000000000' -OutFile 'C:\PathToBWSData\Sun-Cincinnati.csv'
The -Uri
argument specifies the URL to be downloaded and the -OutFile
argument tells PowerShell where to save the CSV file. Make sure both these arguments are enclosed in single quotes as shown here.
Caveats
- If the CSV file already exists, this command will overwrite it with new data.
- The public IP address of the computer you run these commands from, if not the same computer you normally use to access the Better Weather Story API, must be allowed to use your API key. See the App Settings documentation for information on how to do this. The easiest way to find a computer's public IP address is to open a web browser and type "what is my IP address" into your favorite search engine.
Automating Data Download
The PowerShell command described above can be set to run at scheduled intervals by using the PowerShell scheduled jobs feature. This may be desirable when you wish to have unattended access to frequently updated data, such as power outage trends after a weather event for your news department.
This is an advanced topic! You may want or need to have your IT or engineering department help you set this up.
PowerShell includes a command called Register-ScheduledJob
to make this easy. For example, if you wanted to automatically load sunrise/sunset data every morning at 3 AM, you can use Register-ScheduledJob
to accomplish this. An example would look like this:
- You must run PowerShell as an administrator for
Register-ScheduledJob
to work. Otherwise, you will see an "access denied" error.
Register-ScheduledJob `
–Name "BWS-Sun-Cincinnati" `
-ScriptBlock { `
Invoke-WebRequest -UseBasicParsing `
-Uri 'https://a.bwssvc.net/astro/sun?lat=39.09914360720657&lng=-84.49881076812744&format=csv&apikey=00000000-0000-0000-0000-000000000000' `
-OutFile 'C:\PathToBWSData\Sun-Cincinnati.csv' `
} `
-Trigger (New-JobTrigger -Daily -At "3:00 AM")
Once again, you can see the -Uri
and -OutFile
parameters for the source URL and the path to save the downloaded data. The -Trigger
parameter uses another command called New-JobTrigger
to run the job every day at 3 AM. For more information about the New-JobTrigger
command, see this Microsoft documentation.
Be sure to note the name of the scheduled job you just registered, because you will need it to change or delete the job later!
- Tip: Copy and paste the example command above into a text editor like Notepad to get the URL, the save path, and the trigger times correct before pasting it into Windows PowerShell.