Пример #1
0
        public virtual ActionResult GetTorrents()
        {
            try
            {
                var pollModel = new PollTorrentsModel {
                    Sections = new List <Section>()
                };
                var client = CurrentSession.Client;

                var torrents = client.Torrents
                               .Select(t => new TorrentModel(t))
                               .ToList();

                torrents.ForEach(t =>
                {
                    pollModel.DownloadSpeedInBytes += t.DownloadSpeedInBytes;
                    pollModel.UploadSpeedInBytes   += t.UploadSpeedInBytes;
                });

                var grouped = torrents
                              .GroupBy(t => new { Status = (int)t.Status, Finished = t.Percentage == 100 });

                foreach (var grouping in grouped)
                {
                    var sect = new Section
                    {
                        Status   = grouping.Key.Status,
                        Finished = grouping.Key.Finished,
                        Id       = "section-" + grouping.Key.Status + "-" + grouping.Key.Finished,
                        Torrents = grouping.ToList().OrderBy(t => t.QueueOrder).ToList()
                    };

                    pollModel.Sections.Add(sect);
                }

                pollModel.Sections.Sort(new Comparison <Section>((s1, s2) =>
                {
                    bool d1 = DownloadingOrQueued((TorrentStatus)s1.Status, s1.Finished);
                    bool d2 = DownloadingOrQueued((TorrentStatus)s2.Status, s2.Finished);

                    if (d1 == d2)
                    {
                        return(s1.Id.CompareTo(s2.Id));
                    }
                    else
                    {
                        return(d2.CompareTo(d1));
                    }
                }));

                return(JsonContract(pollModel));
            }
            catch (Exception ex)
            {
                return(JsonContract(new TorrentResult {
                    Ok = false, ErrorMessage = ex.Message
                }));
            }
        }
Пример #2
0
        public virtual ActionResult GetTorrents()
        {
            try
            {
                var pollModel = new PollTorrentsModel { Sections = new List<Section>() };
                var client = CurrentSession.Client;

                var torrents = client.Torrents
                    .Select(t => new TorrentModel(t))
                    .ToList();

                torrents.ForEach(t =>
                {
                    pollModel.DownloadSpeedInBytes += t.DownloadSpeedInBytes;
                    pollModel.UploadSpeedInBytes += t.UploadSpeedInBytes;
                });

                var grouped = torrents
                    .GroupBy(t => new { Status = (int)t.Status, Finished = t.Percentage == 100 });

                foreach (var grouping in grouped)
                {
                    var sect = new Section
                    {
                        Status = grouping.Key.Status,
                        Finished = grouping.Key.Finished,
                        Id = "section-" + grouping.Key.Status + "-" + grouping.Key.Finished,
                        Torrents = grouping.ToList().OrderBy(t => t.QueueOrder).ToList()
                    };

                    pollModel.Sections.Add(sect);
                }

                pollModel.Sections.Sort(new Comparison<Section>((s1, s2) =>
                {
                    bool d1 = DownloadingOrQueued((TorrentStatus)s1.Status, s1.Finished);
                    bool d2 = DownloadingOrQueued((TorrentStatus)s2.Status, s2.Finished);

                    if (d1 == d2)
                        return s1.Id.CompareTo(s2.Id);
                    else
                        return d2.CompareTo(d1);
                }));

                return JsonContract(pollModel);
            }
            catch (Exception ex)
            {
                return JsonContract(new TorrentResult { Ok = false, ErrorMessage = ex.Message });
            }
        }