Yahoo Weather Download

Posted on
Fri Dec 06, 2013 11:16 am
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Yahoo Weather Download

Hey,

This script works fine to get weather info from Yahoo into a few variables
but it does strip out the minus sign if it's below zero...which it has been up here.
e.g. -15 becomes 15.

Appreciate it if someone has any idea how that can be fixed.

Code: Select all
-- Get Weather
-- This is the city code. Search the code for your city on http://weather.yahoo.com/
set CityCode to 2430513 ---Hungry Horse 12783901
-- Temperature format
set t_format to "F"
-- Voiceover format
set v_format to "S"
-- Say present condition
set a_format to "Y"

set IURL to "http://weather.yahooapis.com/forecastrss?w=" & CityCode

-- Downloading the file using curl
set file_content to (do shell script "curl " & IURL)
-- Looking for the line with actual condition
set theText to text ((offset of "yweather:condition" in file_content) + 1) thru -1 of file_content
set sub_1 to text ((offset of "\"" in theText) + 1) thru -1 of theText

-- Today conditions found
set actual_condition to text 1 thru ((offset of "\"" in sub_1) - 1) of sub_1

-- Looking for actual temperature temperature
set sub_1a to text ((offset of "temp=" in sub_1)) thru -1 of sub_1
set sub_1b to text ((offset of "\"" in sub_1a) + 1) thru -1 of sub_1a
set actual_temp to text 1 thru ((offset of "\"" in sub_1b) - 1) of sub_1b

if t_format is equal to "C" then
   set actual_temp to (5 / 9) * (actual_temp - 32) as integer
end if

-- Looking for today forecast
set theText to text ((offset of "yweather:forecast" in file_content) + 1) thru -1 of file_content
set sub_2 to text ((offset of "\"" in theText) + 1) thru -1 of theText

-- Maximum and minimum temperatures found
set today_min_temp to word 9 of sub_2
set today_max_temp to word 12 of sub_2
if t_format is equal to "C" then
   set today_min_temp to (5 / 9) * (today_min_temp - 32) as integer
   set today_max_temp to (5 / 9) * (today_max_temp - 32) as integer
end if

-- Looking for today forecast condition (a bit tricky)
set sub_3 to text ((offset of "text" in sub_2) + 1) thru -1 of sub_2
set sub_4 to text ((offset of "\"" in sub_3) + 1) thru -1 of sub_3
set today_forecast to text 1 thru ((offset of "\"" in sub_4) - 1) of sub_4

-- Looking for tomorrow forecast
set sub_5 to text ((offset of "yweather:forecast" in sub_4) + 1) thru -1 of sub_4
set sub_6 to text ((offset of "\"" in sub_5) + 1) thru -1 of sub_5

-- Maximum and minimum temperatures found
set tomorrow_min_temp to word 9 of sub_6
set tomorrow_max_temp to word 12 of sub_6
if t_format is equal to "C" then
   set tomorrow_min_temp to (5 / 9) * (tomorrow_min_temp - 32) as integer
   set tomorrow_max_temp to (5 / 9) * (tomorrow_max_temp - 32) as integer
end if

-- Looking for tomorrow forecast condition (a bit tricky)
set sub_7 to text ((offset of "text" in sub_6) + 1) thru -1 of sub_6
set sub_8 to text ((offset of "\"" in sub_7) + 1) thru -1 of sub_7
set tomorrow_forecast to text 1 thru ((offset of "\"" in sub_8) - 1) of sub_8

tell application "IndigoServer"
   set value of variable "Yahoo_tomorrow_max_temp" to tomorrow_max_temp as string
   set value of variable "Yahoo_tomorrow_forecast" to tomorrow_forecast as string
   set value of variable "Yahoo_tomorrow_min_temp" to tomorrow_min_temp as string
end tell



Thanks,

Carl

Posted on
Fri Dec 06, 2013 8:56 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Yahoo Weather Download

set sub_6 to "Sat\" date=\"7 Dec 2013\" low=\"-8\" high=\"5\" text=\"Sunny\" code=\"32\" />"

---------
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"\""}
set xxx to every text item of sub_6
set x to item 5 of xxx
display dialog x
--------------

gives -8

Karl
took me 20 minutes to figure that one out


here the complete code of that section:
Code: Select all
-- Looking for tomorrow forecast
set sub_5 to text ((offset of "yweather:forecast" in sub_4) + 1) thru -1 of sub_4
set sub_6 to text ((offset of "\"" in sub_5) + 1) thru -1 of sub_5
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"\""}
set xxx to every text item of sub_6
set AppleScript's text item delimiters to old_delims

-- Maximum and minimum temperatures found
set tomorrow_min_temp to item 5 of xxx
set tomorrow_max_temp to item 7 of xxx
if t_format is equal to "C" then
   set tomorrow_min_temp to (5 / 9) * (tomorrow_min_temp - 32) as integer
   set tomorrow_max_temp to (5 / 9) * (tomorrow_max_temp - 32) as integer
end if

Posted on
Sat Dec 07, 2013 12:46 am
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Yahoo Weather Download

Really appreciate your efforts! I spent a couple hours with it and got nowhere.
Seems most any coding and me just don't get along.

Works great, and many thanks!

Carl

Posted on
Sat Dec 07, 2013 5:59 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Yahoo Weather Download

just trial and error ... the items / words /lists / text things in applescript are still not 100% clear to me...I searched the web and tried things out.

when is an item just one letter and when more ..
seems to work better: make a text string into items ..."set xxx to every text item of sub_6" then you have a list of items that can be better accessed without counting character positions.

Karl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 17 guests