UK weather images - I use the BBC's (1 Viewer)

cheezey

Community Plugin Dev
August 26, 2004
1,560
312
56
West Yorks, UK
Home Country
United Kingdom United Kingdom
BBC weather images

As a subject (not a guest) of Her Majesty I am:

  1. Obsessed with the weather forecast
  2. Genetically programmed to understand the BBC weather maps

That said, when I came to program up the MP Weather plugin with the images from the BBC website I found that they don't have static names, they change with the time of day, this isn't very MP friendly. I mailed the BBC about this (and a couple of other things :rolleyes: ) and got a reply saying . . . . mmmmm . . . . . we're looking at a few things.

So while they look at a few things, I scripted up the retrieval of the BBC weather images for my own personal use.

This script can take two parameters, that of the region you want the maps for, if you don't use a parameter you'll get the UK country maps (you can find the available regions in the comments of the code below), the second parameter is the path to where you want to save your images . You just need to then configure the MP weather plugin to load the images from your selected path.

I have this script running as a scheduled task every 30mins to keep the images updated.

If you want to use it:
  1. Save this code as C:\GetBBCWeatherImages.vbs
  2. Schedule wscript.exe C:\GetBBCWeatherImages.vbs <REGION> <PATH> - optional
e.g. For the North East of England images and to save to C:\Temp you schedule
wscript.exe C:\GetBBCWeatherImages.vbs NE C:\Temp

e.g. For Northern Europe and to save to the root of C:\ you schedule
wscript.exe C:\GetBBCWeatherImages.vbs NEUR​


I have pasted this code into a PHP box as the CODE box didn't like my CrLf and formatted it as one big block of text and the QUOTE box removed my Tabs.
PHP:
'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0
'
' NAME: GetBBCWeatherImages.vbs
'
' AUTHOR: Cheezey
' DATE  : 20/08/2006
'
' COMMENT: Grabs BBC weather images, gets the nearest image to the current
'          time, saves the images in the root of C:\ if not otherwise
'          specified
'
' USAGE: cscript //nologo GetBBCWeatherImages.vbs <REGION> <PATH>
'
'        <REGION> is optional but can accept the following
'                uk = United Kingdom (Default)
'                ci = Channel Islands
'                e  = England East
'                m  = England Midlands
'                ne = England North East
'                nw = England North West
'                ni = Northern Ireland
'                ss = Scotland South
'                sn = Scotland North
'                s  = England South
'                se = England South East
'                sw = England South West
'                wl = Wales
'                neur = Europe - North
'                eeur = Europe - Mediterranean and East
'                weur = Europe - West and Canary Islands
'                mide = Middle East
'                ausa = Australasia
'                cam  = America - Central
'                nam  = America - North
'                sam  = America - South
'                easia = Asia - East
'                sasia = Asia - South
'                seasia = Asia - South East
'
'	     <PATH> is optional, it's where you want to save the files To
'               including, it defaults to C:\
'
' DATE        ACTION
' 20/08/06    Created
' 28/08/06    Updated to work with world regions
' 28/08/06    Fixed bug where images before 10am (double figures) weren't
'             saved - DOH!!
' 28/08/06    Added the debug & error handling
'
'==========================================================================
Option Explicit
On Error Resume Next

'==========================================================================
' Initialise debug
'==========================================================================
Dim Debug : Debug = False

'==========================================================================
' Initialise objects
'==========================================================================
Dim oXML
Set oXML  = CreateObject("Microsoft.XMLHTTP")
Dim oArgs
Set oArgs = WScript.Arguments
Dim oFS
Set oFS   = CreateObject("Scripting.FileSystemObject")

'==========================================================================
' Initialise variables
'==========================================================================
Dim sRegion   : sRegion = "uk"
Dim sURLp     : sURLp   = "http://www.bbc.co.uk/weather/charts/"
Dim sArea     : sArea   = "uk"
Dim sPath     : sPath   = "c:\"

'==========================================================================
' Set the region & area using the parameter passed in, default to uk / uk
'==========================================================================
If oArgs.Count = 1 Or oArgs.Count = 2 Then
    sRegion = LCase(oArgs(0))
    Select Case sRegion
        Case "uk", "ci", "e", "m", "ne", "nw", "ni", "ss", "ns", "s", "se", "sw", "wl"
			sArea = "uk"
        Case "neur", "eeur", "weur", "mide", "ausa", "cam", "sam", "easia", "sasia", "seasia"
            sArea = "world"
        Case Else
			sArea = "uk"
            sRegion = "uk"
    End Select
