Пример #1
0
        /// <summary>
        /// Create the string for Flags of the world mark down file and writes to console.
        /// </summary>
        public static void CreateFlagsReadMe()
        {
            Console.Clear();
            Console.WriteLine("<div>");
            string             pattern         = "<div class=\"flagcontainer\"><img src= \"https://raw.githubusercontent.com/sekhargullapalli/cia_factbook_parser/master/cia.factbook.data/country_flags/PATH\" width=\"125\" height =\"100\" title =\"TITLE\"><p>TITLE</p></div>";
            List <CountryData> CountryDataList =
                JsonConvert.DeserializeObject <List <CountryData> >(File.ReadAllText(@"countrydetailslist.json"));
            var files = new DirectoryInfo(@"C:\Users\sekha\Desktop\factbook\attachments\flags").EnumerateFiles("*.gif");

            foreach (var file in files)
            {
                string      filename = file.Name;
                CountryData data     = CountryDataList.Where(x => x.code == file.Name.Substring(0, 2)).FirstOrDefault();
                if (data != null && data.name != string.Empty)
                {
                    Console.WriteLine(pattern.Replace("PATH", filename).Replace("TITLE", data.name));
                }
            }
            Console.WriteLine("</div>");
            //Following css is used
            //< style type = "text/css" >
            //div.flagcontainer {
            //float: left;
            //margin: 10px;
            //max - width:120px;
            //height: 160px;
            //}
            //div p {
            //text - align: center;
            //}
            //</ style >
        }
        /// <summary>
        /// Collect data from factbook json folder into one json file
        /// </summary>
        public static void CreateCountryDataList()
        {
            TextWriter tw = new StreamWriter("countrydetailslist.json", false);

            try
            {
                List <CountryData> CountryDataList = new List <CountryData>();
                var dataFiles = new DirectoryInfo(@"C:\Users\sekha\Desktop\factbook\json").EnumerateFiles("*.json");
                int verified  = 0;
                foreach (var file in dataFiles)
                {
                    CountryData data = JsonConvert.DeserializeObject <CountryData>(File.ReadAllText(file.FullName));
                    Console.WriteLine(data);
                    if (data.code != string.Empty && data.code.Length == 2 && data.name != string.Empty && data.html != string.Empty)
                    {
                        CountryDataList.Add(data); verified++;
                    }
                }
                Console.WriteLine($"Total files processed: {dataFiles.Count()}");
                Console.WriteLine($"Total files verified: {verified}");
                string json = JsonConvert.SerializeObject(CountryDataList, Formatting.Indented);
                tw.WriteLine(json);
            }
            catch (Exception e) { Console.WriteLine(e.Message); }
            finally { tw.Close(); }
        }