コード例 #1
0
        public string GetWeatherInCity(string cityName)
        {
            string request = String.Concat(get_current_weather.Replace("CURRENT_CITY", cityName), "&appid=", api_key);

            JObject o = JObject.Parse(HTTPWorker.Get(request));
            var     m = o.GetValue("message");

            if (m == null)
            {
                string mainDescription = o.GetValue("weather").ToArray()[0].ToObject <JObject>().GetValue("main").ToString();
                string iconName        = o.GetValue("weather").ToArray()[0].ToObject <JObject>().GetValue("icon").ToString();

                var mainParams = o.GetValue("main").ToObject <JObject>();

                string realTemp      = mainParams.GetValue("temp").ToString();
                string feelsLikeTemp = mainParams.GetValue("feels_like").ToString();
                string minInCity     = mainParams.GetValue("temp_min").ToString();
                string maxInCity     = mainParams.GetValue("temp_max").ToString();
                string pressure      = mainParams.GetValue("pressure").ToString();
                string humidity      = mainParams.GetValue("humidity").ToString();
                string windSpeed     = o.GetValue("wind").ToObject <JObject>().GetValue("speed").ToString();

                return(String.Concat($"The weather in {cityName}:\n",
                                     $"It is {mainDescription}, \n",
                                     $"the temperature is {realTemp} degrees Celsius.\nBut it feels like {feelsLikeTemp}.\n",
                                     $"The minimum temperature in city is {minInCity}. \nand maximum is {maxInCity}. \n",
                                     $"Current pressure is {pressure}. \n",
                                     $"And humidity is {humidity}%.\n",
                                     $"The speed of wind is about {windSpeed} m/s ",
                                     get_weather_icon.Replace("ICON_ID", iconName)));
            }

            return("City not found. Are you sure it's spelled like this?");
        }
コード例 #2
0
        public string GetRandomPopularArticle()
        {
            JObject o          = JObject.Parse(HTTPWorker.Get(FromRequest()));
            var     resultsNum = o.GetValue("page_size");


            Random rd  = new Random();
            int    num = rd.Next(0, Int32.Parse(resultsNum.ToString()));

            JObject article = o.GetValue("articles").ToArray()[num].ToObject <JObject>();

            string title        = article.GetValue("title").ToString();
            string abstractDesc = article.GetValue("summary").ToString();
            var    media        = article.GetValue("media");
            string imgUrl       = "";

            if (media != null)
            {
                try
                {
                    imgUrl = media.ToString();
                }
                catch (Exception e)
                { }
            }
            string articleUrl = article.GetValue("link").ToString();


            OGTag oGTag = new OGTag()
            {
                Title       = title,
                Description = abstractDesc,
                Image       = imgUrl,
                Url         = articleUrl
            };

            // Old version
            //String.Concat(title, ". \n", abstractDesc, " ", imgUrl, " ", articleUrl)

            return(JsonSerializer.Serialize(oGTag));
        }