Exemplo n.º 1
0
        public static string GetNovelHeader(HtmlDocument doc, Constants.FileType fileType)
        {
            HtmlNode headerNode = doc.DocumentNode.SelectSingleNode("//div[@id='novel_p']");

            if (headerNode == null)
            {
                return(String.Empty);
            }

            if (fileType == Constants.FileType.Text)
            {
                string s = headerNode.InnerText.Trim(Environment.NewLine.ToCharArray());
                s += Environment.NewLine + Environment.NewLine + "=====" + Environment.NewLine + Environment.NewLine;
                return(s);
            }
            else if (fileType == Constants.FileType.HTML)
            {
                string[] s = headerNode.InnerText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

                StringBuilder sb = new StringBuilder();
                foreach (String str in s)
                {
                    string temp = (str != "") ? ("<p>" + str + "</p>") : ("<p><br/></p>");
                    sb.AppendLine(temp);
                }

                sb.AppendLine("<hr/>");

                return(sb.ToString());
            }
            else
            {
                return(String.Empty);
            }
        }
Exemplo n.º 2
0
        public static string GetNovelBody(HtmlDocument doc, Constants.FileType fileType)
        {
            HtmlNode novelNode  = doc.DocumentNode.SelectSingleNode("//div[@id='novel_honbun']");
            HtmlNode footerNode = doc.DocumentNode.SelectSingleNode("//div[@id='novel_a']");

            if (fileType == Constants.FileType.Text)
            {
                string s = novelNode.InnerText;
                if (footerNode != null)
                {
                    s += Environment.NewLine + "=====" + Environment.NewLine;
                    s += footerNode.InnerText;
                }

                return(s);
            }
            else if (fileType == Constants.FileType.HTML)
            {
                string[] s = novelNode.InnerText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

                StringBuilder sb = new StringBuilder();
                foreach (String str in s)
                {
                    string temp = (str != "") ? ("<p>" + str + "</p>") : ("<p><br/></p>");
                    sb.AppendLine(temp);
                }

                if (footerNode != null)
                {
                    sb.AppendLine("<hr/>");

                    s = footerNode.InnerText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                    foreach (String str in s)
                    {
                        string temp = (str != "") ? ("<p>" + str + "</p>") : ("<p><br/></p>");
                        sb.AppendLine(temp);
                    }
                }

                return(sb.ToString());
            }
            return(String.Empty);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Download a file
        /// </summary>
        /// <param name="fileName">The name of the file to download</param>
        /// <param name="fileUri">Path to the file</param>
        /// <param name="fileType">The filetype</param>
        /// <param name="ct">The cancellation token</param>
        private static async Task <Tuple <string, Exception> > DownloadFileAsync(string fileName, Uri fileUri,
                                                                                 Constants.FileType fileType, CancellationToken ct)
        {
            if (fileUri != null)
            {
                string pathDirectory = String.Empty;
                string extension     = String.Empty;
                switch (fileType)
                {
                case Constants.FileType.BackgroundImage:
                    pathDirectory = Constants.BackgroundMovieDirectory;
                    extension     = Constants.ImageFileExtension;
                    break;

                case Constants.FileType.CoverImage:
                    pathDirectory = Constants.CoverMoviesDirectory;
                    extension     = Constants.ImageFileExtension;
                    break;

                case Constants.FileType.PosterImage:
                    pathDirectory = Constants.PosterMovieDirectory;
                    extension     = Constants.ImageFileExtension;
                    break;

                case Constants.FileType.DirectorImage:
                    pathDirectory = Constants.DirectorMovieDirectory;
                    extension     = Constants.ImageFileExtension;
                    break;

                case Constants.FileType.ActorImage:
                    pathDirectory = Constants.ActorMovieDirectory;
                    extension     = Constants.ImageFileExtension;
                    break;

                case Constants.FileType.TorrentFile:
                    pathDirectory = Constants.TorrentDirectory;
                    extension     = Constants.TorrentFileExtension;
                    break;

                default:
                    return(new Tuple <string, Exception>(fileName, new Exception()));
                }
                string downloadToDirectory = pathDirectory + fileName + extension;


                if (!Directory.Exists(pathDirectory))
                {
                    try
                    {
                        Directory.CreateDirectory(pathDirectory);
                    }
                    catch (Exception e)
                    {
                        return(new Tuple <string, Exception>(fileName, e));
                    }
                }

                using (var webClient = new NoKeepAliveWebClient())
                {
                    ct.Register(webClient.CancelAsync);
                    if (!File.Exists(downloadToDirectory))
                    {
                        try
                        {
                            await webClient.DownloadFileTaskAsync(fileUri,
                                                                  downloadToDirectory);

                            try
                            {
                                FileInfo fi = new FileInfo(downloadToDirectory);
                                if (fi.Length == 0)
                                {
                                    return(new Tuple <string, Exception>(fileName, new Exception()));
                                }
                            }
                            catch (Exception e)
                            {
                                return(new Tuple <string, Exception>(fileName, e));
                            }
                        }
                        catch (WebException e)
                        {
                            return(new Tuple <string, Exception>(fileName, e));
                        }
                        catch (Exception e)
                        {
                            return(new Tuple <string, Exception>(fileName, e));
                        }
                    }
                    else
                    {
                        try
                        {
                            FileInfo fi = new FileInfo(downloadToDirectory);
                            if (fi.Length == 0)
                            {
                                try
                                {
                                    File.Delete(downloadToDirectory);
                                    try
                                    {
                                        await
                                        webClient.DownloadFileTaskAsync(fileUri, downloadToDirectory);

                                        FileInfo newfi = new FileInfo(downloadToDirectory);
                                        if (newfi.Length == 0)
                                        {
                                            return(new Tuple <string, Exception>(fileName, new Exception()));
                                        }
                                    }
                                    catch (WebException e)
                                    {
                                        return(new Tuple <string, Exception>(fileName, e));
                                    }
                                    catch (Exception e)
                                    {
                                        return(new Tuple <string, Exception>(fileName, e));
                                    }
                                }
                                catch (Exception e)
                                {
                                    return(new Tuple <string, Exception>(fileName, e));
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            return(new Tuple <string, Exception>(fileName, e));
                        }
                    }
                }

                return(new Tuple <string, Exception>(fileName, null));
            }

            return(new Tuple <string, Exception>(fileName, new Exception()));
        }