<commit_before>import plugin
import urllib.request
import urllib.error
import urllib.parse
from urllib.parse import quote
import json

defaults = {
    'wunderground_key': '',
    'forecasts_per_message': 3,
    'days_ahead': 6,
}


def f_to_c(f):
    return (f - 32) * (5./9.)


class Plugin(plugin.Plugin):
    def register_commands(self):
        self.commands = [('weather current <<location>>', self.current),
                         ('weather forecast <<location>>', self.forecast),
                         ('weather today <<location>>', self.today)]

    def prepare(self):
        self.url = (
            "http://api.wunderground.com/api/"
            + self.conf['wunderground_key'] + "/%s/q/%s.json")

        self.current_msg = (
            "\x02%s\x02 \x03#|\x03 %s\u00b0F \x03#:\x03 %s\u00b0C \x03#|\x03 "
            "humidity \x03#:\x03 %s \x03#|\x03 wind \x03#:\x03 %s at %s mph")

        self.suggestions_msg = "\x02matches\x02 \x03#|\x03 %s"

        self.conditions_str = (
            '\x02%s\x02 \x03#:\x03 %s%s \x03#:\x03 high %s\u00b0F %s\u00b0C '
            '\x03#:\x03 low %s\u00b0F %s\u00b0C \x03#:\x03 %s%% humid \x03#:'
            '\x03 %s at %s mph')

        # self.conditions_str = "%s \x03#:\x03 %s"

    def loc_string(self, location):
        """
        Creates a human-readable location string based on a wunderground
        results.
        """

        if 'full' in location:
            return location['full']

        return ', '.join([
            location[size] for size in ['name', 'state', 'country']
            if location[size] != ''])

    def suggest(self, data):
        results = data['response']['results']
        suggestions = [self.loc_string(r) for r in results]
        suggestions_str = ' \x03#|\x03 '.join(suggestions)
        msg = self.suggestions_msg % suggestions_str
        return msg

    def current(self, message, args):
        """
        Fetches the current weather in `<location>`.

        $<comchar>w c stafford, uk

        >\x02Stafford, Staffordshire\x02 \x03#|\x03 61\u00b0F \x03#:\x03
        16\u00b0C \x03#|\x03 humidity \x03#:\x03 68% \x03#|\x03 wind
        \x03#:\x03 W at 9 mph
        """

        url = self.url % ('conditions', quote(args["location"]))
        print(url)
        data = json.loads(urllib.request.urlopen(url).read().decode('utf-8'))

        if 'results' in data['response']:
            msg = self.suggest(data)
        else:
            conditions = data['current_observation']

            city = self.loc_string(conditions['display_location'])

            tempf = conditions['temp_f']
            tempc = conditions['temp_c']
            humidity = conditions['relative_humidity']
            wind_mph = conditions['wind_mph']
            wind_dir = conditions['wind_dir'].lower()

            msg = self.current_msg % (
                city, tempf, tempc, humidity, wind_dir, wind_mph)

        self.irc.privmsg(message.source, msg)

    def forecast(self, message, args):
        """
        Fetches the weather forecast for `<location>`.

        $<comchar>w f nantwich and crewe

        >\x02Crewe, Cheshire East\x02 \x03#|\x03 \x02mon\x02 \x03#:\x03 high
        70\u00b0F 21\u00b0C \x03#:\x03 low 48\u00b0F 8\u00b0C \x03#:\x03 partly<commit_msg>Update the !weather docs to account for wolfram's existance.<commit_after>import plugin
import urllib.request
import urllib.error
import urllib.parse
from urllib.parse import quote
import json

defaults = {
    'wunderground_key': '',
    'forecasts_per_message': 3,
    'days_ahead': 6,
}


def f_to_c(f):
    return (f - 32) * (5./9.)


class Plugin(plugin.Plugin):
    def register_commands(self):
        self.commands = [('weather current <<location>>', self.current),
                         ('weather forecast <<location>>', self.forecast),
                         ('weather today <<location>>', self.today)]

    def prepare(self):
        self.url = (
            "http://api.wunderground.com/api/"
            + self.conf['wunderground_key'] + "/%s/q/%s.json")

        self.current_msg = (
            "\x02%s\x02 \x03#|\x03 %s\u00b0F \x03#:\x03 %s\u00b0C \x03#|\x03 "
            "humidity \x03#:\x03 %s \x03#|\x03 wind \x03#:\x03 %s at %s mph")

        self.suggestions_msg = "\x02matches\x02 \x03#|\x03 %s"

        self.conditions_str = (
            '\x02%s\x02 \x03#:\x03 %s%s \x03#:\x03 high %s\u00b0F %s\u00b0C '
            '\x03#:\x03 low %s\u00b0F %s\u00b0C \x03#:\x03 %s%% humid \x03#:'
            '\x03 %s at %s mph')

        # self.conditions_str = "%s \x03#:\x03 %s"

    def loc_string(self, location):
        """
        Creates a human-readable location string based on a wunderground
        results.
        """

        if 'full' in location:
            return location['full']

        return ', '.join([
            location[size] for size in ['name', 'state', 'country']
            if location[size] != ''])

    def suggest(self, data):
        results = data['response']['results']
        suggestions = [self.loc_string(r) for r in results]
        suggestions_str = ' \x03#|\x03 '.join(suggestions)
        msg = self.suggestions_msg % suggestions_str
        return msg

    def current(self, message, args):
        """
        Fetches the current weather in `<location>`.

        $<comchar>we c stafford, uk

        >\x02Stafford, Staffordshire\x02 \x03#|\x03 61\u00b0F \x03#:\x03
        16\u00b0C \x03#|\x03 humidity \x03#:\x03 68% \x03#|\x03 wind
        \x03#:\x03 W at 9 mph
        """

        url = self.url % ('conditions', quote(args["location"]))
        print(url)
        data = json.loads(urllib.request.urlopen(url).read().decode('utf-8'))

        if 'results' in data['response']:
            msg = self.suggest(data)
        else:
            conditions = data['current_observation']

            city = self.loc_string(conditions['display_location'])

            tempf = conditions['temp_f']
            tempc = conditions['temp_c']
            humidity = conditions['relative_humidity']
            wind_mph = conditions['wind_mph']
            wind_dir = conditions['wind_dir'].lower()

            msg = self.current_msg % (
                city, tempf, tempc, humidity, wind_dir, wind_mph)

        self.irc.privmsg(message.source, msg)

    def forecast(self, message, args):
        """
        Fetches the weather forecast for `<location>`.

        $<comchar>we f nantwich and crewe

        >\x02Crewe, Cheshire East\x02 \x03#|\x03 \x02mon\x02 \x03#:\x03 high
        70\u00b0F 21\u00b0C \x03#:\x03 low 48\u00b0F 8\u00b0C \x03#:\x03 partly
{'fraction_non_alphanumeric': 0.15035654680625093, 'fraction_numerical': 0.05370960400546199, 'mean_word_length': 2.11531190926276, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '852226', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2208, 'n_words': 493}