Exemplo n.º 1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("called NHKCovid");

            var url = "https://www3.nhk.or.jp/n-data/opendata/coronavirus/nhk_news_covid19_prefectures_daily_data.csv";

            // CSV形式で取得
            try
            {
                var cl = new HttpClient();
                // 1行ずつ読み込み JSON 形式に変換
                var res = await cl.GetAsync(url);

                using (var st = new StreamReader(await res.Content.ReadAsStreamAsync()))
                {
                    // タイトルは読み飛ばし
                    st.ReadLine();
                    var data = new List <Covid>();
                    while (true)
                    {
                        string line = st.ReadLine();
                        if (string.IsNullOrEmpty(line))
                        {
                            break;
                        }
                        var items = line.Split(",");
                        if (items.Length >= 7)
                        {
                            var it = new Covid()
                            {
                                Date        = DateTime.Parse(items[0]),
                                LocationId  = int.Parse(items[1]),
                                Location    = items[2],
                                Cases       = int.Parse(items[3]),
                                CasesTotal  = int.Parse(items[3]),
                                Deaths      = int.Parse(items[3]),
                                DeathsTotal = int.Parse(items[3]),
                            };
                            data.Add(it);
                        }
                    }
                    // ソートしておく
                    data = data.OrderBy(t => t.LocationId).ThenBy(t => t.Date).ToList();
                    // 週平均を計算
                    calcCasesAve(data);
                    // 週単位Rt値を計算
                    calcCasesRt(data);
                    // 週単位Rt平均値を計算
                    calcCasesRtAve(data);

                    return(new OkObjectResult(new { result = data }));
                }
            }
            catch
            {
                return(new NotFoundResult());
            }
        }
Exemplo n.º 2
0
        public static async Task RunTimer([TimerTrigger("0 5 * * * *")] TimerInfo myTimer,
                                          [Blob("covid/japan.json", FileAccess.Write)] Stream jsonfile,
                                          ILogger log)

        {
            log.LogInformation("called NHKCovidTimer");
            var url = "https://www3.nhk.or.jp/n-data/opendata/coronavirus/nhk_news_covid19_prefectures_daily_data.csv";
            var cl  = new HttpClient();
            // 1行ずつ読み込み JSON 形式に変換
            var res = await cl.GetAsync(url);

            var data = new List <Covid>();

            using (var st = new StreamReader(await res.Content.ReadAsStreamAsync()))
            {
                // タイトルは読み飛ばし
                st.ReadLine();
                while (true)
                {
                    string line = st.ReadLine();
                    if (string.IsNullOrEmpty(line))
                    {
                        break;
                    }
                    var items = line.Split(",");
                    if (items.Length >= 7)
                    {
                        var it = new Covid()
                        {
                            Date        = DateTime.Parse(items[0]),
                            LocationId  = int.Parse(items[1]),
                            Location    = items[2],
                            Cases       = int.Parse(items[3]),
                            CasesTotal  = int.Parse(items[3]),
                            Deaths      = int.Parse(items[3]),
                            DeathsTotal = int.Parse(items[3]),
                        };
                        data.Add(it);
                    }
                }
                // ソートしておく
                data = data.OrderBy(t => t.LocationId).ThenBy(t => t.Date).ToList();
                // 週平均を計算
                calcCasesAve(data);
                // 週単位Rt値を計算
                calcCasesRt(data);
                // 週単位Rt平均値を計算
                calcCasesRtAve(data);
            }
            var json   = JsonConvert.SerializeObject(new { result = data });
            var writer = new StreamWriter(jsonfile);

            writer.Write(json);
            writer.Close();
            // return new OkObjectResult("save json " + DateTime.Now.ToString());
        }
