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) { 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.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 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 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) { 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) { 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) { 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) { 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) { 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); }
static void Main(string[] args) { for (char c = 'a'; c <= 'z'; ++c) { alph.Add(c); } alph.AddRange(Enumerable.Range(1, 9).Select(x => x.ToString()[0]).ToArray()); HtmlReaderManager hrm = new HtmlReaderManager(); LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(200); TaskFactory factory = new TaskFactory(lcts); var tasks = new List<Task>(); int counter = 1; foreach (string url in GetNextUrl()) { Options opt = new Options(); opt.Url = url; opt.counter = counter; tasks.Add(factory.StartNew((opts) => { bool isDownloaded = false; int tryCount = 0; do { Options options = (Options) opts; string targetUrl = "http://prntscr.com/1" + options.Url; try { hrm.Get(targetUrl); string html = hrm.Html; HtmlAgilityPack.HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); string imgUrl = doc.DocumentNode.SelectSingleNode("//meta[@name='twitter:image:src']").Attributes["content"].Value; if (!string.IsNullOrEmpty(imgUrl)) { ImageDownloader.DownloadRemoteImageFile(imgUrl, "images/" + Guid.NewGuid() + ".jpeg"); Console.WriteLine(options.counter + ". Скачиваем " + imgUrl); } else Console.WriteLine(options.counter + ". " + imgUrl + " пуст"); break; } catch (Exception ex) { tryCount++; if (ex.ToString().StartsWith("System.Net.WebException: The remote server returned an error: (503) Server Unavailable.") && tryCount<20) { Random s = new Random(); int randomVal = s.Next(1000, 10000); Thread.Sleep(randomVal); Console.WriteLine(options.counter + ". ждём " + targetUrl +" ("+tryCount+" раз)"); } else { Console.WriteLine(options.counter + ". ошибка " + targetUrl); isDownloaded = true; } } } while (!isDownloaded); }, opt, TaskCreationOptions.LongRunning)); if (counter%200 == 0) Task.WaitAll(tasks.ToArray()); tasks.Clear(); counter++; } Console.ReadLine(); }
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); }
static void Main(string[] args) { for (char c = 'a'; c <= 'z'; ++c) { alph.Add(c); } alph.AddRange(Enumerable.Range(1, 9).Select(x => x.ToString()[0]).ToArray()); HtmlReaderManager hrm = new HtmlReaderManager(); LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(200); TaskFactory factory = new TaskFactory(lcts); var tasks = new List <Task>(); int counter = 1; foreach (string url in GetNextUrl()) { Options opt = new Options(); opt.Url = url; opt.counter = counter; tasks.Add(factory.StartNew((opts) => { bool isDownloaded = false; int tryCount = 0; do { Options options = (Options)opts; string targetUrl = "http://prntscr.com/1" + options.Url; try { hrm.Get(targetUrl); string html = hrm.Html; HtmlAgilityPack.HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); string imgUrl = doc.DocumentNode.SelectSingleNode("//meta[@name='twitter:image:src']").Attributes["content"].Value; if (!string.IsNullOrEmpty(imgUrl)) { ImageDownloader.DownloadRemoteImageFile(imgUrl, "images/" + Guid.NewGuid() + ".jpeg"); Console.WriteLine(options.counter + ". Скачиваем " + imgUrl); } else { Console.WriteLine(options.counter + ". " + imgUrl + " пуст"); } break; } catch (Exception ex) { tryCount++; if (ex.ToString().StartsWith("System.Net.WebException: The remote server returned an error: (503) Server Unavailable.") && tryCount < 20) { Random s = new Random(); int randomVal = s.Next(1000, 10000); Thread.Sleep(randomVal); Console.WriteLine(options.counter + ". ждём " + targetUrl + " (" + tryCount + " раз)"); } else { Console.WriteLine(options.counter + ". ошибка " + targetUrl); isDownloaded = true; } } } while (!isDownloaded); }, opt, TaskCreationOptions.LongRunning)); if (counter % 200 == 0) { Task.WaitAll(tasks.ToArray()); } tasks.Clear(); counter++; } Console.ReadLine(); }