End If
sURLp   = sURLp & sArea & "/"

'==========================================================================
' See if the path has been passed in, otherwise default to c:\
'==========================================================================
If oArgs.Count = 2 Then
	sPath = LCase(oArgs(1))
	If Not Right(sPath,1) = "\" Then sPath = sPath & "\"
	If Not oFS.FolderExists(sPath) Then sPath = "c:\"
End If

'==========================================================================
' Initialise yet more variables
'==========================================================================
Dim sURL
Dim sRain     : sRain     = sRegion & "_rain_"
Dim sCloud    : sCloud    = sRegion & "_cloud_"
Dim sTemp     : sTemp     = sRegion & "_temperature_"
Dim sWind     : sWind     = sRegion & "_wind_"
Dim sPressure : sPressure = sRegion & "_pressure_"
Dim sSummary  : sSummary  = sRegion & "_summary_"
Dim sVisSat   : sVisSat   = "uk_visiblesatellite_"

'==========================================================================
' The BBC uses yymmddhh as their format for the weather image filenames
' so get these in the correct format i.e. 2 char strings
'==========================================================================
Dim yy, mm, dd, td, yd, hh, mn
    yy = Padnum(2,CInt(Right(CStr(Year(Now())),2)))
    mm = Padnum(2,CInt(Month(Now())))
    dd = Padnum(2,CInt(Day(Now())))
    td = Padnum(2,CInt(Day(Now())+1))
    yd = Padnum(2,CInt(Day(Now())-1))
    hh = Padnum(2,CInt(Hour(Now())))
    mn = Padnum(2,CInt(Minute(Now())))
    
'==========================================================================
' Try and get all the images
'==========================================================================
GetImage sRain     & ".jpg", sRain
CheckForError("Error getting rain image.")
GetImage sCloud    & ".jpg", sCloud
CheckForError("Error getting cloud image.")
GetImage sTemp     & ".jpg", sTemp
CheckForError("Error getting temperature image.")
GetImage sWind     & ".jpg", sWind
CheckForError("Error getting wind image.")
GetImage sPressure & ".jpg", sPressure
CheckForError("Error getting pressure image.")
GetImage sSummary  & ".jpg", sSummary
CheckForError("Error getting summary image.")

'==========================================================================
' Only get the visible satellite image if the region is in the uk, 'Cos
' that's all they do
'==========================================================================
If sArea = "uk" Then 
	GetImage sVisSat & ".jpg", sVisSat
	CheckForError("Error getting satellite image.")
End If

'==========================================================================
' Tidy up
'==========================================================================
Set oXML  = Nothing
Set oArgs = Nothing
Set oFS   = Nothing

'==========================================================================
' Subs & Functions
'==========================================================================


Sub GetImage(strFilename, strType)

    Dim i, j, k, l
    '==========================================================================
    ' If minutes are greater than 30 (i.e. nearer the next hour) then add one
    ' to start at the next hour
    '==========================================================================
    If CInt(mn) < 31 Then
        i = CInt(hh)
        Else
            i = CInt(hh) + 1
    End If

    '==========================================================================
    ' Nothing was found in the previous loop so check the whole day (24 - 0)
    ' If no image is found, the next loops will check tomorrow or yesterday
    '==========================================================================
    k = 25
    l = 99
    For j = 0 To 24
        sURL = sURLp & strType & Cstr(yy) & Cstr(mm) & CStr(dd) & Padnum(2, j) & ".jpg"
        oXML.open "GET", sURL, False
        oXML.send
        If oXML.status = "200" Then
        	'==========================================================================
        	' Do a bit of jiggery pokery to get the image with the time nearest to the
        	' current time
			'==========================================================================
            If Abs(i - j) < k Then 
            	k = Abs(i - j)
            	l = j
            End If
        End If
    Next
    sURL = sURLp & strType & Cstr(yy) & Cstr(mm) & Cstr(dd) & Padnum(2, l) & ".jpg"
    oXML.open "GET", sURL, False
    oXML.send
    If oXML.status = "200" Then
        	'==========================================================================
        	' We've found a good image so save it
        	'==========================================================================
            SaveBinaryData sPath & strFilename, oXML.responseBody
            Exit Sub
    End If