Exemplo n.º 3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("called WorldCovid");
            var url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";

            // CSV形式で取得
            try
            {
                var cl = new HttpClient();
                // 1行ずつ読み込み JSON 形式に変換
                var res = await cl.GetAsync(url);

                var dates = new List <DateTime>();
                var data  = new List <Covid>();


                using (var st = new StreamReader(await res.Content.ReadAsStreamAsync()))
                {
                    // 1行目から日付を取得
                    //  5カラム目から日付
                    // 多分、抜けはないが念のため
                    string line    = st.ReadLine();
                    var    items   = line.Split(",");
                    var    culture = new CultureInfo("en-US");
                    for (int i = 4; i < items.Length; i++)
                    {
                        dates.Add(DateTime.Parse(items[i], culture));
                    }

                    // 2行目から国名と陽性者数
                    //  1カラム目が空白, 2カラム目に国名, 5カラム目から陽性者数
                    while (true)
                    {
                        line = st.ReadLine();
                        if (string.IsNullOrEmpty(line))
                        {
                            break;
                        }
                        // 国名を変換しておく。カンマ抜きにする
                        // "Korea, South"
                        // "Bonaire, Sint Eustatius and Saba"
                        line  = line.Replace("\"Korea, South\"", "Korea South");
                        line  = line.Replace("\"Bonaire, Sint Eustatius and Saba\"", "Bonaire Sint Eustatius and Saba");
                        items = line.Split(",");
                        if (items.Length >= dates.Count + 4)
                        {
                            if (items[0] != "")
                            {
                                continue;
                            }
                            int    pre      = 0;
                            string location = items[1];

                            for (int i = 5; i < items.Length; i++)
                            {
                                try
                                {
                                    var date  = dates[i - 4];
                                    int cases = int.Parse(items[i]) - pre;
                                    pre = int.Parse(items[i]);
                                    var it = new Covid()
                                    {
                                        Date     = date,
                                        Location = location,
                                        Cases    = cases,
                                    };
                                    data.Add(it);
                                }
                                catch (Exception ex)
                                {
                                    log.LogError(ex.Message);
                                }
                            }
                        }
                    }
                    // ソートしておく
                    data = data.OrderBy(t => t.Location).ThenBy(t => t.Date).ToList();
                    // 週平均を計算
                    NHKCovid.calcCasesAve(data);
                    // 週単位Rt値を計算
                    NHKCovid.calcCasesRt(data);
                    // 週単位Rt平均値を計算
                    NHKCovid.calcCasesRtAve(data);

                    return(new OkObjectResult(new { result = data }));
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                return(new NotFoundResult());
            }
        }
Exemplo n.º 4
0
        public static async Task RunTimer([TimerTrigger("0 5 * * * *")] TimerInfo myTimer,
                                          [Blob("covid/world.json", FileAccess.Write)] Stream jsonfile,
                                          ILogger log)

        {
            log.LogInformation("called WorldCovidTimer");
            var url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";

            var cl = new HttpClient();
            // 1行ずつ読み込み JSON 形式に変換
            var res = await cl.GetAsync(url);

            var dates = new List <DateTime>();
            var data  = new List <Covid>();


            using (var st = new StreamReader(await res.Content.ReadAsStreamAsync()))
            {
                // 1行目から日付を取得
                //  5カラム目から日付
                // 多分、抜けはないが念のため
                string line    = st.ReadLine();
                var    items   = line.Split(",");
                var    culture = new CultureInfo("en-US");
                for (int i = 4; i < items.Length; i++)
                {
                    dates.Add(DateTime.Parse(items[i], culture));
                }

                // 2行目から国名と陽性者数
                //  1カラム目が空白, 2カラム目に国名, 5カラム目から陽性者数
                while (true)
                {
                    line = st.ReadLine();
                    if (string.IsNullOrEmpty(line))
                    {
                        break;
                    }
                    // 国名を変換しておく。カンマ抜きにする
                    // "Korea, South"
                    // "Bonaire, Sint Eustatius and Saba"
                    line  = line.Replace("\"Korea, South\"", "Korea South");
                    line  = line.Replace("\"Bonaire, Sint Eustatius and Saba\"", "Bonaire Sint Eustatius and Saba");
                    items = line.Split(",");
                    if (items.Length >= dates.Count + 4)
                    {
                        if (items[0] != "")
                        {
                            continue;
                        }
                        int    pre      = 0;
                        string location = items[1];

                        for (int i = 5; i < items.Length; i++)
                        {
                            try
                            {
                                var date  = dates[i - 4];
                                int cases = int.Parse(items[i]) - pre;
                                pre = int.Parse(items[i]);
                                var it = new Covid()
                                {
                                    Date     = date,
                                    Location = location,
                                    Cases    = cases,
                                };
                                data.Add(it);
                            }
                            catch (Exception ex)
                            {
                                log.LogError(ex.Message);
                            }
                        }
                    }
                }
                // ソートしておく
                data = data.OrderBy(t => t.Location).ThenBy(t => t.Date).ToList();
                // 週平均を計算
                NHKCovid.calcCasesAve(data);
                // 週単位Rt値を計算
                NHKCovid.calcCasesRt(data);
                // 週単位Rt平均値を計算
                NHKCovid.calcCasesRtAve(data);
            }
            var json   = JsonConvert.SerializeObject(new { result = data });
            var writer = new StreamWriter(jsonfile);

            writer.Write(json);
            writer.Close();
            // return new OkObjectResult("save json " + DateTime.Now.ToString());
        }