public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc) { string url = "http://know-that.ru/randomizer.php"; string result = string.Empty; HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Encoding = Encoding.GetEncoding(1251); hrm.Get(url); HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(hrm.Html); HtmlNodeCollection factList = htmlDoc.DocumentNode.SelectNodes(@"//./center/table"); if (factList != null) { Random random = new Random(); int showResult = random.Next(factList.Count-1); HtmlNodeCollection tds = factList[showResult].SelectNodes(".//./td");//.InnerText.Trim(); if ((tds != null) && (tds.Count >= 3)) { result = tds[2].InnerText.Trim(); } else { result = "Факт сломался... :("; } } else { result = "Факты кончились... :("; } sendMessageFunc(result,AnswerBehaviourType.Text); }
public void HandleMessage(string command, string args, object clientData, Action<string,AnswerBehaviourType> sendMessageFunc) { HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Encoding = Encoding.GetEncoding(1251); hrm.Get("http://f*****g-great-advice.ru/api/random"); var json = JsonConvert.DeserializeObject<dynamic>(hrm.Html); string advice = json.text.ToString(); sendMessageFunc(HttpUtility.HtmlDecode(advice.RemoveAllTags()), AnswerBehaviourType.Text); }
public static string ToShortUrl(this string url) { string shortenerPostUrl = "https://www.googleapis.com/urlshortener/v1/url"; string postData = string.Format(@"{{""longUrl"": ""{0}""}}", url); HtmlReaderManager hrm = new HtmlReaderManager(); hrm.ContentType = "application/json"; hrm.Post(shortenerPostUrl, postData); var response = JsonConvert.DeserializeObject<dynamic>(hrm.Html); return response.id; }
public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc) { HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Post("http://online-generators.ru/ajax.php", "processor=quotes"); var answerParts = hrm.Html.Split(new string[]{"##"},StringSplitOptions.RemoveEmptyEntries); string quote = answerParts[0]; string author = answerParts[1]; sendMessageFunc(string.Format("{0} ©{1}", quote, author), AnswerBehaviourType.Text); }
public static string ToShortUrl(this string url) { string shortenerPostUrl = "https://www.googleapis.com/urlshortener/v1/url"; string postData = string.Format(@"{{""longUrl"": ""{0}""}}", url); HtmlReaderManager hrm = new HtmlReaderManager(); hrm.ContentType = "application/json"; hrm.Post(shortenerPostUrl, postData); var response = JsonConvert.DeserializeObject <dynamic>(hrm.Html); return(response.id); }
public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc) { HtmlReaderManager hrm = new HtmlReaderManager(); Regex r = new Regex("[а-яА-ЯЁё]+"); bool isRu = r.IsMatch(args); string fromLang = isRu ? "ru" : "en"; string toLang = isRu ? "en" : "ru"; hrm.Get(string.Format("https://translate.google.ru/translate_a/single?client=t&sl={0}&tl={1}&hl=ru&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&dt=sw&ie=UTF-8&oe=UTF-8&oc=1&otf=2&ssel=0&tsel=0&q=", fromLang, toLang) + HttpUtility.UrlEncode(args)); string html = hrm.Html; string anwser = html.Substring(4, html.IndexOf(@""",""") - 4); sendMessageFunc(anwser, AnswerBehaviourType.Text); }
public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc) { if (args.StartsWith("help")) { sendMessageFunc(GetHelpText(),AnswerBehaviourType.Text); } else { string templateCode = @"using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester {{ public class Program {{ public static void Main(string[] args) {{ {0} }} public static void Out(object obj){{ Console.WriteLine(obj); }} }} }}"; HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Post("http://rextester.com/rundotnet/Run", string.Format("LanguageChoiceWrapper=1&EditorChoiceWrapper=1&Program={0}&Input=&ShowWarnings=false&Title=&SavedOutput=&WholeError=&WholeWarning=&StatsToSave=&CodeGuid=&IsInEditMode=False&IsLive=False" , HttpUtility.UrlEncode(string.Format(templateCode, args)))); var response = JsonConvert.DeserializeObject<dynamic>(hrm.Html); string toReturn = response.Result.ToString(); if (string.IsNullOrEmpty(toReturn)) { toReturn = response.Errors.ToString(); } if (!string.IsNullOrEmpty(toReturn)) { toReturn = toReturn.Replace(Environment.NewLine," ").Trim(); sendMessageFunc(toReturn.Length > 200 ? toReturn.Substring(0, 50) + "..." : toReturn, AnswerBehaviourType.Text); } } }
public void HandleMessage(string command, string args, object clientData, Action<string,AnswerBehaviourType> sendMessageFunc) { HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Get("http://pogoda.tut.by/"); string html = hrm.Html; HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); var tds = htmlDoc.DocumentNode.SelectNodes(@"//./td[@class='fcurrent-top' or @class='fcurrent-s']"); StringBuilder sb = new StringBuilder(); sb.Append("Погода в Минске :"); foreach (var td in tds) { sb.Append(td.SelectSingleNode(".//./div[@class='fcurrent-h']").InnerText + " "); sb.Append(td.SelectSingleNode(".//./span[@class='temp-i']").InnerText + " "); sb.Append(td.SelectSingleNode(".//./div[@class='fcurrent-descr']").InnerText + " "); sb.Append(Environment.NewLine); } sendMessageFunc(sb.ToString().Replace("°", "°"), AnswerBehaviourType.Text); }
public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc) { args = args.Replace("?"," ").Trim(); string answer = string.Empty; HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Get(string.Format("http://ru.wikipedia.org/w/index.php?search={0}",HttpUtility.UrlEncode(args))); string html = hrm.Html; HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); var content = htmlDoc.DocumentNode.SelectSingleNode(@"//./div[@id='mw-content-text']/p"); if (content != null && !content.InnerText.Contains("запросу не найдено")) { string h = content.InnerHtml; if (h.Contains("<b>")) { h = HttpUtility.HtmlDecode(h.Substring(h.IndexOf("<b>"))).Replace("\n", ""); htmlDoc.LoadHtml(h); h = Regex.Replace(htmlDoc.DocumentNode.InnerText, @"( ?\[.*?\])|( ?\(.*?\))", ""); if (h.Contains(".")) { h = h.Substring(0,h.IndexOf(".")); answer = h.Length > 700 ? h.Substring(0, 700) + "..." : h+"."; } } } if(string.IsNullOrEmpty(answer)) { answer = notFoundAnswers[r.Next(0,notFoundAnswers.Count)]; } else { answer += ". " + hrm.ResponseUri; } sendMessageFunc(answer, AnswerBehaviourType.Text); }
public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc) { if (!Images.Any()) { HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Get("http://boobs-selfshots.tumblr.com/page/" + r.Next(1, 600)); string html = hrm.Html; HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); var divs = htmlDoc.DocumentNode.SelectNodes(@"//./div[@class='photo_post']/a/img"); StringBuilder sb = new StringBuilder(); foreach (var div in divs) { Images.Add(div.Attributes["src"].Value); } } int rPos = r.Next(0, Images.Count); string url = Images[rPos]; Images.RemoveAt(rPos); sendMessageFunc(url, AnswerBehaviourType.Link); }
public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc) { StringBuilder result = new StringBuilder(); HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Encoding = Encoding.GetEncoding(1251); hrm.Get(@"http://www.goroskop.org/luna/049/segodnya.shtml"); string html = hrm.Html; HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); //title result.Append(htmlDoc.DocumentNode.SelectSingleNode(@"//./span[@class='tabl-header']").InnerText.Trim()); result.Append(Environment.NewLine); //info HtmlNodeCollection tblSpans = htmlDoc.DocumentNode.SelectNodes(@"//./span[@class='tabl-content']"); if ((tblSpans != null) && (tblSpans.Count >= 1)) { HtmlNodeCollection tds = tblSpans[1].SelectNodes(@".//./table[1]/tr/td"); if ((tds != null) && (tds.Count >= 2)) { result.Append(tds[1].InnerText);//.InnerHtml.Replace("<br>", Environment.NewLine).RemoveAllTags().Trim(); } } sendMessageFunc(result.ToString(), AnswerBehaviourType.Text); }
public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc) { if (!Jokes.Any()) { HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Encoding = Encoding.GetEncoding(1251); hrm.Get("http://bash.im/random"); string html = hrm.Html; HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); var divs = htmlDoc.DocumentNode.SelectNodes(@"//./div[@class='text']"); foreach (var div in divs) { Jokes.Add(HttpUtility.HtmlDecode(div.InnerHtml.Replace("<br>", Environment.NewLine))); } } int rPos = r.Next(0, Jokes.Count ); string joke = Jokes[rPos]; Jokes.RemoveAt(rPos); sendMessageFunc(joke, AnswerBehaviourType.Text); }
public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc) { if (!Jokes.Any()) { HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Get("http://ithappens.me/random"); string html = hrm.Html; HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); var divs = htmlDoc.DocumentNode.SelectNodes(@"//./div[@class='text']"); foreach (var div in divs) { Jokes.Add(div.InnerHtml.Replace("<p>", "").Replace("</p>", Environment.NewLine + Environment.NewLine).RemoveAllTags().Trim()); } } int rPos = r.Next(0, Jokes.Count ); string joke = Jokes[rPos]; Jokes.RemoveAt(rPos); sendMessageFunc(joke, AnswerBehaviourType.Text); }
public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc) { string[] arg = args.Split(' '); string url = "http://goroskop.open.by/pda/"; StringBuilder result = new StringBuilder(); string sign, term="today", category="ОБЩИЙ"; string help = "!гороскоп <знак зодиака> [общий/эротический/антигороскоп/бизнес/любовный/здоровья/кулинарный/мобильный] [сегодня/завтра/неделя/месяц/год]"; if (arg.Length == 0) { sendMessageFunc(help, AnswerBehaviourType.Text); return; } if ((arg.Length >= 1) && (Signs.TryGetValue(arg[0].ToUpper(), out sign))) { url += sign; } else { if (string.IsNullOrEmpty(arg[0])){ sendMessageFunc(help, AnswerBehaviourType.Text); }else{ sendMessageFunc(arg[0] + " - неверный знак зодиака", AnswerBehaviourType.Text); } return; } if (arg.Length == 2) { if (!Terms.TryGetValue(arg[1].ToUpper(), out term)) category = arg[1].ToUpper(); }else{ if (arg.Length >= 2) { category = arg[1].ToUpper(); } if ((arg.Length >= 3) && (!Terms.TryGetValue(arg[2].ToUpper(), out term))) { result.Append(arg[2].ToUpper() + " - неверно задан срок. Возможные варианты: [сегодня/завтра/неделя/месяц/год] "); result.Append(Environment.NewLine); } } url += term; HtmlReaderManager hrm = new HtmlReaderManager(); hrm.Get(url); string html = hrm.Html; HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); //title result.Append(arg[0].ToUpper()+" "); result.Append(htmlDoc.DocumentNode.SelectSingleNode(@"//./p[@class='horoTitleName']/span").InnerText); result.Append(Environment.NewLine); //select proper horoscope category HtmlNodeCollection horoCategories = htmlDoc.DocumentNode.SelectNodes(@"//./p[@class='categoryName' or @class='categoryText']"); if ((horoCategories != null)&&(horoCategories.Count >= 2)) { bool categoryFound = false; for (int i = 0; i < horoCategories.Count; i = i + 2) { if (horoCategories[i].InnerText.ToUpper() == category) { result.Append(category); result.Append(Environment.NewLine); result.Append(horoCategories[i + 1].InnerText); categoryFound = true; break; } } if (!categoryFound) { result.Append(category.ToUpper() + " - не удалось найти гороскоп такого типа."); result.Append(Environment.NewLine); result.Append(horoCategories[0].InnerText.ToUpper()); result.Append(Environment.NewLine); result.Append(horoCategories[1].InnerText); } } else { result.Append("На запрашиваемый Вами период гороскоп отсутствует"); } sendMessageFunc(result.ToString(), AnswerBehaviourType.Text); }