Example #1
0
 public async Task GetIp()
 {
     if (await NetUtills.GetJsonUrl("https://api.ipify.org/?format=json") is JObject res)
     {
         await ReplyAsync(res["ip"].ToString());
     }
 }
Example #2
0
        public static async Task <string> GetWeatherCommand(string city)
        {
            if (_weatherToken == null)
            {
                return("Module didn't init");
            }
            var uriBuilder = new UriBuilder(WeatherUrl);
            var query      = HttpUtility.ParseQueryString(uriBuilder.Query);

            query["q"]       = city;
            query["appid"]   = _weatherToken;
            query["units"]   = "metric";
            uriBuilder.Query = query.ToString();
            var url = uriBuilder.ToString();

            if (!(await NetUtills.GetJsonUrl(url) is JObject res))
            {
                return("Couldn't get weather");
            }
            if (!res.ContainsKey("main"))
            {
                return("Couldn't get weather");
            }

            var tempMin = res["main"]["temp_min"].ToString();
            var tempMax = res["main"]["temp_max"].ToString();
            var desc    = res["weather"][0]["description"].ToString();

            return($"Weather for {city}\n{tempMin}°/{tempMax}° - {desc}");
        }
Example #3
0
        public static async Task <GroupPost> GetNewPost(GroupFeeder.Group group)
        {
            var url = Vkapiurl + $"wall.get?count=2&owner_id=-{group.Id}&v=5.84&access_token={_token}";

            var  id      = "";
            var  text    = "";
            var  picture = "";
            long date    = 0;

            try
            {
                var response = ((JObject)await NetUtills.GetJsonUrl(url))["response"];

                var neededObj = response["items"].Contains("is_pinned")
                    ? (JObject)response["items"][1]
                    : (JObject)response["items"][0];

                id   = neededObj["id"].ToString();
                text = neededObj["text"].ToString();

                date = long.Parse(neededObj["date"].ToString());

                picture = GetPicture(neededObj);
            }

            catch (Exception e)
            {
                Logger.Error($"Can't load group info {group.Name}: {e}");
            }

            return(new GroupPost(id, text, picture, date));
        }
Example #4
0
        public static async Task <List <AnimeInfo> > GetAnime()
        {
            var list = new List <AnimeInfo>();

            if (!(await NetUtills.GetJsonUrl(Url) is JArray array))
            {
                return(list);
            }
            list.AddRange(array.Select(entity => new AnimeInfo(entity)));

            return(list);
        }
Example #5
0
        public static async Task <List <PostComment> > GetCommentsFromPost(GroupFeeder.Group group, string postId)
        {
            var url = Vkapiurl +
                      $"wall.getComments?count=100&need_likes=1&owner_id=-{group.Id}&post_id={postId}&v=5.84&access_token={_token}";

            var comments = new List <PostComment>();

            try
            {
                if (!(await NetUtills.GetJsonUrl(url) is JObject data))
                {
                    return(null);
                }
                var count = data["response"]["count"].ToObject <int>();
                var items = data["response"]["items"];

                comments.AddRange(from JObject item in items
                                  let likes                                                  = item["likes"]["count"].ToObject <int>()
                                                                  let text                   = item["text"].ToString()
                                                                                      let id = item["id"].ToString()
                                                                                               let picture = GetPicture(item)
                                                                                                             where !text.Equals("") || !picture.Equals("")
                                                                                                             select new PostComment(likes, text, picture, id));

                if (count < 100)
                {
                    return(comments);
                }
                var k = 100;
                Console.WriteLine(items.ToString());
                while (k < count)
                {
                    var innerUrl =
                        Vkapiurl +
                        $"wall.getComments?count=100&offset={k.ToString()}&need_likes=1&owner_id=-{group.Id}&post_id={postId}&v=5.84&access_token={_token}";
                    data = await NetUtills.GetJsonUrl(innerUrl) as JObject;

                    if (data != null)
                    {
                        items = data["response"]["items"];
                    }

                    comments.AddRange(from JObject item in items
                                      let likes                                                  = item["likes"]["count"].ToObject <int>()
                                                                      let text                   = item["text"].ToString()
                                                                                          let id = item["id"].ToString()
                                                                                                   let picture = GetPicture(item)
                                                                                                                 where !text.Equals("") || !picture.Equals("")
                                                                                                                 select new PostComment(likes, text, picture, id));

                    k += 100;
                    await Task.Delay(500);
                }

                return(comments);
            }
            catch (Exception e)
            {
                Logger.Error($"Can't load comments info {group.Name}: {e}");
                return(null);
            }
        }