public async Task <Updates> Updates(ISession session) { try { Updates upds = new Updates(); upds.Items = new List <Episode>(); if (!UpdateHistory.IsLoaded) { UpdateHistory.Load(); } DaiSukiSession s = session as DaiSukiSession; if (s == null) { return new Updates { ErrorMessage = "Invalid Session", Status = ResponseStatus.InvalidArgument } } ; Shows sws = await Shows(s, false); if (sws.Status != ResponseStatus.Ok) { Updates k = new Updates(); sws.PropagateError(k); return(k); } int cnt = Convert.ToInt32(GetAuthSetting(DaiSukiPluginInfo.MaxFollowItems)); List <Show> avs = sws.Items.Take(cnt).ToList(); List <Task <Episodes> > ms = new List <Task <Episodes> >(); foreach (Show sh in avs) { ms.Add(Episodes(s, sh)); } string datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm"); if (ms.Count > 0) { while (ms.Count > 0) { int max = 5; if (ms.Count < 5) { max = ms.Count; } Task <Episodes>[] tsks = new Task <Episodes> [max]; for (int x = 0; x < max; x++) { tsks[x] = ms[0]; ms.Remove(ms[0]); } await Task.WhenAll(tsks); for (int x = max - 1; x >= 0; x--) { if (tsks[x].Result != null) { if (tsks[x].Result.Items.Count > 0) { Episode ep = tsks[x].Result.Items.OrderByDescending(a => a.Index).First(); ep.UniqueTag = DaiSukiPluginInfo.PluginName + "|" + ep.ShowName + "|" + ep.EpisodeAlpha; if (UpdateHistory.Exists(ep.UniqueTag)) { Episode c = JsonConvert.DeserializeObject <Episode>(UpdateHistory.Get(ep.UniqueTag)); upds.Items.Add(c); } else { ep.DateTime = datetime; UpdateHistory.Add(ep.UniqueTag, JsonConvert.SerializeObject(ep)); upds.Items.Add(ep); } } } } } UpdateHistory.Save(); } return(upds); } catch (Exception e) { return(new Updates { ErrorMessage = e.ToString(), Status = ResponseStatus.SystemError }); } }
private async Task <UpdateResponse> Updates(CrunchySession s, ShowType t) { int startpage = 0; UpdateResponse ret = new UpdateResponse(); bool end = false; do { string url = string.Format(LibSet[UpdateUrlS], ShowFromType(t), startpage); WebStream ws = await WebStream.Get(url, null, LibSet[UserAgentS], null, s.cookies.ToCookieCollection(), SocketTimeout, true, null, _info.ProxyFromGlobalRequirements(_global)); if (ws != null && ws.StatusCode == HttpStatusCode.OK) { if (!VerifyLogin(ws.Cookies)) { ws.Dispose(); SetLoginError(ret); return(ret); } StreamReader rd = new StreamReader(ws); string dta = rd.ReadToEnd(); rd.Dispose(); MatchCollection scol = updregex.Matches(dta); if (scol.Count == 0) { end = true; } foreach (Match m in scol) { string show = m.Groups["show"].Value; string image = m.Groups["image"].Value; string title = WebUtility.HtmlDecode(m.Groups["title"].Value); string ep = m.Groups["ep"].Value; Uri ur = new Uri("http://www.crunchyroll.com" + m.Groups["url"].Value); int a = ep.IndexOf("–", StringComparison.InvariantCulture); if (a >= 0) { ep = ep.Substring(0, a).Trim(); string tag = CrunchyPluginInfo.PluginName + "|" + show + "|" + ep; if (UpdateHistory.Exists(tag)) { Episode c = JsonConvert.DeserializeObject <Episode>(UpdateHistory.Get(tag)); ret.Found.Add(c); } else { Episode p = new Episode(); p.PluginMetadata.Add("Url", ur.ToString()); p.ShowName = title; p.ShowId = show; p.PluginName = CrunchyPluginInfo.PluginName; p.UniqueTag = tag; p.ImageUri = new Uri(image); p.Type = t; ret.NotFound.Add(p); } } } } else { ws?.Dispose(); SetWebError(ret); return(ret); } ws?.Dispose(); startpage++; } while (!end); return(ret); }