Software Freedom Law Center

Changeset 118

Show
Ignore:
Timestamp:
05/31/08 16:39:51 (6 months ago)
Author:
landon
Message:

Basic conversation script added

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/antimatter/luke/myplugs/testconvo.py

    r115 r118  
    1 # -*- coding: utf-8 -*- 
    2 """ show weather based on Google's weather API """ 
     1""" just a test conversation script """ 
    32 
    4 __copyright__ = 'this file is in the public domain' 
    5  
    6 from gozerbot.generic import geturl, waitforuser 
     3from gozerbot.generic import waitforuser 
    74from gozerbot.commands import cmnds 
    8 from gozerbot.datadir import datadir 
    9 from gozerbot.persist import Persist 
    105from gozerbot.users import users 
    116from gozerbot.plughelp import plughelp 
    12 from gozerbot.examples import examples 
    137import time 
    14 from xml.dom import minidom 
    15 from urllib import urlencode 
    168import re 
    179 
    18 plughelp.add('weather', 'show weather by zipcode/city name') 
     10plughelp.add('convo', 'Test conversation script') 
    1911 
    20 userweather = Persist(datadir + '/weather') 
     12def handle_convo(bot, ievent): 
     13    name = ievent.nick 
     14    if (ievent.rest == "Hello?"): 
     15        ievent.reply("Hello, "+name+", how are you?") 
     16        reply = waitforuser(bot,ievent.userhost) 
     17        ievent.reply(name+", you said that you are "+reply.txt+", is this true?") 
     18        reply = waitforuser(bot,ievent.userhost) 
     19        if (reply.txt == "Yes"): 
     20            ievent.reply("That's great!") 
     21        elif (reply.txt == "No"): 
     22            ievent.reply("You lied to me!") 
     23    elif (ievent.rest == "Goodbye."): 
     24        ievent.reply("Goodbye, "+name+"!") 
    2125 
    22 if not userweather.data: 
    23     userweather.data = {} 
    24  
    25 def handle_weather(bot, ievent): 
    26     """ show weather using Google's weather API """ 
    27     name = ievent.nick 
    28     if ievent.rest: 
    29         loc = ievent.rest 
    30     else: 
    31         if not userweather.data.has_key(name): 
    32             ievent.reply("You don't have a location set. What do you want it set to?") 
    33             reply = waitforuser(bot, ievent.userhost) 
    34             if not reply: 
    35                 ievent.reply("No response.") 
    36                 return 
    37             loc = reply.txt 
    38         else: 
    39             loc = userweather.data[name] 
    40     query = urlencode({'weather':loc}) 
    41     weathertxt = geturl('http://www.google.ca/ig/api?%s' % query) 
    42     resultstr = "" 
    43     if len(weathertxt) > 135: 
    44         gweather = minidom.parseString(weathertxt) 
    45         gweather = gweather.getElementsByTagName('weather')[0] 
    46         if ievent.command == "weather": 
    47             info = gweather.getElementsByTagName('forecast_information')[0] 
    48             city = info.getElementsByTagName('city')[0].attributes["data"].value 
    49             zip = info.getElementsByTagName('postal_code')[0].attributes["data"].value 
    50             time = info.getElementsByTagName('current_date_time')[0].attributes["data"].value 
    51  
    52             weather = gweather.getElementsByTagName('current_conditions')[0] 
    53             condition = weather.getElementsByTagName('condition')[0].attributes["data"].value 
    54             temp_f = weather.getElementsByTagName('temp_f')[0].attributes["data"].value 
    55             temp_c = weather.getElementsByTagName('temp_c')[0].attributes["data"].value 
    56             humidity = weather.getElementsByTagName('humidity')[0].attributes["data"].value 
    57             wind = weather.getElementsByTagName('wind_condition')[0].attributes["data"].value 
    58             speed = re.findall(r"([0-9]{1,3})",wind) 
    59             if (speed): 
    60                 wind += " (" + str(round(int(speed[0])*1.609344)) +"km/h)" 
    61             else: 
    62                 wind = "No wind" 
    63             resultstr = "As of %s, %s (%s) has a temperature of %sC/%sF with %s. %s. Oh, and it's %s." % (time, city, zip, temp_c, temp_f, humidity, wind, condition) 
    64         elif ievent.command == "forecast": 
    65             forecasts = gweather.getElementsByTagName('forecast_conditions') 
    66             for forecast in forecasts: 
    67                 condition = forecast.getElementsByTagName('condition')[0].attributes["data"].value 
    68                 low_f = forecast.getElementsByTagName('low')[0].attributes["data"].value 
    69                 high_f = forecast.getElementsByTagName('high')[0].attributes["data"].value 
    70                 day = forecast.getElementsByTagName('day_of_week')[0].attributes["data"].value 
    71                 low_c = round((int(low_f) - 32) * 5.0 / 9.0) 
    72                 high_c = round((int(high_f) - 32) * 5.0 / 9.0) 
    73                 resultstr += "[ \002%s\002 - Hi: %sF°/%sC° Lo: %sF°/%sC° %s ]" % (day, high_f, high_c, low_f, low_c, condition) 
    74     if not resultstr: 
    75         ievent.reply('%s not found!' % loc) 
    76         return 
    77     else: 
    78         ievent.reply(resultstr) 
    79         userweather.data[name] = loc 
    80         userweather.save() 
    81  
    82 cmnds.add('weather', handle_weather, 'USER') 
    83 cmnds.add('forecast', handle_weather, 'USER') 
    84 examples.add('weather', 'get weather for <ZIP/CITY, STATE>', 'weather EHAM') 
     26cmnds.add('convo', handle_convo, 'USER') 

SFLC Main Page

[frdm] Support SFLC