Example #1
0
        /* //альтернативное решение проблемы с кодировкой
         *  WebClient wc = new WebClient();
         *  string str = wc.DownloadString(url);
         *  HtmlDocument htmlDocument = new HtmlDocument();
         *  htmlDocument.LoadHtml(str);*/
        public IEnumerable <TorrentMovie> GetMovies()
        {
            Log.Information($"MoviesDownloader.MoviesGetterService.GetMovies() start  at {DateTime.Now}{Environment.NewLine}");
            var urls = GetPaths();

            foreach (var url in urls)
            {
                var nodes = GetHtmlNodesTagTr(url);

                if (nodes != null)
                {
                    foreach (var node in nodes)
                    {
                        var NodeId = node.Id.ToString();
                        if (NodeId == "tr-5836841")//ignore post"Открылся новый сайт новинок"
                        {
                            continue;
                        }
                        var SubPathDownLoad = "";
                        try
                        {
                            SubPathDownLoad = "https://rutracker.org/forum/"
                                              + node.SelectSingleNode(".//a[@class='torTopic bold tt-text']").Attributes["href"].Value;
                        }
                        catch (Exception ex)
                        {
                            Log.Error($"MoviesDownloader.MoviesGetterService.GetMovies(){Environment.NewLine}" +
                                      $"Get path of movies form url was fail with exception:{Environment.NewLine}{ex.Message}" +
                                      $"{Environment.NewLine}url={url}{Environment.NewLine}NodeId={NodeId}");
                            continue;
                        }
                        Log.Information($"MoviesDownloader.MoviesGetterService.GetMovies(){Environment.NewLine}" +
                                        $"Parent's page address{Environment.NewLine}{url}{Environment.NewLine}" +
                                        $"Address is being processed{Environment.NewLine}{SubPathDownLoad}{Environment.NewLine}" +
                                        $"{DateTime.Now}{Environment.NewLine}");


                        var processedMovie = _unitOfWork.torrentMove.AsQueryable().FirstOrDefault(p => p.pathDownLoad.Equals(SubPathDownLoad)); //Check to be movie in DB
                        if (processedMovie != null)                                                                                             //if movie exist in the DB
                        {
                            var lastPost = node.SelectSingleNode(".//td[@class='vf-col-last-post tCenter small nowrap']").SelectSingleNode(".//p").InnerText;

                            if (processedMovie.lastPost != lastPost || processedMovie.amountOfComments == 0 || processedMovie.commentIndex > 2000)
                            {
                                processedMovie.lastPost = lastPost;
                                _saveCommentsToDb.SaveCommens(processedMovie);
                                _commentHanlder.GetCommentIndex(processedMovie);
                            }
                            continue;
                        }
                        else
                        {
                            try
                            {
                                var textFromTegA = node.SelectSingleNode(".//a[@class='torTopic bold tt-text']").InnerText.ToString();

                                var additInformFromTitle = Regex.Match(textFromTegA, @"(\[.+\])").ToString();

                                var    _title        = Regex.Replace(textFromTegA, @"(\[.+\].+)", "");//delete of addition information
                                var    _genre        = Regex.Replace(Regex.Match(additInformFromTitle, @"\s[а-я].+,").ToString(), @"(,$)", "");
                                var    _country      = Regex.Match(additInformFromTitle, @"[А-Я]{1,3}[a-я]+").ToString();
                                var    _earOfIssue   = Regex.Match(additInformFromTitle, @"\d{4}").ToString();
                                var    _videoQuality = Regex.Replace(Regex.Match(additInformFromTitle, @"[A-Z]+.+]").ToString(), "]", "");
                                var    _size         = Regex.Replace(node.SelectSingleNode(".//a[@class='small f-dl dl-stub']").InnerText.ToString(), @"&nbsp;", "");
                                var    _commentIndex = 0;
                                string _strDownloads = Regex.Replace(node.SelectSingleNode(".//p[@title='Торрент скачан']").InnerText, @"\D+", "").ToString();
                                int    _downloads    = Convert.ToInt32(_strDownloads);
                                var    _pathDownLoad = "https://rutracker.org/forum/" + node.SelectSingleNode(".//a[@class='torTopic bold tt-text']").Attributes["href"].Value;
                                var    _lastPost     = node.SelectSingleNode(".//td[@class='vf-col-last-post tCenter small nowrap']").Element("p").InnerText;
                                if (_genre != null)
                                {
                                    torrentMoves.Add(new TorrentMovie
                                    {
                                        title        = _title,
                                        genre        = _genre,
                                        country      = _country,
                                        earOfIssue   = _earOfIssue,
                                        videoQuality = _videoQuality,
                                        size         = _size,
                                        commentIndex = _commentIndex,
                                        downloads    = _downloads,
                                        pathDownLoad = _pathDownLoad,
                                        movieId      = NodeId,
                                        lastPost     = _lastPost
                                    });
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Information($"MoviesDownloader.MoviesGetterService.GetMovies(){Environment.NewLine}Error reading html tags or/and creating new TorrentMovie object :{Environment.NewLine}{ex.Message}" +
                                                $"{Environment.NewLine}url={url}{Environment.NewLine}SubPath={SubPathDownLoad}{Environment.NewLine}{DateTime.Now}{Environment.NewLine}");
                            }
                        }
                    }
                }
            }
            Log.Information($"MoviesDownloader.MoviesGetterService.GetMovies() finished  at {DateTime.Now}{Environment.NewLine}");
            return(torrentMoves);
        }