コード例 #1
0
ファイル: Weather.cs プロジェクト: StarlitGhost/MoronBot
        public override List <IRCResponse> GetResponse(BotMessage message)
        {
            if (!Regex.IsMatch(message.Command, @"^weather$", RegexOptions.IgnoreCase))
            {
                return(null);
            }
            string query = string.Empty;

            if (message.ParameterList.Count == 0)
            {
                URL.WebPage page = URL.FetchURL("http://www.tsukiakariusagi.net/chatmaplookup.php?nick=" + message.User.Name);
                if (page.Page == ",")
                {
                    return new List <IRCResponse>()
                           {
                               new IRCResponse(ResponseType.Say, "You didn't give a location, and you're not registered on the !chatmap", message.ReplyTo)
                           }
                }
                ;
                else
                {
                    query = page.Page;
                }
            }
            else
            {
                query = HttpUtility.UrlEncode(message.Parameters);
            }

            try
            {
                Stream responseStream = URL.SendToServer("http://free.worldweatheronline.com/feed/weather.ashx?format=json&num_of_days=0&key=f31c15f2f7142209122801&q=" + query);
                string jsonResponse   = URL.ReceiveFromServer(responseStream);

                WeatherData      data     = JsonConvert.DeserializeObject <WeatherRoot>(jsonResponse).data;
                Request          location = data.request[0];
                CurrentCondition weather  = data.current_condition[0];

                return(new List <IRCResponse>()
                {
                    new IRCResponse(
                        ResponseType.Say,
                        String.Format("{0}: {1} | {2} | {3}ºC ({4}ºF) | Humidity: {5}% | Wind: {6}kph ({7}mph) {8}",
                                      location.type,
                                      location.query,
                                      String.Join(", ", weather.weatherDesc),
                                      weather.temp_C,
                                      weather.temp_F,
                                      weather.humidity,
                                      weather.windspeedKmph,
                                      weather.windspeedMiles,
                                      weather.winddir16Point),
                        message.ReplyTo)
                });
            }
            catch (System.Exception ex)
            {
                Logger.Write(ex.Message, Settings.Instance.ErrorFile);
                return(null);
            }
        }
コード例 #2
0
        public override List <IRCResponse> GetResponse(BotMessage message)
        {
            if (!Regex.IsMatch(message.Command, "^(np|nowplaying)$", RegexOptions.IgnoreCase))
            {
                return(null);
            }

            string lastfmName = "";

            if (message.ParameterList.Count > 0)
            {
                if (AccountMap.ContainsKey(message.ParameterList[0].ToUpper()))
                {
                    lastfmName = AccountMap[message.ParameterList[0].ToUpper()];
                }
                else
                {
                    lastfmName = message.ParameterList[0];
                }
            }
            else
            {
                if (AccountMap.ContainsKey(message.User.Name.ToUpper()))
                {
                    lastfmName = AccountMap[message.User.Name.ToUpper()];
                }
                else
                {
                    lastfmName = message.User.Name;
                }
            }

            URL.WebPage recentFeed = new URL.WebPage();

            try
            {
                recentFeed = URL.FetchURL("http://ws.audioscrobbler.com/1.0/user/" + lastfmName + "/recenttracks.rss");
            }
            catch (System.Net.WebException ex)
            {
                Logger.Write(ex.ToString(), Settings.Instance.ErrorFile);
                return(new List <IRCResponse>()
                {
                    new IRCResponse(ResponseType.Say, "User \"" + lastfmName + "\" not found on LastFM", message.ReplyTo)
                });
            }

            Match track = Regex.Match(recentFeed.Page, @"<item>\s*?<title>(?<band>.+?)–(?<song>.+?)</title>\s*?<link>(?<link>.+?)</link>", RegexOptions.IgnoreCase | RegexOptions.Multiline);

            if (track.Success)
            {
                string band = track.Groups["band"].Value;
                string song = track.Groups["song"].Value;

                string songMessage = "\"" + song.Trim() + "\" by " + band.Trim();

                songMessage += " (" + /*ChannelList.EvadeChannelLinkBlock(message, URL.Shorten(*/ track.Groups["link"].Value /*))*/ + ")";

                return(new List <IRCResponse>()
                {
                    new IRCResponse(ResponseType.Say, songMessage, message.ReplyTo)
                });
            }
            else
            {
                return(new List <IRCResponse>()
                {
                    new IRCResponse(ResponseType.Say, "User \"" + lastfmName + "\" exists on LastFM, but hasn't scrobbled any music to it", message.ReplyTo)
                });
            }
        }