internal object DeserializeContent(string content, Type type)
        {
            if (string.IsNullOrEmpty(content) || content.Trim().Length == 0)
            {
                return(null);
            }
            if (type == typeof(TwitterError))
            {
                // {"errors":[{"message":"Bad Authentication data","code":215}]}
                content = content.Trim('\n');
                if (content.StartsWith("{\"errors\":["))
                {
                    var errors = JObject.Parse(content)["errors"];
                    if (errors != null)
                    {
                        var result = new TwitterError {
                            RawSource = content
                        };
                        if (errors.Type == JTokenType.Array)
                        {
                            var error = errors.First();
                            result.Message = error["message"].ToString();
                            result.Code    = int.Parse(error["code"].ToString());
                        }
                        else
                        {
                            result.Message = errors.ToString();
                        }
                        return(result);
                    }
                }
                else if (content.Contains("error"))
                {
                    var error = JObject.Parse(content)["error"].ToString();
                    if (error != null)
                    {
                        var result = new TwitterError {
                            RawSource = content
                        };
                        result.Message = error;
                        result.Code    = 99;
                        return((ITwitterModel)result);
                    }
                }
            }

            if (type == typeof(TwitterTrends))
            {
                return(DeserializeTrends(content));
            }

            if (type == typeof(TwitterLocalTrends))
            {
                var instance = JArray.Parse(content);
                var inner    = instance.First.ToString();
                return(DeserializeSingle(inner, type));
            }

            if (type == typeof(TwitterStreamArtifact))
            {
                content = content.Trim('\n');
                if (content.StartsWith("{\"friends\":["))
                {
                    var friends = (JArray)JObject.Parse(content)["friends"];
                    if (friends != null)
                    {
                        var result = new TwitterUserStreamFriends {
                            RawSource = content
                        };
                        var ids = friends.Select(token => token.Value <long>()).ToList();
                        result.Ids = ids;
                        return(result);
                    }
                }
                // {"delete":{"status":{"user_id_str":"14363427","id_str":"45331017418014721","id":45331017418014721,"user_id":14363427}}}
                else if (content.StartsWith("{\"delete\":{\"status\":"))
                {
                    var deleted = JObject.Parse(content)["delete"]["status"];
                    if (deleted != null)
                    {
                        var result = new TwitterUserStreamDeleteStatus
                        {
                            RawSource = content,
                            StatusId  = deleted["id"].Value <long>(),
                            UserId    = deleted["user_id"].Value <int>()
                        };
                        return(result);
                    }
                }
                else if (content.StartsWith("{\"delete\":{\"direct_message\":"))
                {
                    var deleted = JObject.Parse(content)["delete"]["direct_message"];
                    if (deleted != null)
                    {
                        var result = new TwitterUserStreamDeleteDirectMessage
                        {
                            RawSource       = content,
                            DirectMessageId = deleted["id"].Value <long>(),
                            UserId          = deleted["user_id"].Value <int>()
                        };
                        return(result);
                    }
                }
                else
                {
                    var artifact = JObject.Parse(content);
                    if (artifact["target_object"] != null)
                    {
                        return(DeserializeUserStreamEvent(content));
                    }

                    if (artifact["user"] != null)
                    {
                        var tweet  = DeserializeSingle(content, typeof(TwitterStatus)) as TwitterStatus;
                        var @event = new TwitterUserStreamStatus {
                            Status = tweet, RawSource = content
                        };
                        return(@event);
                    }

                    if (artifact["direct_message"] != null)
                    {
                        var json   = artifact["direct_message"].ToString();
                        var dm     = DeserializeSingle(json, typeof(TwitterDirectMessage)) as TwitterDirectMessage;
                        var @event = new TwitterUserStreamDirectMessage {
                            DirectMessage = dm, RawSource = json
                        };
                        return(@event);
                    }

                    var unknown = new TwitterStreamArtifact {
                        RawSource = content
                    };
                    return(unknown);
                }
            }

            if (type == typeof(IEnumerable <TwitterTrends>))
            {
                return(DeserializeTrends(content));
            }

            var wantsCollection = typeof(IEnumerable).IsAssignableFrom(type);

            var deserialized = wantsCollection
                                   ? DeserializeCollection(content, type)
                                   : DeserializeSingle(content, type);

            return(deserialized);
        }
Esempio n. 2
0
 void DMAction(TwitterUserStreamDirectMessage p)
 {
     if (p.DirectMessage.Recipient == CurrentUser)
     {
         NotifyDirectMessage(p.DirectMessage);
     }
 }