<commit_before>"""Weather module for ppbot.
@package ppbot

Given a zipcode, will lookup the associated weather for that zipcode.
Will be using wunderground.com's API.

@syntax w <zipcode>

"""
import urllib2
import string
from xml.dom.minidom import parseString

from modules import *

class Weather(Module):
    
    def __init__(self, server):
        """Constructor"""
        
        Module.__init__(self, server)
        
        # url for wunderground's api, forecast url
        self.wurl = 'http://www.google.com/ig/api?weather=%s'
        
    def _register_events(self):
        """Register module commands."""
        
        self.add_command('w')
    
    def w(self, event):
        """Action to react/respond to user calls."""
        
        if self.num_args == 1:
            # need to fetch the weather and parse it
            zipcode = event['args'][0]
            try:
                weather = self.get_weather(zipcode)
                # stylize the message output
                message1 = "%(city)s (%(zipcode)s) - Currently: %(temp_f)sF (%(temp_c)sC) - Conditions: %(condition)s, %(humidity)s, %(wind)s" % (weather)
                message2 = "Today (%(day)s) - High: %(high)sF, Low: %(low)sF - %(condition)s" % weather['forecast'][0]
                message3 = "Tomorrow (%(day)s) - High: %(high)sF, Low: %(low)sF - %(condition)s" % weather['forecast'][1]
                # send the messages
                self.msg(event['target'], message1)
                self.msg(event['target'], message2)
                self.msg(event['target'], message3)
            except:
                self.msg(event['target'], 'Could not get weather data for "%s"' % zipcode)
                print "something fucked connecting to weather api. : %s" % zipcode
        else:
            self.syntax_message(event['nick'], '.w <zipcode>')
        
        
    def get_weather(self, zipcode):
        """Connects to google's secret weather API and parses the receiving XML for the weather."""
        
        # make the parser, and send the xml to be parsed
        xml = urllib2.urlopen(self.wurl % zipcode).read()
        xml = string.replace(xml, '<?xml version="1.0"?>', '')
        dom = parseString(xml)
        weather = {}
        forecast = []

        forecast_information = dom.getElementsByTagName('forecast_information')[0]
        current_conditions = dom.getElementsByTagName('current_conditions')[0]
        forecast_conditions = dom.getElementsByTagName('forecast_conditions')

        weather['city'] = forecast_information.getElementsByTagName('city')[0].getAttribute('data')
        weather['zipcode'] = forecast_information.getElementsByTagName('postal_code')[0].getAttribute('data')
        weather['condition'] = current_conditions.getElementsByTagName('condition')[0].getAttribute('data')
        weather['wind'] = current_conditions.getElementsByTagName('wind_condition')[0].getAttribute('data')
        weather['humidity'] = current_conditions.getElementsByTagName('humidity')[0].getAttribute('data')
        weather['temp_f'] = current_conditions.getElementsByTagName('temp_f')[0].getAttribute('data')
        weather['temp_c'] = current_conditions.getElementsByTagName('temp_c')[0].getAttribute('data')

        for x in forecast_conditions:
            fc_temp = {}
            fc_temp['day'] = x.getElementsByTagName('day_of_week')[0].getAttribute('data') 
            fc_temp['low'] = x.getElementsByTagName('low')[0].getAttribute('data') 
            fc_temp['high'] = x.getElementsByTagName('high')[0].getAttribute('data') 
            fc_temp['condition'] = x.getElementsByTagName('condition')[0].getAttribute('data') 
            forecast.append(fc_temp)<commit_msg>Removing the limit of 1 param for weather bot, can now do city, state lookups<commit_after>"""Weather module for ppbot.
@package ppbot

Given a zipcode, will lookup the associated weather for that zipcode.
Will be using wunderground.com's API.

@syntax w <zipcode>

"""
import urllib2
import string
from xml.dom.minidom import parseString

from modules import *

class Weather(Module):
    
    def __init__(self, server):
        """Constructor"""
        
        Module.__init__(self, server)
        
        # url for wunderground's api, forecast url
        self.wurl = 'http://www.google.com/ig/api?weather=%s'
        
    def _register_events(self):
        """Register module commands."""
        
        self.add_command('w')
    
    def w(self, event):
        """Action to react/respond to user calls."""
        
        # need to fetch the weather and parse it
        zipcode = ''.join(event['args'])
        try:
            weather = self.get_weather(zipcode)
            # stylize the message output
            message1 = "%(city)s (%(zipcode)s) - Currently: %(temp_f)sF (%(temp_c)sC) - Conditions: %(condition)s, %(humidity)s, %(wind)s" % (weather)
            message2 = "Today (%(day)s) - High: %(high)sF, Low: %(low)sF - %(condition)s" % weather['forecast'][0]
            message3 = "Tomorrow (%(day)s) - High: %(high)sF, Low: %(low)sF - %(condition)s" % weather['forecast'][1]
            # send the messages
            self.msg(event['target'], message1)
            self.msg(event['target'], message2)
            self.msg(event['target'], message3)
        except:
            self.msg(event['target'], 'Could not get weather data for "%s"' % zipcode)
            print "something fucked connecting to weather api. : %s" % zipcode
            #self.syntax_message(event['nick'], '.w <zipcode>')
        
        
    def get_weather(self, zipcode):
        """Connects to google's secret weather API and parses the receiving XML for the weather."""
        
        # make the parser, and send the xml to be parsed
        xml = urllib2.urlopen(self.wurl % zipcode).read()
        xml = string.replace(xml, '<?xml version="1.0"?>', '')
        dom = parseString(xml)
        weather = {}
        forecast = []

        forecast_information = dom.getElementsByTagName('forecast_information')[0]
        current_conditions = dom.getElementsByTagName('current_conditions')[0]
        forecast_conditions = dom.getElementsByTagName('forecast_conditions')

        weather['city'] = forecast_information.getElementsByTagName('city')[0].getAttribute('data')
        weather['zipcode'] = forecast_information.getElementsByTagName('postal_code')[0].getAttribute('data')
        weather['condition'] = current_conditions.getElementsByTagName('condition')[0].getAttribute('data')
        weather['wind'] = current_conditions.getElementsByTagName('wind_condition')[0].getAttribute('data')
        weather['humidity'] = current_conditions.getElementsByTagName('humidity')[0].getAttribute('data')
        weather['temp_f'] = current_conditions.getElementsByTagName('temp_f')[0].getAttribute('data')
        weather['temp_c'] = current_conditions.getElementsByTagName('temp_c')[0].getAttribute('data')

        for x in forecast_conditions:
            fc_temp = {}
            fc_temp['day'] = x.getElementsByTagName('day_of_week')[0].getAttribute('data') 
            fc_temp['low'] = x.getElementsByTagName('low')[0].getAttribute('data') 
            fc_temp['high'] = x.getElementsByTagName('high')[0].getAttribute('data') 
            fc_temp['condition'] = x.getElementsByTagName('condition')[0].getAttribute('data') 
            forecast.append(fc_temp)
{'fraction_non_alphanumeric': 0.14140730717185385, 'fraction_numerical': 0.007171853856562923, 'mean_word_length': 2.9481837606837606, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 2, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2439952', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2154, 'n_words': 449}