Exemple #1
0
        /// <summary>
        /// tries to get the password from nzb file
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string GetNzbPassword(string filePath)
        {
            string ret = null;

            try
            {
                using (System.IO.FileStream fsNzb = File.OpenRead(filePath))
                {
                    Task <NzbDocument> tDoc = NzbDocument.Load(fsNzb);
                    tDoc.Wait();
                    NzbDocument nDoc = tDoc.Result;

                    if (nDoc.Metadata.Any(a => a.Key.Equals("password", StringComparison.OrdinalIgnoreCase)))
                    {
                        ret = nDoc.Metadata.First(f => f.Key.Equals("password", StringComparison.OrdinalIgnoreCase)).Value;
                    }
                }
            }
            catch { }

            if (!string.IsNullOrWhiteSpace(ret))
            {
                return(ret);
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Initializes the queue with an NZB, a directory to store temp files, and a directory to store completed files.
        /// </summary>
        /// <param name="nzb">NzbDocument to process</param>
        /// <param name="cachePath">Location to store file segments until they can be assembled</param>
        /// <param name="completedPath">Location to store completed files.</param>
        public DownloadQueue(NzbDocument nzb, string cachePath, string completedPath)
        {
            CacheDirectory = new DirectoryInfo(Path.Combine(cachePath, nzb.Name));
            if (!CacheDirectory.Exists)
            {
                CacheDirectory.Create();
            }

            CompletedDirectory = new DirectoryInfo(Path.Combine(completedPath, nzb.Name));
            if (!CompletedDirectory.Exists)
            {
                CompletedDirectory.Create();
            }

            queue = nzb.Files.SelectMany(
                f => f.Segments,
                (f, s) => new Job {
                FileID    = f.GetHashCode(),
                Number    = s.Number,
                Total     = f.Segments.Count(),
                ArticleId = s.ArticleId,
                Status    = JobStatus.Queued
            }
                ).ToList();

            assembled = queue.GroupBy(q => q.FileID).Select(g => g.Key).ToDictionary(k => k, v => false);
        }
 public NzbDocumentWrapper(string name, string path, IEnumerable <byte> buffer, NzbDocument document)
 {
     this.Path     = path;
     this.Document = document;
     this.Buffer   = buffer;
     this.Name     = name;
 }
        public void MinimalNzbDataShouldBeParsed()
        {
            const string nzbText        = @"<nzb xmlns=""http://www.newzbin.com/DTD/2003/nzb""></nzb>";
            NzbDocument  actualDocument = NzbParser.Parse(nzbText);

            Assert.Empty(actualDocument.MetaData);
            Assert.Empty(actualDocument.Files);
        }
        public void ValidNzbDataShouldBeParsed(string fileName)
        {
            string      nzbData        = testData.GetEmbeddedFile(fileName).ReadAllText(UsenetEncoding.Default);
            NzbDocument actualDocument = NzbParser.Parse(nzbData);

            Assert.Equal("Your File!", actualDocument.MetaData["title"].Single());
            Assert.Equal("secret", actualDocument.MetaData["password"].Single());
            Assert.Equal("HD", actualDocument.MetaData["tag"].Single());
            Assert.Equal("TV", actualDocument.MetaData["category"].Single());
            Assert.Equal(106895, actualDocument.Size);
        }
        public void FileShouldBeExtractedFromSubjectWhenNotQuotedAndNoParenthesis()
        {
            const string nzbText = @"
<nzb xmlns=""http://www.newzbin.com/DTD/2003/nzb"">
  <file subject=""[2 / 9] - TWD151 - 153.rar yEnc""></file>
</nzb>";

            NzbDocument actualDocument = NzbParser.Parse(nzbText);

            Assert.Equal("[2 / 9] - TWD151 - 153.rar", actualDocument.Files.Single().FileName);
        }
        public void FileShouldBeExtractedFromSubjectWhenQuoted()
        {
            const string nzbText = @"
<nzb xmlns=""http://www.newzbin.com/DTD/2003/nzb"">
  <file subject=""(TWD151 - 153)[2 / 9] - &quot;TWD151 - 153.rar&quot; yEnc (001 / 249)""></file>
</nzb>";

            NzbDocument actualDocument = NzbParser.Parse(nzbText);

            Assert.Equal("TWD151 - 153.rar", actualDocument.Files.Single().FileName);
        }
Exemple #8
0
        private static NzbDocument GetNzbDocument(byte[] contents)
        {
            NzbDocument retour = null;

            using (var file = new BufferedStream(new MemoryStream(contents)))
            {
                retour = NzbDocument.Load(file).Result;
            }

            return(retour);
        }
        public void FileDateShouldBeParsed()
        {
            DateTimeOffset expected = DateTimeOffset.Parse(@"2017-06-01T06:49:13+00:00");
            const string   nzbText  = @"
<nzb xmlns=""http://www.newzbin.com/DTD/2003/nzb"">
  <file date=""1496299753""></file>
</nzb>";

            NzbDocument actualDocument = NzbParser.Parse(nzbText);

            Assert.Equal(expected, actualDocument.Files[0].Date);
        }
Exemple #10
0
        private static void TestDownloadNzbStreaming(NntpClient client, string nzbFileName)
        {
            string fullPath = Path.Combine(nzbFileName);
            string nzbData = File.ReadAllText(fullPath, UsenetEncoding.Default);
            NzbDocument nzbDocument = NzbParser.Parse(nzbData);

            string downloadDir = Path.Combine("downloads", nzbFileName);
            Directory.CreateDirectory(downloadDir);

            var sw = new Stopwatch();
            sw.Restart();

            log.LogInformation("Downloading nzb {nzbFileName}", nzbFileName);
            foreach (NzbFile file in nzbDocument.Files)
            {
                log.LogInformation("Downloading file {subject}", file.Subject);
                foreach (NzbSegment segment in file.Segments)
                {
                    log.LogInformation("Downloading article {messageId}", segment.MessageId);
                    NntpArticleResponse response = client.Article(segment.MessageId);
                    using (YencStream yencStream = YencStreamDecoder.Decode(response.Article.Body))
                    {
                        YencHeader header = yencStream.Header;

                        string fileName = Path.Combine(downloadDir, header.FileName);
                        if (!File.Exists(fileName))
                        {
                            log.LogInformation("Creating file {fileName}", fileName);
                            // create file and pre-allocate disk space for it
                            using (FileStream stream = File.Create(fileName))
                            {
                                stream.SetLength(header.FileSize);
                            }
                        }

                        log.LogInformation("Writing {size} bytes to file {fileName} at offset {offset}",
                            header.PartSize, fileName, header.PartOffset);

                        using (FileStream stream = File.Open(
                            fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
                        {
                            stream.Seek(header.PartOffset, SeekOrigin.Begin);
                            yencStream.CopyTo(stream);
                        }
                    }
                }
            }
            log.LogInformation("Nzb downloaded in {elapsed}", sw.Elapsed);
        }
Exemple #11
0
        public void MultipleMetaDataKeysShouldBeParsed()
        {
            const string nzbText        = @"
<nzb xmlns=""http://www.newzbin.com/DTD/2003/nzb"">
  <head>
    <meta type=""tag"">SD</meta>
    <meta type=""tag"">avi</meta>
  </head>
</nzb>";
            NzbDocument  actualDocument = NzbParser.Parse(nzbText);

            Assert.Single(actualDocument.MetaData);
            Assert.Equal(2, actualDocument.MetaData["tag"].Count);
            Assert.NotNull(actualDocument.MetaData["tag"].SingleOrDefault(m => m == "SD"));
            Assert.NotNull(actualDocument.MetaData["tag"].SingleOrDefault(m => m == "avi"));
        }
Exemple #12
0
        public void ShouldWriteDocumentToFile(string fileName)
        {
            IFileInfo   file     = testData.GetEmbeddedFile(fileName);
            NzbDocument expected = NzbParser.Parse(file.ReadAllText(UsenetEncoding.Default));

            using var stream = new MemoryStream();
            using var writer = new StreamWriter(stream, UsenetEncoding.Default);
            using var reader = new StreamReader(stream, UsenetEncoding.Default);

            // write to file and read back for comparison
            writer.WriteNzbDocument(expected);
            stream.Position = 0;
            NzbDocument actual = NzbParser.Parse(reader.ReadToEnd());

            // compare
            Assert.Equal(expected, actual);
        }
Exemple #13
0
        private static string GetKeyName(string nzbFullPath, NzbDocument document)
        {
            var retour = Path.Combine(Path.GetFileNameWithoutExtension(nzbFullPath), ".nzb");

            if (document.Files.Any())
            {
                var nzb     = document.Files.First();
                var pattern = @"""(.*?)\.\w+""";
                var match   = Regex.Match(nzb.Subject, pattern);
                if (match.Success)
                {
                    retour = match.Groups[1].Value;
                }

                return(retour);
            }

            return(retour);
        }