public override IEnumerable <IAudioBookGroup> GetAllRecordGroups(IDbContainer dbContainer)
        {
            var result = GroupKeys.Select(groupKey => GetRecordGroup(groupKey, dbContainer));

            dbContainer.SaveChanges();
            return(result);
        }
예제 #2
0
        public override void DownloadRecordTarget(string loginCoockies, IAudioBook record, IDbContainer dbContainer,
                                                  CancellationToken cancellationToken)
        {
            var config = Config.Load <RutrackerTorrentFilesDownloaderConfig>();

            Directory.CreateDirectory(config.TorrentFilesFolder);

            var userData = dbContainer.UserDataSet.CreateUserData();

            userData.RecordGroupKey = record.GroupKey;
            userData.RecordKey      = record.Key;

            var targetLibraryPath = GetAbsoluteLibraryPath(record);

            userData.LocalPath = targetLibraryPath;

            var downloader = Context.I.ComponentFactory.GetCreators <ITorrentFileDownloaderPlugin>()
                             .Select(item => item.GetInstance <ITorrentFileDownloaderPlugin>())
                             .FirstOrDefault(item => item.DisplayName == config.TorrentClientName);

            if (downloader != null && downloader.IsExists)
            {
                downloader.Download(record.MagnetLink, targetLibraryPath);
            }

            dbContainer.UserDataSet.AddUserData(userData);
            dbContainer.SaveChanges();
        }
        protected override void DownloadRecordGroup(IDbContainer dbContainer, IAudioBookGroup audioBookGroup,
                                                    CancellationToken cancellationToken)
        {
            dbContainer.SaveChanges();

            var url = $"http://rutracker.org/forum/viewforum.php?f={audioBookGroup.Key}";

            var pageIndex = 0;
            var pageCount = 0;

            do
            {
                var progressMessage = audioBookGroup.Title +
                                      (pageCount > 0 ? $": {pageIndex} / {pageCount}" : string.Empty);

                ProgressMessage.Report(pageIndex, pageCount, progressMessage);

                string pageHtml;

                using (var webClient = WebClientPool.GetPoolItem())
                {
                    var pageUrl = $"{url}&start={pageIndex * RecordsOnPageCount}";
                    pageHtml = webClient.Target.DownloadString(pageUrl);
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                }

                var document = new HtmlDocument();
                document.LoadHtml(pageHtml);

                if (pageCount == 0)
                {
                    UpdateRecordGroupInfo(audioBookGroup, document);
                    dbContainer.AudioBookGroupSet.AddChangedRecordGroups(audioBookGroup);
                    pageCount = audioBookGroup.LastPageCount;
                }

                ParseRecordGroupPage(dbContainer, audioBookGroup, document, cancellationToken);

                Thread.Sleep(200);

                pageIndex++;
            } while (pageIndex < pageCount);

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }
            audioBookGroup.LastUpdate = DateTime.Now;
        }