Exemplo n.º 1
0
        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
                });
            }
        }
Exemplo n.º 2
0
        public async Task <Updates> Updates(ISession session)
        {
            try
            {
                if (!UpdateHistory.IsLoaded)
                {
                    UpdateHistory.Load();
                }
                CrunchySession s = session as CrunchySession;
                if (s == null)
                {
                    return new Updates {
                               ErrorMessage = "Invalid Session", Status = ResponseStatus.InvalidArgument
                    }
                }
                ;

                Task <UpdateResponse> c1 = Updates(s, ShowType.Anime);
                Task <UpdateResponse> c2 = Updates(s, ShowType.Drama);
                Task <UpdateResponse> c3 = Updates(s, ShowType.Pop);
                await Task.WhenAll(c1, c2, c3);

                c3.Result.PropagateError(c2.Result);
                c2.Result.PropagateError(c1.Result);
                if (c1.Result.Status != ResponseStatus.Ok)
                {
                    Updates k = new Updates();

                    c1.Result.PropagateError(k);
                    return(k);
                }
                c1.Result.Found = c1.Result.Found.Concat(c2.Result.Found).Concat(c3.Result.Found).ToList();
                Episode[] nfnd =
                    c1.Result.NotFound.Concat(c2.Result.NotFound).Concat(c3.Result.NotFound).ToArray();
                Array.Reverse(nfnd);
                c1.Result.NotFound = nfnd.ToList();
                string datetime           = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                List <Task <Episode> > ms = new List <Task <Episode> >();
                foreach (Episode m in c1.Result.NotFound)
                {
                    ms.Add(GetEpisodeUpdate(s, m, datetime));
                }
                if (ms.Count > 0)
                {
                    while (ms.Count > 0)
                    {
                        int max = 5;
                        if (ms.Count < 5)
                        {
                            max = ms.Count;
                        }
                        Task <Episode>[] tsks = new Task <Episode> [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)
                            {
                                c1.Result.Found.Add(tsks[x].Result);
                            }
                        }
                    }
                    UpdateHistory.Save();
                }
                Updates n = new Updates();
                n.Items = c1.Result.Found.ToList();
                int cnt = 1;
                foreach (Episode epu in n.Items)
                {
                    epu.Index = cnt++;
                }
                n.Status       = ResponseStatus.Ok;
                n.ErrorMessage = "OK";
                return(n);
            }
            catch (Exception e)
            {
                return(new Updates {
                    ErrorMessage = e.ToString(), Status = ResponseStatus.SystemError
                });
            }
        }