Exemplo n.º 1
0
        /// <summary>
        /// このクラスでの実行すること。
        /// </summary>
        /// <param name="runChildren"></param>
        public override void Run(bool runChildren)
        {
            if (Folder == null)
            {
                ReportManage.ErrEmptyPropertyName(this, nameof(Folder));
                return;
            }
            Folder.Parent = this;
            var client = this.GetAncestorRawler().OfType <WebClient>().DefaultIfEmpty(new WebClient()).FirstOrDefault();

            string url = Url;

            if (string.IsNullOrEmpty(url))
            {
                url = GetText();
            }
            var    page    = this.GetUpperRawler <Page>();
            string referer = string.Empty;

            if (page != null)
            {
                referer = page.GetCurrentUrl();
            }

            var data = client.HttpGetByte(url, referer);

            data.Wait();
            string fileName = string.Empty;

            if (string.IsNullOrEmpty(SaveName) == false)
            {
                fileName = SaveName.Convert(this);
            }
            else
            {
                fileName = System.IO.Path.GetFileNameWithoutExtension(url);
            }
            string ex = System.IO.Path.GetExtension(url);

            if (ex.Split('?').Length > 0)
            {
                ex = ex.Split('?').First();
            }

            Folder?.WriteFile(fileName + ex, data.Result);

            SetText(fileName + ex);

            base.Run(runChildren);
        }
Exemplo n.º 2
0
        /// <summary>
        /// このクラスでの実行すること。
        /// </summary>
        /// <param name="runChildren"></param>
        public override void Run(bool runChildren)
        {
            var client = this.GetAncestorRawler().OfType <WebClient>().DefaultIfEmpty(new WebClient()).FirstOrDefault();

            string url = Url;

            if (string.IsNullOrEmpty(url))
            {
                url = GetText();
            }
            var page = this.GetUpperRawler <Page>();

            if (page != null)
            {
                client.Referer = page.GetCurrentUrl();
            }

            var    data = client.HttpGetByte(url);
            string path = string.Empty;

            if (FolderNameTree != null)
            {
                path = RawlerBase.GetText(this.Parent.Text, FolderNameTree, this.Parent);
            }
            else
            {
                path = Folder.Convert(this);
            }
            if (path != null)
            {
                if (System.IO.Path.IsPathRooted(path) == false)
                {
                    path = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), path);
                }

                if (System.IO.Directory.Exists(path) == false)
                {
                    try
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                    catch (Exception e)
                    {
                        ReportManage.ErrReport(this, "ディレクトリーの作成に失敗しました" + e.Message);
                    }
                }
            }
            else
            {
                path = System.IO.Directory.GetCurrentDirectory();
            }
            string ex = System.IO.Path.GetExtension(url);

            if (ex.Split('?').Length > 0)
            {
                ex = ex.Split('?').First();
            }
            string fileName = string.Empty;

            if (SaveNameTree != null)
            {
                fileName = RawlerBase.GetText(this.Parent.Text, SaveNameTree, this.Parent);
            }
            else if (string.IsNullOrEmpty(SaveName) == false)
            {
                fileName = SaveName.Convert(this);
            }
            else
            {
                fileName = System.IO.Path.GetFileNameWithoutExtension(url);
            }
            path = System.IO.Path.Combine(path, fileName + ex);

            try
            {
                using (var writer = System.IO.File.Create(path))
                {
                    writer.Write(data, 0, data.Length);
                    SetText(path);
                }
            }
            catch (Exception e)
            {
                ReportManage.ErrReport(this, "ファイルの保存に失敗しました" + e.Message + " Path:" + path);
            }
            SetText(fileName + ex);

            base.Run(runChildren);
        }