view hae_saatiedot.tcl @ 0:1c4e2814cd41

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 21 Sep 2010 13:12:49 +0300
parents
children 7b03971c6d28
line wrap: on
line source

#!/usr/bin/tclsh

# Polku ja tiedosto mihin tiedot talletetaan
set datafile "/home/niinuska/bot/saa.data"

# HTTP proxy
# 1 = kyllä
# 0 = ei
set use_proxy 0

# HTTP proxy osoite ja portti (jos ed. optio 1)
set http_proxy_host "cache.inet.fi"
set http_proxy_port 800


##############################################################################
package require http
::http::config -urlencoding iso8859-1 -useragent "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 9.5"
if {$use_proxy != 0} {
	::http::config -proxyhost $http_proxy_host -proxyport $http_proxy_port
}


##############################################################################
proc fetch_uri {uurl} {
	global ngot
	if {[catch {set utoken [::http::geturl $uurl -binary true -timeout 5000]} uerrmsg]} {
		puts "Error getting #$uurl: $uerrmsg"
		return ""
	} else {
		incr ngot
		set udata [::http::data $utoken]
		::http::cleanup $utoken
		return $udata
	}
}

##############################################################################
set tmpfname "$datafile.tmp"
if {[catch {set savefile [open $tmpfname w 0600]} uerrmsg]} {
	puts "Error opening $tmpfname: $uerrmsg"
	return 1
}

# Haetaan 22 framea osoitteista 
# http://www.tiehallinto.fi/alk/tiesaa/tiesaa_maak_1.html -
# http://www.tiehallinto.fi/alk/tiesaa/tiesaa_maak_22.html
# ja talletetaan kaikki samaan tiedostoon
set ngot 0
for { set i 1 } { $i < 23 } { incr i } {
	puts $savefile [fetch_uri "http://alk.tiehallinto.fi/alk/tiesaa/tiesaa_maak_$i.html"]
}

#set paske [fetch_uri "http://www.wunderground.com/global/stations/56294.html"]

close $savefile
if {$ngot > 5} {
	if {[catch {file rename -force -- $tmpfname $datafile} uerrmsg]} {
		puts "Error renaming $tmpfname to $datafile: $uerrmsg"
	}
}