Exemplo n.º 1
0
        public void Download(string quality, IList <ImageDownloadProgress> allProgress)
        {
            var progressEnumerable = allProgress
                                     .Where(_ => Str.Equals(_.QualityGroup.Quality, quality))
                                     .OrderBy(getReleaseDate);

            var commonStack = new ConcurrentStack <ImageDownloadProgress>();
            var gdriveStack = new Stack <ImageDownloadProgress>();
            var megaStack   = new Stack <ImageDownloadProgress>();

            foreach (var progress in progressEnumerable)
            {
                if (progress.MegaUrl != null && progress.GdriveUrl != null)
                {
                    commonStack.Push(progress);
                }
                else if (progress.MegaUrl != null)
                {
                    megaStack.Push(progress);
                }
                else if (progress.GdriveUrl != null)
                {
                    gdriveStack.Push(progress);
                }
            }

            Console.WriteLine("Found {0} directories for quality '{1}' in configuration",
                              commonStack.Count + megaStack.Count + gdriveStack.Count,
                              quality);

            TotalCount = commonStack.Concat(megaStack).Concat(gdriveStack).Sum(_ => _.FilesOnline.Count);

            _countInDownloadedDirs = 0;
            ProgressChanged?.Invoke();

            var webClient1 = new GdriveWebClient();
            var webClient2 = new GdriveWebClient();

            void megaFileDownloaded()
            {
                Interlocked.Increment(ref _countInDownloadedDirs);
                ProgressChanged?.Invoke();
            }

            _megatools.FileDownloaded += megaFileDownloaded;

            Parallel.Invoke(
                () => download(megaStack, commonStack, downloadFromMega),
                // google drive delays download start
                () => download(gdriveStack, commonStack, t => downloadFromGdrive(t, webClient1)),
                () => download(gdriveStack, commonStack, t => downloadFromGdrive(t, webClient2)));

            _megatools.FileDownloaded -= megaFileDownloaded;
        }
Exemplo n.º 2
0
        public static bool DownloadAndExtract(this GdriveWebClient webClient, string gdriveUrl, string targetDirectory, string fileName)
        {
            if (!Str.Equals(".7z", Path.GetExtension(fileName)))
            {
                throw new ArgumentException();
            }

            string archiveFileName = targetDirectory.AddPath(fileName);

            if (File.Exists(archiveFileName))
            {
                try
                {
                    File.Delete(archiveFileName);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}");
                    return(false);
                }
            }

            try
            {
                webClient.DownloadFromGdrive(gdriveUrl, targetDirectory);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error downloading {archiveFileName} from {gdriveUrl}: {ex.Message}");
                return(false);
            }

            if (!File.Exists(archiveFileName))
            {
                Console.WriteLine($"Failed to download {archiveFileName} from {gdriveUrl}");
                return(false);
            }

            var sevenZip = new SevenZip(silent: true);

            sevenZip.Extract(archiveFileName, targetDirectory, Enumerable.Empty <string>());

            try
            {
                File.Delete(archiveFileName);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}");
            }

            return(true);
        }
Exemplo n.º 3
0
        private void downloadFromGdrive(ImageDownloadProgress task, GdriveWebClient webClient)
        {
            string fileName        = task.Dir.Subdirectory + ".7z";
            string archiveFileName = task.TargetDirectory.AddPath(fileName);

            lock (_syncOutput)
                Console.WriteLine($"Downloading {task.Dir.Subdirectory} from {task.GdriveUrl}");

            if (File.Exists(archiveFileName))
            {
                try
                {
                    File.Delete(archiveFileName);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}");
                    return;
                }
            }

            try
            {
                webClient.DownloadFromGdrive(task.GdriveUrl, task.TargetDirectory);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error downloading {archiveFileName} from {task.GdriveUrl}: {ex.Message}");
                return;
            }

            if (!File.Exists(archiveFileName))
            {
                Console.WriteLine($"Failed to download {archiveFileName} from {task.GdriveUrl}");
                return;
            }

            var sevenZip = new SevenZip(silent: true);

            sevenZip.Extract(archiveFileName, task.TargetDirectory, Enumerable.Empty <string>());

            Interlocked.Add(ref _countInDownloadedDirs, task.FilesOnline.Count - task.FilesDownloaded.Count);
            ProgressChanged?.Invoke();

            try
            {
                File.Delete(archiveFileName);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}");
            }
        }
Exemplo n.º 4
0
        public static void TryDownload(this GdriveWebClient webClient, string url, string targetDirectory, string description)
        {
            Console.Write($"Download {description} from {url} ...");

            try
            {
                webClient.DownloadFromGdrive(url, targetDirectory);
            }
            catch (Exception ex)
            {
                Console.WriteLine($" {ex}");
                return;
            }

            Console.WriteLine(" done");
        }
Exemplo n.º 5
0
        private void downloadFromGdrive(ImageDownloadProgress task, GdriveWebClient webClient)
        {
            string fileName        = task.Dir.Subdirectory + ".7z";
            string targetDirectory = task.TargetDirectory;
            string gdriveUrl       = task.GdriveUrl;

            lock (_syncOutput)
                Console.WriteLine($"Downloading {task.Dir.Subdirectory} from {gdriveUrl}");

            if (!webClient.DownloadAndExtract(gdriveUrl, targetDirectory, fileName))
            {
                return;
            }

            Interlocked.Add(ref _countInDownloadedDirs, task.FilesOnline.Count - task.FilesDownloaded.Count);
            ProgressChanged?.Invoke();
        }
Exemplo n.º 6
0
        public void Download(string storageUrl, string targetDirectory, string name, bool silent = false, int?timeoutSec = null)
        {
            if (_process != null)
            {
                throw new InvalidOperationException("Download is already running. Use another instance to start new download.");
            }

            _silent = silent;

            lock (_syncSelfdownload)
                if (!File.Exists(MegadlExePath))
                {
                    var webClient = new GdriveWebClient();
                    webClient.DownloadAndExtract(_megatoolsUrl, AppDir.Update, "megatools.7z");
                }

            DownloadedCount = 0;

            string arguments;

            if (targetDirectory.Contains(' '))
            {
                arguments = $@"--path=""{targetDirectory}"" --print-names {storageUrl}";
            }
            else
            {
                arguments = $@"--path={targetDirectory} --print-names {storageUrl}";
            }

            _process = new Process
            {
                StartInfo = new ProcessStartInfo(MegadlExePath, arguments)
                {
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    UseShellExecute        = false,
                    CreateNoWindow         = true
                },

                EnableRaisingEvents = true
            };

            _process.OutputDataReceived += downloadOutputReceived;
            _process.ErrorDataReceived  += downloadErrorReceived;

            if (!_silent)
            {
                Console.WriteLine("Downloading {0} from {1} to {2}", name, storageUrl, targetDirectory);
            }

            AppDomain.CurrentDomain.ProcessExit += processExit;
            _process.Start();
            _process.BeginOutputReadLine();
            _process.BeginErrorReadLine();

            if (timeoutSec.HasValue)
            {
                _process.WaitForExit(timeoutSec.Value * 1000);
            }
            else
            {
                _process.WaitForExit();
            }

            Abort();
        }