Example #1
0
        public DownloadStatus GetDownloadStatus(string gid)
        {
            string filePath = "";

            while (true)
            {
                var status = AriaClient.TellStatus(gid);
                if (status == null || status.Result == null)
                {
                    continue;
                }

                if (status.Result.Result == null && status.Result.Error != null)
                {
                    if (status.Result.Error.Message.Contains("is not found"))
                    {
                        OnDownloadFinish(false, null, gid, status.Result.Error.Message);
                        return(DownloadStatus.ABORT);
                    }
                }

                if (status.Result.Result.Files != null && status.Result.Result.Files.Count >= 1)
                {
                    filePath = status.Result.Result.Files[0].Path;
                }

                long totalLength     = long.Parse(status.Result.Result.TotalLength);
                long completedLength = long.Parse(status.Result.Result.CompletedLength);
                long speed           = long.Parse(status.Result.Result.DownloadSpeed);
                // 回调
                OnTellStatus(totalLength, completedLength, speed, gid);

                if (status.Result.Result.Status == "complete")
                {
                    break;
                }
                if (status.Result.Result.ErrorCode != null && status.Result.Result.ErrorCode != "0")
                {
                    Console.WriteLine("ErrorMessage: " + status.Result.Result.ErrorMessage);

                    //// 如果返回状态码不是200,则继续
                    //if (status.Result.Result.ErrorMessage.Contains("The response status is not successful"))
                    //{
                    //    Thread.Sleep(1000);
                    //    continue;
                    //}

                    // aira中删除记录
                    var ariaRemove1 = AriaClient.RemoveDownloadResultAsync(gid);
                    Console.WriteLine(ariaRemove1);

                    // 返回回调信息,退出函数
                    OnDownloadFinish(false, null, gid, status.Result.Result.ErrorMessage);
                    return(DownloadStatus.FAILED);
                }

                // 降低CPU占用
                Thread.Sleep(500);
            }
            OnDownloadFinish(true, filePath, gid, null);
            return(DownloadStatus.SUCCESS);
        }
Example #2
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;
                }
            })));
        }
Example #3
0
        /// <summary>
        /// 获取gid下载项的状态
        ///
        /// TODO
        /// 对于下载的不同状态的返回值的测试
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public DownloadResult GetDownloadStatus(string gid, Action action = null)
        {
            string filePath = "";

            while (true)
            {
                Task <AriaTellStatus> status = AriaClient.TellStatus(gid);
                if (status == null || status.Result == null)
                {
                    continue;
                }

                if (status.Result.Result == null && status.Result.Error != null)
                {
                    if (status.Result.Error.Message.Contains("is not found"))
                    {
                        OnDownloadFinish(false, null, gid, status.Result.Error.Message);
                        return(DownloadResult.ABORT);
                    }
                }

                if (status.Result.Result.Files != null && status.Result.Result.Files.Count >= 1)
                {
                    filePath = status.Result.Result.Files[0].Path;
                }

                long totalLength     = long.Parse(status.Result.Result.TotalLength);
                long completedLength = long.Parse(status.Result.Result.CompletedLength);
                long speed           = long.Parse(status.Result.Result.DownloadSpeed);

                // 回调
                OnTellStatus(totalLength, completedLength, speed, gid);

                // 在外部执行
                if (action != null)
                {
                    action.Invoke();
                }

                if (status.Result.Result.Status == "complete")
                {
                    break;
                }
                if (status.Result.Result.ErrorCode != null && status.Result.Result.ErrorCode != "0")
                {
                    if (status.Result != null)
                    {
                        Utils.Debugging.Console.PrintLine("ErrorMessage: " + status.Result.Result.ErrorMessage);
                        LogManager.Error("AriaManager", status.Result.Result.ErrorMessage);
                    }

                    //// 如果返回状态码不是200,则继续
                    //if (status.Result.Result.ErrorMessage.Contains("The response status is not successful"))
                    //{
                    //    Thread.Sleep(1000);
                    //    continue;
                    //}

                    // aira中删除记录
                    Task <AriaRemove> ariaRemove1 = AriaClient.RemoveDownloadResultAsync(gid);
                    Utils.Debugging.Console.PrintLine(ariaRemove1);
                    if (ariaRemove1.Result != null)
                    {
                        LogManager.Debug("AriaManager", ariaRemove1.Result.Result);
                    }

                    // 返回回调信息,退出函数
                    OnDownloadFinish(false, null, gid, status.Result.Result.ErrorMessage);
                    return(DownloadResult.FAILED);
                }

                // 降低CPU占用
                Thread.Sleep(100);
            }
            OnDownloadFinish(true, filePath, gid, null);
            return(DownloadResult.SUCCESS);
        }