示例#1
0
文件: VkApi.cs 项目: he305/sharpbot
        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));
        }
示例#2
0
文件: VkApi.cs 项目: he305/sharpbot
        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);
            }
        }