saa.py

TKa 21.06.04 08:54

XChat:iin pieni python-skripti joka hakee säätiedot ja tulostaa ne kanavalle

 Tekstiversio  Arvo: 0 (0 ääntä)  Äänestä: +  -
import xchat
import urllib
import string

__module_name__ = "saa"
__module_version__ = "0.1"
__module_description__ = "Weather thingie"

def saa_cb( word, word_eol, userdata ):
        # A word about the url: You need your locations
        # ICAO-code and replace EFKE.TXT with suitable
        # filename.
        handle = urllib.urlopen( 'http://weather.noaa.gov/pub/data/observations/metar/decoded/EFKE.TXT' )
        data = handle.readlines()
        handle.close()
        aika = data[1]
        dict = {}
        for item in data[2:]:
                i = item.split( ':' )
                if len( i ) > 2:
                        i[1] = string.join( i[1:], ':' )
                        i.pop()
                dict[ i[0] ] = i[1]
        try:
                temp_tmp = dict[ "Temperature" ]
                temp = temp_tmp[temp_tmp.index( '(' )+1:temp_tmp.index( 'C' )]
        except KeyError:
                temp = "N/A"
        try:
                sky = dict[ "Sky conditions" ]
        except KeyError:
                sky = "N/A"
        out1 = aika.strip()
        output = 'Temperature: ' + temp.strip() + '°C Sky conditions: ' + sky.strip()
        conteksti = xchat.find_context( )
        conteksti.command( "say " + out1 )
        conteksti.command( "say " + output )
        return xchat.EAT_ALL

xchat.hook_command( "saa", saa_cb )