1#!../expect -f
2
3# weather - Expect script to get the weather (courtesy University of Michigan)
4# Don Libes
5# Version 1.10
6
7# local weather is retrieved if no argument
8# argument is the National Weather Service designation for an area
9# I.e., WBC = Washington DC (oh yeah, that's obvious)
10
11# Notes from Larry Virden <lvirden@yahoo.com> about the new host,
12# rainmaker.wunderground.com: "[the] new site requires the
13# machine doing the request be located in reverse dns lookup
14# or it refuses to provide data."  This appears to be a blind error
15# condition on the part of rainmaker.
16
17exp_version -exit 5.0
18
19if {$argc>0} {set code $argv} else {set code "WBC"}
20
21proc timedout {} {
22	send_user "Weather server timed out.  Try again later when weather server is not so busy.\n"
23	exit 1
24}
25
26set timeout 60
27
28set env(TERM) vt100	;# actual value doesn't matter, just has to be set
29
30spawn telnet rainmaker.wunderground.com 3000
31while {1} {
32	expect timeout {
33		send_user "failed to contact weather server\n"
34		exit
35	} "Press Return to continue*" {
36               # this prompt used sometimes, eg, upon opening connection
37               send "\r"
38	} "Press Return for menu*" {
39               # this prompt used sometimes, eg, upon opening connection
40               send "\r"
41	} "M to display main menu*" {
42		# sometimes ask this if there is a weather watch in effect
43		send "M\r"
44	} "Change scrolling to screen*Selection:" {
45		break
46	} eof {
47		send_user "failed to telnet to weather server\n"
48		exit
49	}
50}
51send "C\r"
52expect timeout timedout "Selection:"
53send "4\r"
54expect timeout timedout "Selection:"
55send "1\r"
56expect timeout timedout "Selection:"
57send "1\r"
58expect timeout timedout "city code:"
59send "$code\r"
60expect $code		;# discard this
61
62while {1} {
63	expect timeout {
64		timedout
65	} "Press Return to continue*:*" {
66		send "\r"
67	} "Press Return to display statement, M for menu:*" {
68		send "\r"
69	} -re "(.*)CITY FORECAST MENU.*Selection:" {
70		break
71	}
72}
73
74send "X\r"
75expect
76