Beispiel #1
0
        public async Task <VkNewsResponse> Get(string sourceIds = null, string filters = null, int count = 0, string startFrom = null)
        {
            var parameters = new Dictionary <string, string>();

            if (!string.IsNullOrEmpty(sourceIds))
            {
                parameters.Add("source_ids", sourceIds);
            }

            if (!string.IsNullOrEmpty(filters))
            {
                parameters.Add("filters", filters);
            }

            if (count > 0)
            {
                parameters.Add("count", count.ToString());
            }

            if (!string.IsNullOrEmpty(startFrom))
            {
                parameters.Add("start_from", startFrom);
            }

            _vkontakte.SignMethod(parameters);

            var response = await VkRequest.GetAsync(VkConst.MethodBase + "newsfeed.get", parameters);

            if (response.SelectToken("response.items") != null)
            {
                var result = new VkNewsResponse((from n in response["response"]["items"] select VkNewsEntry.FromJson(n)).ToList());

                if (response["response"]["profiles"] != null)
                {
                    var users = (from n in response["response"]["profiles"] select VkProfile.FromJson(n)).ToList();
                    foreach (var entry in result.Items)
                    {
                        entry.Author = users.FirstOrDefault(u => u.Id == entry.SourceId);
                    }
                }

                if (response["response"]["groups"] != null)
                {
                    var groups = (from n in response["response"]["groups"] select VkGroup.FromJson(n)).ToList();
                    foreach (var entry in result.Items.Where(e => e.Author == null))
                    {
                        entry.Author = groups.FirstOrDefault(g => g.Id == Math.Abs(entry.SourceId));
                    }
                }

                if (response["response"]["next_from"] != null)
                {
                    result.NextFrom = response["response"]["next_from"].Value <string>();
                }

                return(result);
            }

            return(null);
        }
Beispiel #2
0
        public static VkNewsEntry FromJson(JToken json)
        {
            if (json == null)
            {
                throw new ArgumentException("Json can not be null.");
            }

            var result = new VkNewsEntry();

            if (json["post_id"] != null)
            {
                result.Id = json["post_id"].Value <long>();
            }

            if (json["type"] != null)
            {
                result.Type = json["type"].Value <string>();
            }

            if (json["source_id"] != null)
            {
                result.SourceId = json["source_id"].Value <long>();
            }

            if (json["date"] != null)
            {
                result.Date = DateTimeExtensions.UnixTimeStampToDateTime(json["date"].Value <long>());
            }

            if (json["text"] != null)
            {
                result.Text = WebUtility.HtmlDecode(json["text"].Value <string>());
                result.Text = result.Text.Replace("<br>", Environment.NewLine);
            }

            if (json["attachments"] != null)
            {
                result.Attachments = new List <VkAttachment>();

                foreach (var a in json["attachments"])
                {
                    switch (a["type"].Value <string>())
                    {
                    case "audio":
                        result.Attachments.Add(VkAudioAttachment.FromJson(a["audio"]));
                        break;

                    case "photo":
                        result.Attachments.Add(VkPhotoAttachment.FromJson(a["photo"]));
                        break;
                    }
                }
            }


            if (json["copy_history"] != null)
            {
                result.CopyHistory = new List <VkNewsEntry>();

                foreach (var p in json["copy_history"])
                {
                    var post = VkNewsEntry.FromJson(p);
                    if (post != null)
                    {
                        result.CopyHistory.Add(post);
                    }
                }
            }

            if (json["comments"] != null)
            {
                result.CommentsCount   = json["comments"]["count"].Value <long>();
                result.CanWriteComment = json["comments"]["can_post"].Value <int>() == 1;
            }

            return(result);
        }