private IEnumerable <TorrentDescription> GetTorrents(string showId, string seasonNumber, string episodeNumber)
        {
            string       downloadsContent = Encoding.GetEncoding(1251).GetString(client.GetAsync(string.Format(DownloadsUrl, showId, seasonNumber, episodeNumber)).Result.Content.ReadAsByteArrayAsync().Result);
            HtmlDocument document         = new HtmlDocument();

            document.LoadHtml(downloadsContent);

            List <HtmlNode> torrentNodes = document.DocumentNode
                                           .SelectNodes("//div//table//tr//td//span")
                                           .Where(n => n.Element("div") != null).ToList();
            List <TorrentDescription> torrents = new List <TorrentDescription>();

            foreach (var node in torrentNodes)
            {
                string description = node.InnerText;
                description = description.Substring(0, description.IndexOf('\n'));
                string             uri = node.Element("div").Element("nobr").Element("a").InnerHtml;
                TorrentDescription torrentDescription = new TorrentDescription()
                {
                    TorrentUri  = new Uri(uri),
                    Description = description,
                    Size        = SizeRegex.IsMatch(description) ? SizeRegex.Match(description).Groups[1].Value : string.Empty,
                    Quality     = QualityRegex.IsMatch(description) ? QualityRegex.Match(description).Groups[1].Value : string.Empty
                };

                foreach (char c in charsToReplace)
                {
                    torrentDescription.Quality = torrentDescription.Quality.Replace(c, '_');
                }

                torrents.Add(torrentDescription);
            }

            return(torrents);
        }
Example #2
0
        public override void Execute()
        {
            using (AppDbContext db = new AppDbContext())
            {
                Program.Logger.Debug($"{GetType().Name}: Retrieving notification with id: {NotificationId}");
                var notification = db.GetNotificationById(NotificationId);
                if (notification == null)
                {
                    Program.Logger.Debug($"{GetType().Name}: Notification with specified Id was not found");
                    Status = false;
                    return;
                }

                Program.Logger.Debug($"{GetType().Name}: Retrieving settings of {notification.Subscription.User}");
                var settings = db.GetSettingsByUser(notification.Subscription.User);
                if (settings == null)
                {
                    Program.Logger.Debug($"{GetType().Name}: User's settings were not found");
                    Status = false;
                    return;
                }

                ITorrentGetter            torrentGetter = new LostFilmTorrentGetter();
                List <TorrentDescription> torrents      = null;
                Program.Logger.Debug($"{GetType().Name}: Retrieving torrents for {notification.Episode.Show} - {notification.Episode.Title}");
                try
                {
                    torrents = torrentGetter.GetEpisodeTorrents(notification.Episode, settings.SiteLogin, settings.SitePassword);
                }
                catch (Exception e)
                {
                    Program.Logger.Error(e, $"An error occured while retrieving torrents for {notification.Episode.Show.Title} - {notification.Episode.Title}");
                    TelegramApi.SendMessage(Message.From, "(Не удалось получить список торрентов. Возможно указан неверный логин/пароль)");
                    Status = false;
                    return;
                }

                Program.Logger.Debug($"{GetType().Name}: Number of torrents: {torrents?.Count() ?? 0}");
                TorrentDescription torrent = null;
                if (torrents != null && torrents.Count() != 0)
                {
                    Program.Logger.Debug($"{GetType().Name}: Retrieving torrent with required quality ({Quality})");
                    torrent = torrents.FirstOrDefault(t => t.Quality == Quality);
                }

                if (torrent == null)
                {
                    Program.Logger.Debug($"{GetType().Name}: Torrent with required quality was not found");
                    Status = false;
                    return;
                }

                Program.Logger.Debug($"{GetType().Name}: Creating new download task");
                db.DownloadTasks.Add(new DownloadTask()
                {
                    Episode    = notification.Episode,
                    User       = notification.Subscription.User,
                    TorrentUrl = torrent.TorrentUri.ToString()
                });

                Program.Logger.Debug($"{GetType().Name}: Saving changes to database");
                db.SaveChanges();
                Status = true;
            }
        }
        private IEnumerable<TorrentDescription> GetTorrents(string showId, string seasonNumber, string episodeNumber)
        {
            string downloadsContent = Encoding.GetEncoding(1251).GetString(client.GetAsync(string.Format(DownloadsUrl, showId, seasonNumber, episodeNumber)).Result.Content.ReadAsByteArrayAsync().Result);
            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(downloadsContent);

            List<HtmlNode> torrentNodes = document.DocumentNode
                .SelectNodes("//div//table//tr//td//span")
                .Where(n => n.Element("div") != null).ToList();
            List<TorrentDescription> torrents = new List<TorrentDescription>();
            foreach (var node in torrentNodes)
            {
                string description = node.InnerText;
                description = description.Substring(0, description.IndexOf('\n'));
                string uri = node.Element("div").Element("nobr").Element("a").InnerHtml;
                TorrentDescription torrentDescription = new TorrentDescription()
                {
                    TorrentUri = new Uri(uri),
                    Description = description,
                    Size = SizeRegex.IsMatch(description) ? SizeRegex.Match(description).Groups[1].Value : string.Empty,
                    Quality = QualityRegex.IsMatch(description) ? QualityRegex.Match(description).Groups[1].Value : string.Empty
                };

                foreach (char c in charsToReplace)
                {
                    torrentDescription.Quality = torrentDescription.Quality.Replace(c, '_');
                }

                torrents.Add(torrentDescription);
            }

            return torrents;
        }