private ImportStatus AddLocalFile(NzbImportModel import) { //Determine if the file is a RAW NZB (or is zipped, gzipped, rarred) and then Import if(!_disk.FileExists(import.Location)) { Logger.Error("NZB: {0} does not exist", import.Location); return ImportStatus.Invalid; } import.Name = _disk.SimpleFilename(import.Location); //Get the Filename without path or Extension import.Stream = _disk.OpenAsStream(import.Location); //Get the NZB as a stream if (import.Stream == null) { return ImportStatus.Failed; } var nzb = _parse.Process(import); if (nzb == null) //If its null return return ImportStatus.Invalid; //Run PreQueue if one is set if (!String.IsNullOrEmpty(_config.GetValue("PreQueueScript", String.Empty, false))) { if (!_preQueue.Run(nzb)) return ImportStatus.Rejected; //Return rejected status if NZB is not wanted due to PreQueue } var position = _queue.AllItems().Count(q => (int)q.Priority >= (int)import.Priority); //Find all items with a higher or equal priority and insert it at that point (zero-indexed list mataches perfectly) _queue.Insert(nzb, position); //Do the insert! _nntp.Connect(); //Start Downloading if not already doing so return ImportStatus.Ok; }
public void DownloadAsStream(NzbImportModel nzb) { WebClient wc = new WebClient(); Stream nzbStream = wc.OpenRead(nzb.Location); WebHeaderCollection whc = wc.ResponseHeaders; if (GetConnectionResponse(whc)) { nzb.Name = GetRemoteNzbFilename(whc); if (String.IsNullOrEmpty(nzb.Category)) nzb.Category = GetRemoteNzbCategory(whc); nzb.Stream = StreamToMemoryStream(nzbStream); nzbStream.Close(); } }
public void BeginImport(NzbImportModel import) { //Add to Queue and then Start Processing _list.Add(import); Logger.Debug("NZB to be Imported: {0}", import.Location); if (_importThread == null || !_importThread.IsAlive) { Logger.Debug("Initializing background import of NZBs."); _importThread = new Thread(Import) { Name = "ImportNzbs", Priority = ThreadPriority.Lowest }; _importThread.Start(); } else { Logger.Warn("NZB Importing already in Progress"); } }
private ImportStatus AddUrl(NzbImportModel import) { //Determine if the file is a RAW NZB (or is zipped, gzipped, rarred) and then Import _http.DownloadAsStream(import); if (import.Stream == null) { import.RetryCount++; import.WaitUntil = DateTime.Now.AddMinutes(1); return ImportStatus.Failed; } var nzb = _parse.Process(import); if (nzb == null) //If its null return return ImportStatus.Invalid; //Run PreQueue if one is set if (!String.IsNullOrEmpty(_config.GetValue("PreQueueScript", String.Empty, false))) { if (!_preQueue.Run(nzb)) return ImportStatus.Rejected; //Return rejected status if NZB is not wanted due to PreQueue } var position = _queue.AllItems().Count(q => (int)q.Priority >= (int)import.Priority); //Find all items with a higher or equal priority and insert it at that point (zero-indexed list mataches perfectly) _queue.Insert(nzb, position); //Do the insert! _nntp.Connect(); //Start Downloading if not already doing so return ImportStatus.Ok; }
private bool ProcessAddUrl(NameValueCollection queryStrrings) { NzbImportModel nzb = new NzbImportModel(); nzb.ImportType = ImportType.Url; GetAddNzbDetails(queryStrrings, nzb); //Get the details for the NZB (only using URL right now) //Todo: Return "ok\n" to user _import.BeginImport(nzb); return true; }
private bool ProcessAddLocalFile(NameValueCollection queryStrrings) { NzbImportModel nzb = new NzbImportModel(); nzb.ImportType = ImportType.Disk; GetAddNzbDetails(queryStrrings, nzb); //Get the details for the NZB (only using URL right now) //Import the NZB and Add to Queue _import.BeginImport(nzb); return true; }
private void GetAddNzbDetails(NameValueCollection queryStrrings, NzbImportModel nzb) { if (queryStrrings.AllKeys.Contains("name")) nzb.Location = queryStrrings.GetValues("name")[0]; //If Post Processing is defined, use it, otherwise set to -100 (Default) if (queryStrrings.AllKeys.Contains("pp")) nzb.PostProcessing = Convert.ToInt32(queryStrrings.GetValues("pp")[0]); else nzb.PostProcessing = -100; if (queryStrrings.AllKeys.Contains("script")) nzb.Script = queryStrrings.GetValues("script")[0]; if (queryStrrings.AllKeys.Contains("cat")) nzb.Category = queryStrrings.GetValues("cat")[0]; //If Priority is defined, use it, otherwise set to -100 (Default) if (queryStrrings.AllKeys.Contains("priority")) nzb.Priority = Convert.ToInt32(queryStrrings.GetValues("priority")[0]); else nzb.Priority = -100; if (queryStrrings.AllKeys.Contains("nzbname")) nzb.NewName = queryStrrings.GetValues("nzbname")[0]; return; }
private PostProcessing GetPostProcessing(NzbImportModel import) { if (Enum.IsDefined(typeof (PostProcessing), import.PostProcessing)) { if (import.PostProcessing == -100) return _category.Default().PostProcessing; return (PostProcessing)import.PostProcessing; //If not supplied use default for this category/default } if (!String.IsNullOrEmpty(import.Category)) { var category = _category.Find(import.Category); if (category != null) { var pp = category.PostProcessing; if (pp != null) return pp; } } return _category.Default().PostProcessing; }
public NzbModel Process(NzbImportModel import) { XNamespace ns = "http://www.newzbin.com/DTD/2003/nzb"; import.Stream.Seek(0, SeekOrigin.Begin); XDocument xDoc = XDocument.Load(import.Stream); var nzb = from n in xDoc.Descendants(ns + "nzb") select n; if (nzb.Count() != 1) return null; NzbModel newNzb = new NzbModel(); newNzb.Name = !String.IsNullOrEmpty(import.NewName) ? import.NewName : import.Name; newNzb.Id = Guid.NewGuid(); newNzb.Status = NzbStatus.Queued; newNzb.Priority = (Priority)import.Priority; newNzb.Script = import.Script; newNzb.Category = import.Category; newNzb.PostProcessing = GetPostProcessing(import); var nzbFileList = new List<NzbFileModel>(); //Get all the files for this NZB var files = from f in nzb.Elements(ns + "file") select f; foreach (var file in files) { var nzbFile = new NzbFileModel(); nzbFile.Status = NzbFileStatus.Queued; nzbFile.NzbId = newNzb.Id; var segmentList = new List<NzbSegmentModel>(); //Get the Age of the File and Convert to DateTime var date = Convert.ToInt64((from d in file.Attributes("date") select d.Value).FirstOrDefault()); nzbFile.DatePosted = TicksToDateTime(date); //Get the Subject and set the NzbFile's Filename var subject = (from s in file.Attributes("subject") select s).FirstOrDefault(); int fileNameStart = subject.Value.IndexOf("\"") + 1; int fileNameEnd = subject.Value.LastIndexOf("\"") - fileNameStart; nzbFile.Filename = subject.Value.Substring(fileNameStart, fileNameEnd); //Get the groups for the NzbFile nzbFile.Groups = (from g in file.Descendants(ns + "group") select g.Value).ToList(); //Get the Segments for this file var segments = from s in file.Descendants(ns + "segment") select s; foreach (var segment in segments) { var nzbFileSegment = new NzbSegmentModel(); nzbFileSegment.Status = NzbSegmentStatus.Queued; nzbFileSegment.NzbFileName = nzbFile.Name; nzbFileSegment.Number = Convert.ToInt32((from n in segment.Attributes("number") select n.Value).FirstOrDefault()); nzbFileSegment.Size = Convert.ToInt64((from n in segment.Attributes("bytes") select n.Value).FirstOrDefault()); nzbFileSegment.SegmentId = segment.Value; segmentList.Add(nzbFileSegment); } nzbFile.Segments = segmentList; nzbFileList.Add(nzbFile); } newNzb.Files = nzbFileList; return newNzb; }