Пример #1
0
 public Aria2Downloader(IConfiguration configuration, IHttpClientFactory httpClientFactory, ILogger <Aria2Downloader> logger)
 {
     _logger  = logger;
     _manager = new AriaManager(configuration["Aria2:Url"],
                                configuration["Aria2:Token"],
                                httpClientFactory.CreateClient());
 }
Пример #2
0
        /// <summary>
        ///     The main method for this class, import the URL and sparate automatically.
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task TaskHandler(string url, IProgress <double[]> progressStatus)
        {
            // Declare aria manager
            var ariaManager = new AriaManager();

            // Case 1: Single Bilibili video
            if (Regex.IsMatch(url, @"bilibili.com/video/av(\d+)"))
            {
                // Make some fake news
                progressStatus.Report(new[] { 0d, 0d });

                await ariaManager.AddUri(await GenerateGeneralVideoLink(url),
                                         "Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)", url);

                // Make some fake news again
                progressStatus.Report(new[] { 1d, 1d });
            }

            // Case 2: A user and his/her videos
            else if (Regex.IsMatch(url, @"space.bilibili.com/(\d+)"))
            {
                await ariaManager.AddUri(await GenerateUserVideoLink(url, progressStatus),
                                         "Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)", url);
            }

            // Case 3: Aria2 commandline
            else if (Regex.IsMatch(url, @"^(aria2c).*http(s?)://"))
            {
                MessageBox.Show("Not yet implemented, use URL instead.");
            }

            // Case 4: Normal task
            else if (Regex.IsMatch(url, @"^http(s?)://"))
            {
                await ariaManager.AddUri(new List <string> {
                    url
                });
            }

            // Case 5: Don't know wtf is this...
            else
            {
                progressStatus.Report(new[] { -999d, -999d });
            }
        }
Пример #3
0
 public TaskHandler()
 {
     _AriaManager = Properties.Settings.Default.UseExternalAria
         ? new AriaManager(Properties.Settings.Default.ExternalRpc)
         : new AriaManager();
 }
Пример #4
0
 public StatusTest()
 {
     _ariaManager = new AriaManager();
 }
        public static async Task InitializeAsync(long maxDownloadSpeed, FileInfo logFile)
        {
            AriaHttpPatchAcquisition.maxDownloadSpeed = maxDownloadSpeed;

            if (ariaProcess == null || ariaProcess.HasExited)
            {
                // Kill stray aria2c-xl processes
                var stray = Process.GetProcessesByName("aria2c-xl");

                foreach (var process in stray)
                {
                    try
                    {
                        process.Kill();
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "[ARIA] Could not kill stray process.");
                    }
                }

                // I don't really see the point of this, but aria complains if we don't provide a secret
                var rng    = new Random();
                var secret = BitConverter.ToString(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes($"{rng.Next()}{rng.Next()}{rng.Next()}{rng.Next()}")));

                var ariaPath = Path.Combine(Paths.ResourcesPath, "aria2c-xl.exe");

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    ariaPath = "aria2c";
                }

                var ariaPort = Util.GetAvailablePort();
                var ariaHost = $"http://localhost:{ariaPort}/jsonrpc";

                var ariaArgs =
                    $"--enable-rpc --rpc-secret={secret} --rpc-listen-port={ariaPort} --log=\"{logFile.FullName}\" --log-level=notice --max-connection-per-server=8 --auto-file-renaming=false --allow-overwrite=true";

                Log.Verbose($"[ARIA] Aria process not there, creating from {ariaPath} {ariaArgs}...");

                var startInfo = new ProcessStartInfo(ariaPath, ariaArgs)
                {
#if !DEBUG
                    CreateNoWindow = true,
                    WindowStyle    = ProcessWindowStyle.Hidden,
#endif
                    UseShellExecute = false,
                };

                ariaProcess = Process.Start(startInfo);

                Thread.Sleep(400);

                if (ariaProcess == null)
                {
                    throw new Exception("ariaProcess was null.");
                }

                if (ariaProcess.HasExited)
                {
                    throw new Exception("ariaProcess has exited.");
                }

                manager = new AriaManager(secret, ariaHost);
            }
        }
Пример #6
0
        /// <summary>
        /// 采用Aria下载文件
        /// </summary>
        /// <param name="downloading"></param>
        /// <returns></returns>
        private DownloadResult DownloadByAria(DownloadingItem downloading, List <string> urls, string path, string localFileName)
        {
            // path已斜杠结尾,去掉斜杠
            path = path.TrimEnd('/').TrimEnd('\\');

            //检查gid对应任务,如果已创建那么直接使用
            //但是代理设置会出现不能随时更新的问题

            if (downloading.Downloading.Gid != null)
            {
                Task <AriaTellStatus> status = AriaClient.TellStatus(downloading.Downloading.Gid);
                if (status == null || status.Result == null)
                {
                    downloading.Downloading.Gid = null;
                }
                else if (status.Result.Result == null && status.Result.Error != null)
                {
                    if (status.Result.Error.Message.Contains("is not found"))
                    {
                        downloading.Downloading.Gid = null;
                    }
                }
            }

            if (downloading.Downloading.Gid == null)
            {
                AriaSendOption option = new AriaSendOption
                {
                    //HttpProxy = $"http://{Settings.GetAriaHttpProxy()}:{Settings.GetAriaHttpProxyListenPort()}",
                    Dir = path,
                    Out = localFileName,
                    //Header = $"cookie: {LoginHelper.GetLoginInfoCookiesString()}\nreferer: https://www.bilibili.com",
                    //UseHead = "true",
                    UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36",
                };

                // 如果设置了代理,则增加HttpProxy
                if (SettingsManager.GetInstance().IsAriaHttpProxy() == AllowStatus.YES)
                {
                    option.HttpProxy = $"http://{SettingsManager.GetInstance().GetAriaHttpProxy()}:{SettingsManager.GetInstance().GetAriaHttpProxyListenPort()}";
                }

                // 添加一个下载
                Task <AriaAddUri> ariaAddUri = AriaClient.AddUriAsync(urls, option);
                if (ariaAddUri == null || ariaAddUri.Result == null || ariaAddUri.Result.Result == null)
                {
                    return(DownloadResult.FAILED);
                }

                // 保存gid
                string gid = ariaAddUri.Result.Result;
                downloading.Downloading.Gid = gid;
            }
            else
            {
                Task <AriaPause> ariaUnpause = AriaClient.UnpauseAsync(downloading.Downloading.Gid);
            }

            // 管理下载
            AriaManager ariaManager = new AriaManager();

            ariaManager.TellStatus     += AriaTellStatus;
            ariaManager.DownloadFinish += AriaDownloadFinish;
            return(ariaManager.GetDownloadStatus(downloading.Downloading.Gid, new Action(() =>
            {
                cancellationToken.ThrowIfCancellationRequested();
                switch (downloading.Downloading.DownloadStatus)
                {
                case DownloadStatus.PAUSE:
                    Task <AriaPause> ariaPause = AriaClient.PauseAsync(downloading.Downloading.Gid);
                    // 通知UI,并阻塞当前线程
                    Pause(downloading);
                    break;

                case DownloadStatus.DOWNLOADING:
                    break;
                }
            })));
        }