コード例 #1
0
ファイル: Program-CodeGen.cs プロジェクト: xsharper/xsharper
        private static Script genDownload(ScriptContext context, List<string> filteredArgs)
        {
            Script script;
            script = createEmptyScript(context,"xsharper //download");
            script.Id = "download";
            filteredArgs.AddRange(context.GetStringArray(xs.download));
            script.Usage.Options = UsageOptions.IfHelp | UsageOptions.IfNoArguments | UsageOptions.UsageLine | UsageOptions.AutoSuffix;
            script.Parameters.Add(new CommandLineParameter("uri", CommandLineValueCount.Single, null, null) { Required = true,  Value = "Source URL" });
            script.Parameters.Add(new CommandLineParameter("file", CommandLineValueCount.Single, ".",null) { Value = "Destination file or directory" });
            script.Parameters.Add(new CommandLineParameter(null, "cache", CommandLineValueCount.Single, Utils.LowercaseFirstLetter(RequestCacheLevel.Default.ToString()), null) { Description = "cache-level", Value = "Cache level, one of "+Utils.AllEnumValuesToString(typeof(RequestCacheLevel)," / ") });
            script.Parameters.Add(new CommandLineParameter("passive", "activeFtp", CommandLineValueCount.None, true, false) { Value = "Use active FTP" });
            script.Parameters.Add(new CommandLineParameter("userAgent","userAgent", CommandLineValueCount.Single, null,null) { Value = "User agent string (http://)" });
            script.Parameters.Add(new CommandLineParameter("post", "post", CommandLineValueCount.Single, null, null) { Value = "HTTP Post string (evaluated as XSharper expression)"});
            script.Parameters.Add(new CommandLineParameter("postContentType", "postContentType", CommandLineValueCount.Single, null, null) { Value = "HTTP Post content type (default is application/x-www-form-urlencoded)" });
            script.Parameters.Add(new CommandLineParameter("timeout", "timeout", CommandLineValueCount.Single, null, null) { Value = "Timeout" });
            script.Parameters.Add(new CommandLineParameter("ignoreCertificateErrors", "ignoreCertificateErrors", CommandLineValueCount.None, true, false) { Value = "Ignore SSL certificate errors " });

            script.Add(new Set("oldReceived", "0"));
            script.Add(new SetAttr("d","cacheLevel","${cache}"));
            script.Add(new SetAttr("d", "passiveFtp", "${passive}"));
            script.Add(new Set("fn", "${=XS.Download.UrlToLocalFilename($uri,$file)}",TransformRules.Expand));
            script.Add(new Print { Value = "Downloading ${=XS.Utils.SecureUri($uri)} => ${fn} ... "});
            If ifs1 = new If()  {   IsTrue = "${ignoreCertificateErrors}"};

            ifs1.Add(new Code("System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };") { Dynamic = true });
            script.Add(ifs1);
            Download d = new Download
                             {
                                 Id = "d",
                                 From="${uri}",
                                 To = "${file}",
                                 UserAgent = "${userAgent|=null}",
                                 Post = "${=.Expand(${post|=null})}",
                                 PostContentType = "${postContentType|=null}",
                                 Timeout = "${timeout|=null}",
                                 Transform = TransformRules.Expand
                             };

            If ifs = new If()
                {
                    Condition = "${= $.bytesReceived/1024/100 #GT# $oldReceived/1024/100}"
                };
            ifs.Add(new Print(".") { OutTo="^info" , NewLine = false});
            d.Add(ifs);

            d.Add(new Set("oldReceived","${= $.bytesReceived}",TransformRules.Expand));

            script.Add(d);
            script.Add(new Print { Value = "Completed. ${oldReceived} bytes downloaded." });
            return script;
        }