public static void DownloadFile(string url, string directory = null, bool startNow = false, string parameters = null) { //NamedValues<ZValue> parameters2 = NamedValues.ParseValues(parameters); NamedValues <ZValue> parameters2 = ParseParameters(parameters); DownloadManagerClientBase downloadManagerClient = DownloadAutomateManagerCreator.CreateDownloadManagerClient(GetDownloadAutomateManagerConfig(GetTestValue(parameters2))); Debrider debrider = DownloadAutomateManagerCreator.CreateDebrider(XmlConfig.CurrentConfig); url = debrider.DebridLink(url); Uri uri = new Uri(url); string file = uri.Segments[uri.Segments.Length - 1]; if (directory != null) { file = zPath.Combine(directory, file); } downloadManagerClient.AddDownload(url, file, startNow: startNow); }
private void ManageNewDownloadFiles() { if (_trace) { pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 01 begin"); } int nb = _downloadClient.GetDownloadCount(); if (_trace) { pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 02 : search new download link : current download count {0}", nb); } if (nb >= _maxSimultaneousDownload) { if (_trace) { pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 09 end"); } return; } foreach (QueueDownloadFile queueDownloadFile in _mongoQueueDownloadFileManager.Find("{}", sort: "{ _id: 1 }")) { QueueDownloadFile queueDownloadFile2 = queueDownloadFile; if (_queueDownloadFiles.ContainsKey(queueDownloadFile2.Id)) { queueDownloadFile2 = GetQueueDownloadFile(queueDownloadFile2.Id); } while (true) { if (!_queueDownloadFiles.ContainsKey(queueDownloadFile2.Id)) { if (!_queueDownloadFiles.TryAdd(queueDownloadFile2.Id, queueDownloadFile2)) { pb.Trace.WriteLine("error adding QueueDownloadFile id {0} to ConcurrentDictionary _queueDownloadFiles", queueDownloadFile2.Id); } if (_trace) { //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 05 : add to _queueDownloadFiles, queueDownloadFile.Id {0} queueDownloadFile.Key {1} queueDownloadFile.File \"{2}\"", queueDownloadFile2.Id, queueDownloadFile2.Key, queueDownloadFile2.File); pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 05 : add to _queueDownloadFiles, queueDownloadFile.Id {0} queueDownloadFile.Key {1} queueDownloadFile.Directory \"{2}\" queueDownloadFile.Filename \"{3}\"", queueDownloadFile2.Id, queueDownloadFile2.Key, queueDownloadFile2.Directory, queueDownloadFile2.Filename); } } DownloadLinkRef downloadLinkRef = GetNextDownloadLink(queueDownloadFile2); if (downloadLinkRef == null) { if (_trace) { //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 03 : no more link to download for \"{0}\"", queueDownloadFile2.File); pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 03 : no more link to download for \"{0}\" \"{1}\"", queueDownloadFile2.Directory, queueDownloadFile2.Filename); //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 04 : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} file \"{3}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.File); pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 04 : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} directory \"{3}\" filename \"{4}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.Directory, queueDownloadFile2.Filename); } // sauver dans mongo Downloaded _mongoDownloadedFileManager if (queueDownloadFile2.DownloadNbInProgress == 0) { EndDownload(queueDownloadFile2); } else { SaveQueueDownloadFile(queueDownloadFile2); } break; } else { downloadLinkRef.DownloadId = _downloadClient.AddDownload(downloadLinkRef.DebridedDownloadLink, downloadLinkRef.File, startNow: true); queueDownloadFile2.Modified = true; if (_trace) { pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 06 : start download DownloadId {0} file \"{1}\"", downloadLinkRef.DownloadId, downloadLinkRef.File); } DownloadFilePartLink filePartLink = GetDownloadFilePartLink(downloadLinkRef); if (filePartLink != null) { filePartLink.State = DownloadState.DownloadStarted; filePartLink.StartDownloadTime = DateTime.Now; } if (queueDownloadFile2.StartDownloadTime == null) { queueDownloadFile2.StartDownloadTime = DateTime.Now; } queueDownloadFile2.DownloadNbInProgress++; SaveQueueDownloadFile(queueDownloadFile2); if (_trace) { //pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 07 : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} file \"{3}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.File); pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 07 : downloadFile.Id {0} downloadFile.DownloadNbInProgress {1} downloadFile.AllDownloadLinkTreated {2} directory \"{3}\" filename \"{4}\"", queueDownloadFile2.Id, queueDownloadFile2.DownloadNbInProgress, queueDownloadFile2.AllDownloadLinkTreated, queueDownloadFile2.Directory, queueDownloadFile2.Filename); pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 08 : _currentDownloadFiles.Add({0})", downloadLinkRef.DownloadId); } _currentDownloadFiles.Add(downloadLinkRef.DownloadId, downloadLinkRef); _mongoCurrentDownloadFileManager.Save(BsonValue.Create(downloadLinkRef.DownloadId), downloadLinkRef); if (++nb >= _maxSimultaneousDownload) { break; } } } if (nb >= _maxSimultaneousDownload) { break; } } if (_trace) { pb.Trace.WriteLine("DownloadManager.ManageNewDownloadFiles() 09 end"); } }