Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Current temperature
#1
This macro will display the current temperature based on zipcode (US only).
Code:
Copy      Help
str Src Message zipcode url
zipcode="29455" ;;enter your zipcode here
ARRAY(str) Temp FeelsLike
url.from("http://www.weather.com/weather/local/" zipcode "?lswe=" zipcode "&lwsa=WeatherLocalUndeclared&from=whatwhere")
int MatchSucess

IntGetFile
if findrx(Src "<B CLASS=obsTempTextA>([\d]{1,3})" 0 0 Temp) ;;([\d]{1,3}+)
,if findrx(Src "<B CLASS=obsTextA>Feels Like<BR> ([\d]{1,3})" 0 0 FeelsLike)
,,Message.from("Temperature: " Temp[1] "F" "[]Feels Like: " FeelsLike[1] "F")
,,mes(Message "Current Temperature" "isa")
Matt B
#2
WOW nice code you got there can be usefull sometimes Big Grin
#3

I'm liking this.
Taking on Quick Macros one day at a time
#4

I was trying to add onto this somewhat and make the 10-Day-Forecast, but I can't get the hang of using
findrx.
Taking on Quick Macros one day at a time
#5

The 10-Day Forecast, complete.


Macro ( 10 Day Forecast ) Trigger ( @11 )
Code:
Copy      Help
str s message zipcode url
zipcode="48183" ;;<----------Enter your zipcode
url.from("http://www.weather.com/weather/tenday/" zipcode)
IntGetFile url s
ARRAY(str) a
HtmlTableToArray s 12 a 0 2

int i nTDinRow=8
for i 0 a.len nTDinRow

,message.formata("%s %s %s %s %s %s[]" a[i+1] "[]Hi/Lo:" a[i+5] "[]" a[i+2] "[]-----------------------")

mes message "10-Day Forecast" "sa"


Must have function: HtmlTableToArray

Function ( HtmlTableToArray )
Code:
Copy      Help
;/;
function $HTML VARIANT'tableNameOrIndex [ARRAY(str)&a] [ARRAY(MSHTML.IHTMLElement)&a2] [flags] [str&tableText] ;;flags: 1 get HTML, 2 get all TD elements, including of inner tables

;Gets cells of a HTML table into array.

;HTML - all HTML (page source).
;tableNameOrIndex - table name or 0-based index in the html.
;a - array variable for results. The function creates 1-dimension array where each element is cell text. Can be 0 if not needed.
;a2 - array variable for results. The function creates 1-dimension array where each element is cell object that can be used to get html element within the cell. Can be 0 if not needed.
;tableText - optional str variable that receives whole text of the table. Can be 0 if not needed.



MSHTML.IHTMLDocument2 d; MSHTML.IHTMLDocument3 d3
HtmlParse HTML d d3

IDispatch disp=d3.getElementsByTagName("TABLE").item(tableNameOrIndex); err end "the specified table does not exist"
MSHTML.IHTMLElementCollection cells

if(flags&2)
,MSHTML.IHTMLElement2 t2=disp
,cells=t2.getElementsByTagName("TD")
else
,MSHTML.IHTMLTable2 table=disp
,cells=table.cells

int i nc=cells.length
if(&a) a.create(nc)
if(&a2) a2.create(nc)

for i 0 nc
,MSHTML.IHTMLElement el=cells.item(i)
,if(&a) if(flags&1) a[i]=el.innerHTML; else a[i]=el.innerText
,if(&a2) a2[i]=el
,el=0 ;;without this may be exception in destructors, don't know why

if(&tableText)
,el=disp
,if(flags&1) tableText=el.innerHTML; else tableText=el.innerText


Also, HtmlParse:
Function ( HtmlParse )
Code:
Copy      Help
function $HTML [MSHTML.IHTMLDocument2&doc2] [MSHTML.IHTMLDocument3&doc3]

;Creates document object model of HTML.

;HTML - HTML.
;doc2, doc3 - variables that, after calling the function, can be used to get parsed HTML information. Can be omitted or 0.



MSHTML.IHTMLDocument2 d._create(uuidof(MSHTML.HTMLDocument))
ARRAY(VARIANT) a.create(1)
a[0]=HTML
d.write(a)

if(&doc2) doc2=d
if(&doc3) doc3=+d
Taking on Quick Macros one day at a time
#6
Hey i dont have the function "HtmlParse" ???
#7
NeverMind found it!
#8
Hey my freind tried using this and doesnt work.....does it only work in the USA?
#9

