Esempio n. 1
0
        public void TemporaryTest()
        {
            var math = new LibCore();

            Assert.AreEqual(math.Area(new int[] { 5 }), 5 * 5 * Math.PI);
            Assert.AreEqual(math.Area(new int[] { 5, 4, 3 }), 6);

            var triangle = new Triangle()
            {
                A = 5, B = 4, C = 3
            };

            Assert.IsTrue(triangle.IsRightTriangle);

            triangle = new Triangle()
            {
                A = 5, B = 4, C = 2
            };
            Assert.IsFalse(triangle.IsRightTriangle);
        }
        /// <summary>
        /// Yahoo!天気・災害から最新地震情報を取得します。
        /// </summary>
        /// <param name="uri">URL</param>
        /// <returns></returns>
        public static async Task <NewEarthquakeInformation> GetNewEarthquakeInformationFromYahooAsync(Uri uri)
        {
            string source;

            try
            {
                source = await LibCore.DownloadString(uri);
            }
            catch (WebException)
            {
                return(null);
            }

            var doc = new HtmlDocument();

            doc.LoadHtml(source);
            var rows  = doc.DocumentNode.SelectNodes("//*[@id=\"eqinfdtl\"]/table[1]/tr");
            var datas = new Dictionary <string, string>();

            foreach (var node in rows)
            {
                var title = node.SelectSingleNode("td[1]/small");
                var data  = node.SelectSingleNode("td[2]/small");
                datas.Add(title.InnerText, data.InnerText.Trim());
            }

            var regionUrl = doc.DocumentNode.SelectSingleNode("//img[@alt=\"各地域の震度\"]");
            var detailUrl = doc.DocumentNode.SelectSingleNode("//img[@alt=\"全地点の震度\"]");
            var ints      = doc.DocumentNode.SelectSingleNode("//*[@id=\"eqinfdtl\"]/table[2]/tr[1]/td[1]/small");

            var latlon = datas["緯度/経度"].Replace("度", "").Split('/');

            var(lat, lon) = (latlon[0], latlon[1]);
            Location location = null;

            if (float.TryParse(lat.Substring(2), out var f))
            {
                location = new Location {
                    Latitude = f, Longitude = float.Parse(lon.Substring(2))
                };

                if (!lon.Contains("東経"))
                {
                    location.Longitude *= -1;
                }
                if (!lat.Contains("北緯"))
                {
                    location.Longitude *= -1;
                }
            }

            var rtn = new NewEarthquakeInformation(
                datas["発生時刻"], datas["震源地"], datas["マグニチュード"],
                ints?.InnerText, null, null, datas["深さ"],
                datas["情報"], detailUrl?.Attributes["src"].Value, regionUrl?.Attributes["src"].Value)
            {
                Info_url = new Uri("https://typhoon.yahoo.co.jp/weather/earthquake/"),
                Location = location
            };

            var match = new Regex(
                "<tr bgcolor=\"#ffffff\" valign=middle>.*?<td>.*?<a href=\"/weather/jp/earthquake(?<url>.*?)\">(?<time>.*?)</a>.*?</td>.*?<td align=center>(?<area>.*?)</td>.*?<td align=center>(?<mag>.*?)</td>.*?<td align=center>(?<sind>.*?)</td>.*?</tr>",
                RegexOptions.IgnoreCase | RegexOptions.Singleline).Match(source).NextMatch();
            var oldinfo = new List <OldEarthquakeInformation>();

            if (match.Success)
            {
                for (var i = 1; i < 9; i++)
                {
                    var epicenter = match.Groups["area"].Value;
                    var magnitude = match.Groups["mag"].Value;
                    var intensity = match.Groups["sind"].Value;

                    oldinfo.Add(new OldEarthquakeInformation(match.Groups["time"].Value, epicenter, magnitude,
                                                             intensity, "https://typhoon.yahoo.co.jp/weather/jp/earthquake" +
                                                             match.Groups["url"].Value));
                    match = match.NextMatch();
                }
            }

            rtn.Oldinfo = oldinfo.AsEnumerable();

            var htmlNode = doc.DocumentNode.SelectSingleNode(@"//table[@class=""yjw_table""]")
                           .ChildNodes.Where((a, i) => i % 2 == 1);

            rtn.Shindo = htmlNode.Select(t =>
            {
                var tag  = t.ChildNodes;
                var info = new EarthquakeInformation.ShindoInformation(tag[1].InnerText.Trim(),
                                                                       tag[3].ChildNodes[1].ChildNodes
                                                                       .Where((a, i) => i % 2 == 1)
                                                                       .Select(a => a.ChildNodes)
                                                                       .Select(a => new EarthquakeInformation.ShindoPlace
                                                                               (
                                                                                   a[1].InnerText.Trim(),
                                                                                   a[3].InnerText.Split('\n', ' ').Select(x => x.Trim())
                                                                                   .Where(x => !string.IsNullOrWhiteSpace(x) && !x.Contains('-'))
                                                                               )));
                return(info);
            });
            return(rtn);
        }
        /// <summary>
        /// Yahoo!天気・災害から地震情報を取得します。
        /// </summary>
        /// <param name="uri">URL</param>
        /// <returns></returns>
        public static async Task <EarthquakeInformation> GetEarthquakeInformationFromYahooAsync(Uri uri)
        {
            string source;

            try
            {
                source = await LibCore.DownloadString(uri);
            }
            catch (WebException)
            {
                return(null);
            }

            var doc = new HtmlDocument();

            doc.LoadHtml(source);
            var rows  = doc.DocumentNode.SelectNodes("//*[@id=\"eqinfdtl\"]/table[1]/tr");
            var datas = new Dictionary <string, string>();

            foreach (var node in rows)
            {
                var title = node.SelectSingleNode("td[1]/small");
                var data  = node.SelectSingleNode("td[2]/small");
                datas.Add(title.InnerText, data.InnerText.Trim());
            }

            var regionUrl = doc.DocumentNode.SelectSingleNode("//img[@alt=\"各地域の震度\"]");
            var detailUrl = doc.DocumentNode.SelectSingleNode("//img[@alt=\"全地点の震度\"]");
            var ints      = doc.DocumentNode.SelectSingleNode("//*[@id=\"eqinfdtl\"]/table[2]/tr[1]/td[1]/small");

            var latlon = datas["緯度/経度"].Replace("度", "").Split('/');

            var(lat, lon) = (latlon[0], latlon[1]);
            Location location = null;

            if (float.TryParse(lat.Substring(2), out var f))
            {
                location = new Location {
                    Latitude = f, Longitude = float.Parse(lon.Substring(2))
                };

                if (!lon.Contains("東経"))
                {
                    location.Longitude *= -1;
                }
                if (!lat.Contains("北緯"))
                {
                    location.Longitude *= -1;
                }
            }

            var rtn = new EarthquakeInformation(
                datas["発生時刻"], datas["震源地"], datas["マグニチュード"],
                ints?.InnerText, null, null, datas["深さ"],
                datas["情報"], detailUrl?.Attributes["src"].Value, regionUrl?.Attributes["src"].Value)
            {
                Info_url = new Uri("https://typhoon.yahoo.co.jp/weather/earthquake/"),
                Location = location
            };


            var htmlNode = doc.DocumentNode.SelectSingleNode(@"//table[@class=""yjw_table""]")
                           .ChildNodes.Where((a, i) => i % 2 == 1);

            rtn.Shindo = htmlNode.Select(t =>
            {
                var tag = t.ChildNodes;
                return(new EarthquakeInformation.ShindoInformation(tag[1].InnerText.Trim(),
                                                                   tag[3].ChildNodes[1]
                                                                   .ChildNodes
                                                                   .Where((a, i) => i % 2 == 1)
                                                                   .Select(a => a.ChildNodes)
                                                                   .Select(a => new EarthquakeInformation.ShindoPlace
                                                                           (
                                                                               a[1].InnerText.Trim(),
                                                                               a[3].InnerText.Split('\n', ' ')
                                                                               .Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x))
                                                                           ))));
            });
            return(rtn);
        }