/// <summary> /// add /// </summary> /// <param name="uris"></param> /// <returns></returns> public static async Task <string> AddUriAsync(List <string> uris) { AriaSendOption option = new AriaSendOption { Out = "index测试.html", Dir = "home/" }; List <object> ariaParams = new List <object> { "token:" + TOKEN, uris, option }; AriaSendData ariaSend = new AriaSendData { Id = Guid.NewGuid().ToString("N"), Jsonrpc = JSONRPC, Method = "aria2.addUri", Params = ariaParams }; string sendJson = JsonConvert.SerializeObject(ariaSend); string result = await SendAndReceiveAsync(sendJson); return(result); }
/// <summary> /// This method adds a new download. /// uris is an array of HTTP/FTP/SFTP/BitTorrent URIs (strings) pointing to the same resource. /// If you mix URIs pointing to different resources, /// then the download may fail or be corrupted without aria2 complaining. /// When adding BitTorrent Magnet URIs, /// uris must have only one element and it should be BitTorrent Magnet URI. /// options is a struct and its members are pairs of option name and value. /// See Options below for more details. /// If position is given, it must be an integer starting from 0. /// The new download will be inserted at position in the waiting queue. /// If position is omitted or position is larger than the current size of the queue, /// the new download is appended to the end of the queue. /// This method returns the GID of the newly registered download. /// </summary> /// <param name="uris"></param> /// <param name="dir"></param> /// <param name="outFile"></param> /// <returns></returns> public static async Task <AriaAddUri> AddUriAsync(List <string> uris, AriaSendOption option, int position = -1) { List <object> ariaParams = new List <object> { "token:" + TOKEN, uris, option }; if (position > -1) { ariaParams.Add(position); } AriaSendData ariaSend = new AriaSendData { Id = Guid.NewGuid().ToString("N"), Jsonrpc = JSONRPC, Method = "aria2.addUri", Params = ariaParams }; return(await GetRpcResponseAsync <AriaAddUri>(ariaSend)); }
/// <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; } }))); }