rem Basic! Weather App 1.0
! Initialize TTS
tts.init
startprog:
! Setup Variables - Mostly for parsing Google's weather API XML result
qq$ = chr$(34)
zpfile$ = "zip.txt"
wx$ = "weather.txt"
br$ = " "
dbbr$ = "
"
wxdateP$ = " 5
popup "please enter a 5 digit zip code only",0,0,1
goto entzip
else if val(zce$) = 0
popup "please do not use letters and/or all zeros in the zip code.",1
goto entzip
endif
zip$ = zce$
! File operations to save as zip.txt with zip code in it
text.open W, fn1, zpfile$
text.writeln fn1, zip$
text.close fn1
goto ldgxml
! Load zip code in
lddata:
text.open R, fn2, zpfile$
text.readln fn2, zip$
text.close fn2
! Load google wx xml
ldgxml:
gxml$ = "http://www.google.com/ig/api?weather=" + zip$
graburl xmldata$, gxml$
t = len(xmldata$)
! Parse xml for City Location
p = val(cityN$)
x = 1
y = t - p
While x < y
if mid$(xmldata$, x, p) = cityP$ then gosub grabval
if f = 1 then w_r.break
x = x + 1
repeat
f = 0
if x = y then goto prsfail
cityD$ = prs$
print "Location Data Loaded..."
! Parse xml for Date
p = val(wxdateN$)
x = 1
y = t - p
While x < y
if mid$(xmldata$, x, p) = wxdateP$ then gosub grabval
if f = 1 then w_r.break
x = x + 1
repeat
f = 0
if x = y then goto prsfail
wxdateD$ = prs$
print "Date Loaded..."
gosub fixdate
! Parse xml for Current Conditions
p = val(currentN$)
x = 1
y = t - p
While x < y
if mid$(xmldata$, x, p) = currentP$ then gosub grabval
if f = 1 then w_r.break
x = x + 1
repeat
f = 0
if x = y then goto prsfail
currentD$ = prs$
print "Current Conditions Loaded..."
! Parse xml for Current icon
p = val(curicoN$)
x = 1
y = t - p
While x < y
if mid$(xmldata$, x, p) = curicoP$ then gosub grabval
if f = 1 then w_r.break
x = x + 1
repeat
f = 0
if x = y then goto prsfail
curicoD$ = prs$
print "Weather Graphic Loaded..."
ph$ = "http://www.google.com" + curicoD$
curicoD$ = ph$
! Parse xml for Current Temperature
p = val(curtempN$)
x = 1
y = t - p
While x < y
if mid$(xmldata$, x, p) = curtempP$ then gosub grabval
if f = 1 then w_r.break
x = x + 1
repeat
f = 0
if x = y then goto prsfail
curtempD$ = prs$
print "Temperature Loaded..."
! Parse xml for Current Humidity
p = val(humidityN$)
x = 1
y = t - p
While x < y
if mid$(xmldata$, x, p) = humidityP$ then gosub grabval
if f = 1 then w_r.break
x = x + 1
repeat
f = 0
if x = y then goto prsfail
humidityD$ = prs$
print "Humidity Loaded..."
! Build Head of html
h$ = ""
h$ = h$ + ""
! Build body of html
h$ = h$ + "
"
! Build End of html page
h$ = h$ + ""
! Display html page in browser
html.open
html.load.string h$
! Wait for Input Then Go
! Loop until data$ received
waiting:
do
html.get.datalink data$
until data$ <> ""
type$ = left$(data$, 4)
data$ = mid$(data$,5)
sw.begin type$
! Back Key hit: refresh weather data
sw.case "BAK:"
html.close
cls
goto startprog
sw.break
! An error occured
sw.case "ERR:" + currentD$
print "Error: " + data$
end
sw.break
! User data returned
sw.case "DAT:"
if left$(data$, 3) = "Exi"
print "App closed by user request."
html.close
end
elseif left$(data$, 3) = "Ano"
html.close
goto entzip
elseif left$(data$, 3) = "Tal"
gosub speakit
goto waiting
endif
sw.break
sw.default
print "Unexpected data type:", type$ + data$
END
sw.end
html.close
end
speakit:
tts.speak "conditions for"
tts.speak cityD$
tts.speak CURRENTD$
tts.speak "temperature"
tts.speak curtempD$
tts.speak "degrees fahrenheit"
tts.speak humidityD$
return
grabval:
prs$ = ""
z = x + p
do
prs$ = prs$ + mid$(xmldata$, z, 1)
z = z + 1
srch$ = mid$(xmldata$, z, 1)
until srch$ = qq$
f = 1
return
! Make date look to my preference
fixdate:
yr$ = left$(wxdateD$,4)
mo$ = mid$(wxdateD$, 6, 2)
m = val(mo$)
day$ = mid$(wxdateD$, 9, 2)
if m = 1 then mo$ = "January"
if m = 2 then mo$ = "February"
if m = 3 then mo$ = "March"
if m = 4 then mo$ = "April"
if m = 5 then mo$ = "May"
if m = 6 then mo$ = "June"
if m = 7 then mo$ = "July"
if m = 8 then mo$ = "August"
if m = 9 then mo$ = "September"
if m = 10 then mo$ = "October"
if m = 11 then mo$ = "November"
if m = 12 then mo$ = "December"
wxdateD$ = mo$ + " " + day$ + ", " + yr$
return
prsfail:
print "You may have entered a bad zip code or are having connection difficulties."
print "If this keeps happening, then this program is out of date. Please upgrade to the newest release."
print "...please wait a moment"
pause 10000
file.delete d, "zip.txt"
goto startprog
! Exit App because no internet or no network or error
wxxit:
Print " Error - No interent available. Please check your internet connection and try again."
end