view hae_saatiedot.tcl @ 174:d94be9a1be6a

weather: Add support for listing currently defined aliases.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 03 Jun 2014 13:00:38 +0300
parents 593874678e45
children
line wrap: on
line source

#!/usr/bin/tclsh
#
# NOTICE! Change above path to correct tclsh binary path!
#
##########################################################################
#
# Improved weather data fetcher for Jedah's "saa.tcl"
# by Matti 'ccr' Hamalainen <ccr@tnsp.org>
# (C) Copyright 2009-2011 Tecnic Software productions (TNSP)
#
# This script is freely distributable under GNU GPL (version 2) license.
#
##########################################################################

# Path and filename of the data file, MUST BE SAME as in "saa.tcl"
set datafile "/home/niinuska/bot/saa.data"

# Use a HTTP proxy? (1 = yes, 0 = no)
set use_proxy 0

# HTTP proxy host address and port (only needed if use_proxy = 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"
  }
}