Esempio n. 1
0
        public static VkWallEntry FromJson(JToken json)
        {
            if (json == null)
            {
                throw new ArgumentException("Json can not be null.");
            }

            var result = new VkWallEntry();

            result.Id = json["id"].Value <double>();

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

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

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

            if (json["attachments"] != null)
            {
                result.Attachments = VkAttachment.FromJson(json["attachments"]);
            }

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

                foreach (var p in json["copy_history"])
                {
                    try
                    {
                        var post = VkWallEntry.FromJson(p);
                        if (post != null)
                        {
                            result.CopyHistory.Add(post);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
            }

            return(result);
        }
Esempio n. 2
0
        public static VkWallEntry FromJson(JToken json)
        {
            if (json == null)
                throw new ArgumentException("Json can not be null.");

            var result = new VkWallEntry();
            result.Id = json["id"].Value<double>();

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

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

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

            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<VkWallEntry>();

                foreach (var p in json["copy_history"])
                {
                    try
                    {
                        var post = VkWallEntry.FromJson(p);
                        if (post != null)
                            result.CopyHistory.Add(post);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }

                }
            }

            return result;
        }
Esempio n. 3
0
        public async Task <VkItemsResponse <VkWallEntry> > Get(long ownerId, string filter, int count = 0, int offset = 0)
        {
            if (_vkontakte.AccessToken == null || string.IsNullOrEmpty(_vkontakte.AccessToken.Token) || _vkontakte.AccessToken.HasExpired)
            {
                throw new Exception("Access token is not valid.");
            }

            var parameters = new Dictionary <string, string>();

            if (ownerId != 0)
            {
                parameters.Add("owner_id", ownerId.ToString());
            }

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

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

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

            parameters.Add("extended", "1");

            _vkontakte.SignMethod(parameters);

            var response = await new VkRequest(new Uri(VkConst.MethodBase + "wall.get"), parameters).Execute();

            VkErrorProcessor.ProcessError(response);

            if (response.SelectToken("response.items") != null)
            {
                var result = new VkItemsResponse <VkWallEntry>();
                result.Items      = (from p in response["response"]["items"] where p.HasValues select VkWallEntry.FromJson(p)).ToList();
                result.TotalCount = response["response"]["count"].Value <int>();

                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));
                    }
                }

                return(result);
            }

            return(null);
        }