示例#1
0
        public static string GetDescription(this GetGamesOptions opts)
        {
            StringBuilder sb = new StringBuilder();
            var           descriptiveOptions = opts.GetType().GetProperties().Where(x => Attribute.IsDefined(x, typeof(DescriptionAttribute)) && Attribute.IsDefined(x, typeof(OptionAttribute)));

            foreach (var opt in descriptiveOptions)
            {
                var desc = opt.GetPropertyDescription(opts);
                var val  = opt.GetPropertyValue(opts);
                sb.AppendLine($"{desc}: {val}");
            }
            return(sb.ToString());
        }
示例#2
0
        private static void GetLichessGames(GetGamesOptions opts)
        {
            try
            {
                var exportUri = baseUri + $"games/export/{opts.TargetUser}";
                //https://lichess.org/games/export/fptan?max=10
                StringBuilder queryString = new StringBuilder();

                var consoleDescription = $"Exporting games for {opts.TargetUser} with the following options:\r\n";
                consoleDescription += opts.GetDescription();
                Console.Write(consoleDescription);
                Console.WriteLine("Continue? y for Yes or any other character for no.");

                if (Char.ToLower(Console.ReadKey().KeyChar).Equals('y'))
                {
                    var fileText = string.Empty;
                    var url      = $"{exportUri}{opts.CreateQueryString()}";
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
                    var request = (HttpWebRequest)HttpWebRequest.Create(url);
                    request.Method = "GET";
                    if (!string.IsNullOrWhiteSpace(opts.AccessToken))
                    {
                        request.Headers.Add(HttpRequestHeader.Authorization, $"Bearer {opts.AccessToken}");
                    }
                    request.ProtocolVersion = HttpVersion.Version10;

                    var response = (HttpWebResponse)request.GetResponse();
                    //var responseEncoding = Encoding.GetEncoding(response.CharacterSet);

                    using (StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default))
                    {
                        fileText = sr.ReadToEnd();
                    }
                    File.WriteAllText(opts.OutFile, fileText);
                }
                if (Debugger.IsAttached)
                {
                    Console.ReadKey();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: " + e.Message);
                if (Debugger.IsAttached)
                {
                    Console.ReadKey();
                }
            }
        }
示例#3
0
        public static string CreateQueryString(this GetGamesOptions opts)
        {
            var qsOptions = new Dictionary <string, string>();

            foreach (var pInfo in opts.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(QueryStringDescriptionAttribute), false).Any()))
            {
                var value      = pInfo.GetValue(opts);
                var type       = pInfo.PropertyType;
                var defaultVal = type.GetDefault();
                if (!AreEqual(defaultVal, value, type))
                {
                    var attr = (QueryStringDescriptionAttribute[])pInfo.GetCustomAttributes(typeof(QueryStringDescriptionAttribute), false);
                    if (attr.Any())
                    {
                        qsOptions.Add(attr.First().QueryStringKey, value.ToString());
                    }
                }
            }

            return(qsOptions.CreateQueryString());
        }