private async Task UpdateParser(Parser obj) { try { foreach (var u in obj.Newslist) { if ((TimeChron.GetRealTime() - new DateTime(long.Parse(u.CachedOnStr))) .Hours > App.Get.Config.CacheAlive) { await NewsItemDetailed.Process(u); } } } catch (Exception) { // ignored } }
public static Task PageThread(NewsItem item) { return(NewsItemDetailed.Process(item)); }
public async Task <string> SearchNewsAsync() { var Query = Info.Query; try { if (Query.HasKey("query")) { string qpar = Query["query"]; int count; var news = new List <NewsItem>(); var tr = new List <Task>(); try { if (qpar.Contains(',')) { var t = qpar.Split(','); count = int.Parse(t[0]); if (count > 1000) { throw new FormatException("Freak: server might fall"); } qpar = t[1]; } else { throw new FormatException("Expected count parameter. Search results may be very huge"); } bool virg = true; var rm = await RequestAllocator.Instance.UsingPool(new Request("http://nuwm.edu.ua/search?text=" + qpar.Replace(' ', '+'))); HtmlDocument doc = new HtmlDocument(); doc.Load(await rm.Content.ReadAsStreamAsync()); var wnode = doc.DocumentNode.Descendants().Where(x => x.Name == "div" && x.HasClass("news") && x.HasClass("search") && x.GetAttributeValue("role", "") == "group"); var nodes = wnode as HtmlNode[] ?? wnode.ToArray(); if (!nodes.Any()) { throw new InvalidDataException("Not found"); } var node = nodes.First(); foreach (var a in node.Elements("article")) { var btnf = a.Descendants("a").Where(x => x.HasClass("btn") && x.HasClass("s2")); var htmlNodes = btnf as HtmlNode[] ?? btnf.ToArray(); if (htmlNodes.Any()) { var link = htmlNodes.First().GetAttributeValue("href", ""); if (link.Contains("/news")) { bool found = false; foreach (var i in ParserPool.Current.POOL.Values) { var t = i.Newslist.Where(x => x.Url == link).ToArray(); if (t.Length == 1) { found = true; news.Add(t.First()); break; } } if (pool.InstantCache != null) { var inst = pool.InstantCache.Where(x => x.Url == link).ToArray(); if (inst.Length == 1) { news.Add(inst.First()); found = true; } } if (!found) { var u = new NewsItem { Excerpt = a.Descendants("p").First().InnerText, Title = a.Descendants("a").First(x => x.HasClass("name")).InnerText, Url = link }; tr.Add(Task.Run(() => NewsItemDetailed.Process(u))); news.Add(u); if (pool.InstantCache == null) { pool.InstantCache = new List <NewsItem>(); } virg = false; pool.InstantCache.Add(u); } } } if (news.Count == count) { break; } } await Task.WhenAll(tr); if (news.Count == 0) { return(JsonConvert.SerializeObject(ResponseTyper(new InvalidDataException("Not Found")))); } return(JsonConvert.SerializeObject(ResponseTyper(null, news, (virg ? InstantState.FromCache : InstantState.Success)))); } catch (Exception ex) { return(JsonConvert.SerializeObject(ResponseTyper(ex, news))); } } } catch (Exception ex) { return(JsonConvert.SerializeObject(ResponseTyper(ex))); } return(JsonConvert.SerializeObject(ResponseTyper(new InvalidOperationException("InvalidKey: query expected")))); }