End Sub

Function SaveBinaryData(FileName, ByteArray)

	Const adTypeBinary = 1
	Const adSaveCreateOverWrite = 2
	'==========================================================================
	' Create Stream object
	'==========================================================================
	Dim BinaryStream
	Set BinaryStream = CreateObject("ADODB.Stream")
	'==========================================================================
	'Specify stream type - we want to save binary data.
	'==========================================================================
	BinaryStream.Type = adTypeBinary
	
	'==========================================================================
	' Open the stream and write binary data to the object
	'==========================================================================
	BinaryStream.Open
	BinaryStream.Write ByteArray

	'==========================================================================
	' Save binary data to disk
	'==========================================================================
	BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
	
End Function

Function Padnum(intLength, intNumber)

    Dim iLen : iLen = Len(CStr(intNumber))

    If iLen < intLength Then        
        Padnum = String(intLength - iLen, "0") & CStr(intNumber)
        Else
            Padnum = CStr(intNumber)        
    End If
    
End Function 
	
Sub CheckForError(strMsg)

	If Err.Number <> 0 Then
		If Debug = True Then
			Dim fDebug
			Set fDebug = oFS.CreateTextFile("c:\GetBBCWeatherImages.vbs.ERRORS.txt", True)
			fDebug.WriteLine(String(78,"*"))
			fDebug.WriteLine(Now() & " : " & strMsg)
			fDebug.WriteLine(String(78,"*"))
			fDebug.WriteLine("Error number : " & CStr(Err.Number))
			fDebug.WriteLine("Error source : " & Err.Source)
			fDebug.WriteLine("Error text   : " & Err.Description)
			fDebug.Close
		End If
		WScript.Quit(1)
	End If
End Sub
 

tkortell

Portal Pro
April 13, 2005
320
0
44
Vaasa / Sulva
Home Country
Finland Finland
Good work Cheezey! :)

So if I would like to use this script to get some images from BBC covering the north_europe region what shanges do I need to do?

Regards / Tomas
 

surfermc

Portal Pro
May 7, 2006
369
17
Dorset
Home Country
England England
cheezey, Thanks for that a neat solution. I must be a genetic thing in the British to be obsessed with the weather.:)
 

Inker

Retired Team Member
  • Premium Supporter
  • December 6, 2004
    2,055
    318
    cheezey, Thanks for that a neat solution. I must be a genetic thing in the British to be obsessed with the weather.:)

    I wonder why, I mean how many times can you hear the word "rain" and "cold" and still act like you learned something new? :p
     

    cheezey

    Community Plugin Dev
    August 26, 2004
    1,560
    312
    56
    West Yorks, UK
    Home Country
    United Kingdom United Kingdom
    • Thread starter
    • Moderator
    • #6
    I wonder why, I mean how many times can you hear the word "rain" and "cold" and still act like you learned something new? :p

    Ah, but how many types of rain & cold are there? Plus, I'm an optimist, tomorrow will always be better ;)

    Good work Cheezey! :)

    So if I would like to use this script to get some images from BBC covering the north_europe region what shanges do I need to do?

    Regards / Tomas

    You can add neur as a region, I've done this for all the world regions the BBC does but have a wee problem with the fact that Oz is 12 hours ahead. I'll update the code above tomorrow.
     

    tkortell

    Portal Pro
    April 13, 2005
    320
    0
    44
    Vaasa / Sulva
    Home Country
    Finland Finland
    I've updated the code above to grab the BBC's world weather images too.

    Thanks :) it works great :), just one litle thing. Is it posible to make a "Scheduled Task" without a user account that has a password? I like to have my computer loggin automaticaly when started.

    Thanks again for the great script ;)

    Regards / Tomas
     

    cheezey

    Community Plugin Dev
    August 26, 2004
    1,560
    312
    56
    West Yorks, UK
    Home Country
    United Kingdom United Kingdom
    • Thread starter
    • Moderator
    • #10
    You can't run windows scheduled tasks with an account that has a blank password, but it's a once only setting.

    Once you've configured your scheduled task with the appropriate credentials, you will not need to supply the password again.

    You've got windows auto logging on so use the same user/password as you have for that - assuming that user account has a password, if not just create another user account with a password.

    Cheers
     

    Users who are viewing this thread

    Top Bottom