Exemple #1
0
        public async Task <StateReport> ExecuteScrapeAsync()
        {
            List <CountyReport> countyReports = new List <CountyReport>(250);

            HttpClient client = new HttpClient();

            using (Stream file = await client.GetStreamAsync(URI).ConfigureAwait(false))
            {
                var    wb    = new XSSFWorkbook(file);
                ISheet sheet = wb.GetSheetAt(0);

                if (sheet != null)
                {
                    int rowCount = sheet.LastRowNum;

                    for (int i = 1; i <= rowCount; i++)
                    {
                        IRow curRow = sheet.GetRow(i);

                        if (curRow == null)
                        {
                            // Valid row count
                            rowCount = i - 1;
                            break;
                        }

                        string name   = curRow.GetCell(0).StringCellValue.Trim();
                        string status = curRow.GetCell(1).StringCellValue.Trim();
                        int    cases  = (int)curRow.GetCell(2).NumericCellValue;
                        int    deaths = (int)curRow.GetCell(3).NumericCellValue;

                        var countyReport = countyReports.FirstOrDefault(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));

                        if (countyReport == null)
                        {
                            countyReport = new CountyReport(
                                name: name,
                                confirmed: -1,
                                probable: -1,
                                deaths: deaths,
                                hospitalizations: -1,
                                rate: -1);
                        }

                        if (status == "Confirmed")
                        {
                            countyReport.Confirmed = cases;
                        }
                        else if (status == "Probable")
                        {
                            countyReport.Probable = cases;
                        }

                        countyReports.Add(countyReport);
                    }
                }
            }

            return(new StateReport(state: "Michigan", abbreviation: "MI", reports: countyReports));
        }
        public async Task <StateReport> ExecuteScrapeAsync()
        {
            List <CountyReport> countyReports = new List <CountyReport>(250);

            HttpClient client = new HttpClient();

            using (var file = await client.GetStreamAsync(URI).ConfigureAwait(false))
                using (var memoryStream = new MemoryStream())
                {
                    await file.CopyToAsync(memoryStream);

                    using (ZipArchive z = new ZipArchive(memoryStream))
                    {
                        ZipArchiveEntry zEntry = z.Entries.FirstOrDefault(e => e.Name.Contains("countycases.csv"));

                        using (Stream csvStream = zEntry.Open())
                            using (StreamReader reader = new StreamReader(csvStream))
                            {
                                bool headersLine = true;
                                while (!reader.EndOfStream)
                                {
                                    string line  = reader.ReadLine();
                                    var    cells = line.Split(',');

                                    if (headersLine)
                                    {
                                        headersLine = false;
                                    }
                                    else
                                    {
                                        string name             = cells[0];
                                        int    confirmed        = int.Parse(cells[1]);
                                        int    deaths           = int.Parse(cells[2]);
                                        int    hospitalizations = int.Parse(cells[3]);
                                        double rate             = double.Parse(cells[4]);

                                        CountyReport report = new CountyReport(
                                            name: name,
                                            confirmed: confirmed,
                                            probable: -1,
                                            deaths: deaths,
                                            hospitalizations: hospitalizations,
                                            rate: rate);

                                        countyReports.Add(report);
                                    }
                                }
                            }
                    }
                }

            return(new StateReport(state: "Georgia", abbreviation: "GA", reports: countyReports));
        }
        private static void AddCounty(List <CountyReport> countyReports, ArrayList countyData)
        {
            string name             = (string)countyData[0];
            int    confirmed        = (int)int.Parse((string)countyData[1]);
            int    deaths           = (int)int.Parse((string)countyData[2]);
            int    hospitalizations = (int)int.Parse((string)countyData[3]);
            double rate             = -1;

            CountyReport report = new CountyReport(
                name: name,
                confirmed: confirmed,
                probable: -1,
                deaths: deaths,
                hospitalizations: hospitalizations,
                rate: rate);

            countyReports.Add(report);
        }