<commit_before>﻿using System.IO;
using System.Collections.Generic;
using System.Threading;
using IvionSoft;
using IvionWebSoft;
// Using directives for plugin use.
using MeidoCommon;
using System.ComponentModel.Composition;


[Export(typeof(IMeidoHook))]
public class IrcWeather : IMeidoHook
{
    public string Name
    {
        get { return "WeatherUnderground"; }
    }
    public string Version
    {
        get { return "0.54"; }
    }

    public Dictionary<string,string> Help
    {
        get 
        {
            return new Dictionary<string, string>()
            {
                {"w", "w [location] - Reports weather conditions at location. (Powered by WeatherUnderground)"},
                {"W", "W <location> - Sets default location for your nick."}
            };
        }
    }

    readonly IIrcComm irc;
    readonly ILog log;

    readonly object _locker = new object();
    WeatherUnderground weather;

    readonly string storagePath;
    readonly Storage<string> defaultLocations;

    const string weatherError = "Weather querying disabled due to missing API key.";


    public void Stop()
    {}


    [ImportingConstructor]
    public IrcWeather(IIrcComm irc, IMeidoComm meido)
    {
        this.irc = irc;
        log = meido.CreateLogger(this);

        // Setting up configuration.
        var xmlConf = new XmlConfig2<Config>(
            Config.DefaultConfig(),
            (xml) => new Config(xml),
            log,
            Configure
        );
        meido.LoadAndWatchConfig("WeatherUnderground.xml", xmlConf);

        // Setting up locations database/dict.
        storagePath = meido.DataPathTo("_weatherunderground.xml");
        try
        {
            defaultLocations = Storage<string>.Deserialize(storagePath);
        }
        catch (FileNotFoundException)
        {
            defaultLocations = new Storage<string>();
        }

        meido.RegisterTrigger("w", WeatherSearch);
        meido.RegisterTrigger("W", SetWeatherLocation);
    }

    void Configure(Config conf)
    {
        lock (_locker)
        {
            if (!string.IsNullOrWhiteSpace(conf.WeatherUndergroundApiKey))
            {
                weather = new WeatherUnderground(conf.WeatherUndergroundApiKey);
            }
            else
            {
                log.Error(weatherError);
                weather = null;
            }
        }
    }


    // --- Weather Search, 'w' trigger ---

    void WeatherSearch(IIrcMessage e)
    {
        var location = GetLocation(e);

        if (!string.IsNullOrWhiteSpace(location))
        {
            ThreadPool.QueueUserWorkItem( (state) => WeatherSearch(e, location) );
        }
        else
        {
            e.Reply("Either specify a location or set your default location with the 'W' trigger. " +
                    "(That is a upper case W)");
        }
    }

    void WeatherSearch(IIrcMessage e, string location)
    {
        WeatherConditions cond;
        if (TryGetConditions(location, out cond))
        {
            if (cond.Success)
                irc.SendMessage(e.ReturnTo, Format(cond));
            else
                e.Reply(cond.Exception.Message);
        }
        else
            e.Reply(weatherError);
    }


    // --- Helper functions for WeatherSearch ---

    string GetLocation(IIrcMessage e)
    {
        string location;<commit_msg>WeatherUnderground: Rework to new Trigger<commit_after>﻿using System.IO;
using System.Collections.Generic;
using System.Threading;
using IvionSoft;
using IvionWebSoft;
// Using directives for plugin use.
using MeidoCommon;
using System.ComponentModel.Composition;


[Export(typeof(IMeidoHook))]
public class IrcWeather : IMeidoHook
{
    public string Name
    {
        get { return "WeatherUnderground"; }
    }
    public string Version
    {
        get { return "0.54"; }
    }

    public Dictionary<string,string> Help
    {
        get 
        {
            return new Dictionary<string, string>()
            {
                {"w", "w [location] - Reports weather conditions at location. (Powered by WeatherUnderground)"},
                {"W", "W <location> - Sets default location for your nick."}
            };
        }
    }

    public IEnumerable<Trigger> Triggers { get; private set; }


    readonly IIrcComm irc;
    readonly ILog log;

    readonly object _locker = new object();
    WeatherUnderground weather;

    readonly string storagePath;
    readonly Storage<string> defaultLocations;

    const string weatherError = "Weather querying disabled due to missing API key.";


    public void Stop()
    {}


    [ImportingConstructor]
    public IrcWeather(IIrcComm irc, IMeidoComm meido)
    {
        this.irc = irc;
        log = meido.CreateLogger(this);

        // Setting up configuration.
        var xmlConf = new XmlConfig2<Config>(
            Config.DefaultConfig(),
            (xml) => new Config(xml),
            log,
            Configure
        );
        meido.LoadAndWatchConfig("WeatherUnderground.xml", xmlConf);

        // Setting up locations database/dict.
        storagePath = meido.DataPathTo("_weatherunderground.xml");
        try
        {
            defaultLocations = Storage<string>.Deserialize(storagePath);
        }
        catch (FileNotFoundException)
        {
            defaultLocations = new Storage<string>();
        }

        Triggers = new Trigger[] {
            new Trigger("w", WeatherSearch),
            new Trigger("W", SetWeatherLocation)
        };
    }

    void Configure(Config conf)
    {
        lock (_locker)
        {
            if (!string.IsNullOrWhiteSpace(conf.WeatherUndergroundApiKey))
            {
                weather = new WeatherUnderground(conf.WeatherUndergroundApiKey);
            }
            else
            {
                log.Error(weatherError);
                weather = null;
            }
        }
    }


    // --- Weather Search, 'w' trigger ---

    void WeatherSearch(IIrcMessage e)
    {
        var location = GetLocation(e);

        if (!string.IsNullOrWhiteSpace(location))
        {
            WeatherSearch(e, location);
        }
        else
        {
            e.Reply("Either specify a location or set your default location with the 'W' trigger. " +
                    "(That is a upper case W)");
        }
    }

    void WeatherSearch(IIrcMessage e, string location)
    {
        WeatherConditions cond;
        if (TryGetConditions(location, out cond))
        {
            if (cond.Success)
                irc.SendMessage(e.ReturnTo, Format(cond));
            else
                e.Reply(cond.Exception.Message);
        }
        else
            e.Reply(weatherError);
    }


    // --- Helper functions for WeatherSearch ---

    string GetLocation(IIrcMessage e)
    {
        string location;
{'fraction_non_alphanumeric': 0.0947152686283121, 'fraction_numerical': 0.0011711316059142146, 'mean_word_length': 1.8372093023255813, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '3375180', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1799, 'n_words': 448}