Yes, only united states zipcodes.
Taking on Quick Macros one day at a time
#10
ok...thought that cud be the reason...any way it can be adjusted to use a site where u can enter any zip?
#11

If you changed the site, then you would have to change the whole code for finding the information.
Taking on Quick Macros one day at a time
#12
OK TY
#13
hey matt,
is there a reason for this line?

Src.setclip


it seems it was just for debug but i'm not sure.

thanks.

btw: i use this and the Speak function to get the temp 5 before the hour so i know how to dress before i go to lunch or home.

great work!!!
An old blog on QM coding and automation.

The Macro Hook
#14
Ken,
Sorry I'm replying so late. I haven't played with QM in a while.
Quote:is there a reason for this line?

Src.setclip


it seems it was just for debug but i'm not sure.
You're right Src.setclip was used during debugging. Forgot to delete it. Thanks for the catch.

Matt B
Matt B
#15
QM Assistant Wrote:
The 10-Day Forecast, complete.


Macro ( 10 Day Forecast ) Trigger ( @11 )
Code:
Copy      Help
str s message zipcode url
zipcode="48183" ;;<----------Enter your zipcode
url.from("http://www.weather.com/weather/tenday/" zipcode)
IntGetFile url s
ARRAY(str) a
HtmlTableToArray s 12 a 0 2

int i nTDinRow=8
for i 0 a.len nTDinRow

,message.formata("%s %s %s %s %s %s[]" a[i+1] "[]Hi/Lo:" a[i+5] "[]" a[i+2] "[]-----------------------")

mes message "10-Day Forecast" "sa"


Any idea why I get this error now?
Quote:Error (RT) in 10 Day Forecast: invalid index
This shows up right before:
Quote:,message.formata("%s %s %s %s %s %s[]" a[i+1] "[]Hi/Lo:" a[i+5] "[]" a[i+2] "[]-----------------------")
Taking on Quick Macros one day at a time
#16
Probably page source HTML has been changed. For example, table index now can be not 12.
#17

Alright, thanks, i'll give it a look and see if I can find the problem.
Taking on Quick Macros one day at a time
#18
I tried to open the URL in web browser and it says page not found.
#19

That's the first thing I just checked, but it opens for me.
Taking on Quick Macros one day at a time
#20
Quote:We're sorry, the page you requested was not found on weather.com
#21

I just put this into a seperate macro, and it works for me.

Macro ( Macro88 )
Code:
Copy      Help
str s message zipcode url
zipcode="48183" ;;<----------Enter your zipcode
url.from("http://www.weather.com/weather/tenday/" zipcode)
web url 8
Taking on Quick Macros one day at a time
#22

I tried Matt's version of this, with just current temperature, and his still works. Could this have something to do with the HtmlTables being arrayed?
Taking on Quick Macros one day at a time
#23
urls in code1 and code2 are different

http://www.weather.com/weather/local/29 ... =whatwhere
http://www.weather.com/weather/tenday/48183

If the second url worked but now not, I think the page structure changed. Need to edit the macro.
#24

I'll take a better look at it when I get some free time.
Taking on Quick Macros one day at a time
#25
Some reason on the top temp code i get an error

Error in "Macro /240"oMacro: expected 2 to 8 arguments, not 0.

And it highlights IntGetFile


Any ideas on what is wrong?
#26
should be
IntGetFile url Src
#27
Thanks now it works perfectly
#28
Im trying to make this tell the Forecast, like if its snowing or rainging or sunny, getting that from the same page as temp.
but for some reason this is not working? any ideas...

Macro
Code:
Copy      Help
str Src Message Message2 zipcode url
zipcode="08620" ;;enter your zipcode here
str Forecast
url.from("http://www.weather.com/weather/local/" zipcode "?lswe=" zipcode "&lwsa=WeatherLocalUndeclared&from=whatwhere")

IntGetFile url Src
if findrx(Src "<B class=obsTextA>[a-zA-Z]</B>" 0 0 Forecast)
,Message.from("Forecast: " Forecast)
,mes(Message "Current Temperature" "isa")
#29
incorrect regular expression and findrx usage
#30
Im guessing the problem is in this line

if findrx(Src "<B class=obsTextA>[a-zA-Z]</B>" 0 0 Forecast)

most likely the part [a-zA-Z], i wasnt exactly sure what to use...
it changes between different things ex.
-Sunny
-cloudy
-sunny and cloudy
-snow storm
-rain
-scattered showers

any idea thanks.


Forum Jump:


Users browsing this thread: 1 Guest(s)