Mercurial > hg > egg-tcls
annotate hae_saatiedot.tcl @ 172:2c89e47739e8
weather: Change weather data loading to not discard current data if data
file can't be opened.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 03 Jun 2014 12:59:53 +0300 |
parents | 593874678e45 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/tclsh |
69
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
2 # |
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
3 # NOTICE! Change above path to correct tclsh binary path! |
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
4 # |
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
5 ########################################################################## |
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
6 # |
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
7 # Improved weather data fetcher for Jedah's "saa.tcl" |
114
593874678e45
Clarify authorship by doing sed "s/ccr\/TNSP/Matti 'ccr' Hamalainen/g".
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
8 # by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
69
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
9 # (C) Copyright 2009-2011 Tecnic Software productions (TNSP) |
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
10 # |
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
11 # This script is freely distributable under GNU GPL (version 2) license. |
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
12 # |
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
13 ########################################################################## |
0 | 14 |
69
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
15 # Path and filename of the data file, MUST BE SAME as in "saa.tcl" |
0 | 16 set datafile "/home/niinuska/bot/saa.data" |
17 | |
69
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
18 # Use a HTTP proxy? (1 = yes, 0 = no) |
0 | 19 set use_proxy 0 |
20 | |
69
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
21 # HTTP proxy host address and port (only needed if use_proxy = 1) |
0 | 22 set http_proxy_host "cache.inet.fi" |
23 set http_proxy_port 800 | |
24 | |
25 | |
69
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
26 ########################################################################## |
0 | 27 package require http |
28 ::http::config -urlencoding iso8859-1 -useragent "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 9.5" | |
29 if {$use_proxy != 0} { | |
63 | 30 ::http::config -proxyhost $http_proxy_host -proxyport $http_proxy_port |
0 | 31 } |
32 | |
33 | |
69
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
34 ########################################################################## |
0 | 35 proc fetch_uri {uurl} { |
63 | 36 global ngot |
37 if {[catch {set utoken [::http::geturl $uurl -binary true -timeout 5000]} uerrmsg]} { | |
38 puts "Error getting #$uurl: $uerrmsg" | |
39 return "" | |
40 } else { | |
41 incr ngot | |
42 set udata [::http::data $utoken] | |
43 ::http::cleanup $utoken | |
44 return $udata | |
45 } | |
0 | 46 } |
47 | |
69
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
48 ########################################################################## |
0 | 49 set tmpfname "$datafile.tmp" |
50 if {[catch {set savefile [open $tmpfname w 0600]} uerrmsg]} { | |
63 | 51 puts "Error opening $tmpfname: $uerrmsg" |
52 return 1 | |
0 | 53 } |
54 | |
55 # Haetaan 22 framea osoitteista | |
56 # http://www.tiehallinto.fi/alk/tiesaa/tiesaa_maak_1.html - | |
57 # http://www.tiehallinto.fi/alk/tiesaa/tiesaa_maak_22.html | |
58 # ja talletetaan kaikki samaan tiedostoon | |
59 set ngot 0 | |
60 for { set i 1 } { $i < 23 } { incr i } { | |
63 | 61 puts $savefile [fetch_uri "http://alk.tiehallinto.fi/alk/tiesaa/tiesaa_maak_$i.html"] |
0 | 62 } |
63 | |
64 #set paske [fetch_uri "http://www.wunderground.com/global/stations/56294.html"] | |
65 | |
66 close $savefile | |
67 if {$ngot > 5} { | |
63 | 68 if {[catch {file rename -force -- $tmpfname $datafile} uerrmsg]} { |
69 puts "Error renaming $tmpfname to $datafile: $uerrmsg" | |
70 } | |
0 | 71 } |
72 |