Exemple #1
0
        /// <summary>
        /// Retrieve the users blocked by the current user.
        /// Populate the corresponding attributes according to the value of the boolean parameters.
        /// Return the list of users blocked by the current user.
        /// </summary>
        /// <param name="createBlockUsers">True by default. Update the attribute _blocked_users if this parameter is true</param>
        /// <param name="createBlockedUsersIds">True by default. Update the attribute _blocked_users_ids if this parameter is true</param>
        /// <returns>Null if there is no valid token for the current user. Otherwise, The list of users blocked by the current user.</returns>
        public List <IUser> GetBlockedUsers(bool createBlockUsers = true, bool createBlockedUsersIds = true)
        {
            if (_token == null)
            {
                return(null);
            }

            List <IUser>            blockedUsers    = new List <IUser>();
            DynamicResponseDelegate blockedUsersDel = delegate(Dictionary <string, object> twitterBlockedUsers, long previousCursor, long nextCursor)
            {
                // Get the users from the Twitter API's response and store them in the users list
                foreach (var tbu in (IEnumerable <object>)twitterBlockedUsers["users"])
                {
                    IUser blockedUser = User.Create(tbu);
                    if (blockedUser != null)
                    {
                        blockedUser.ObjectToken = _shareTokenWithChild ? this._token : null;
                        blockedUsers.Add(blockedUser);
                        if (createBlockedUsersIds)
                        {
                            // update the list of ids of the blocked users if required
                            if (blockedUser.Id != null)
                            {
                                _blocked_users_ids.Add(Int64.Parse(blockedUser.Id.ToString()));
                            }
                        }
                    }
                }

                return(((IEnumerable <object>)twitterBlockedUsers["users"]).Count());
            };

            executeBlockedUsersQuery(Resources.TokenUser_GetBlockedUsers, blockedUsersDel);

            // Update the list of blocked users if required
            if (createBlockUsers)
            {
                _blocked_users = blockedUsers;
            }

            return(blockedUsers);
        }
        /// <summary>
        /// Create a Message and fill all its fields.
        /// Do nothing if the parameter is null.
        /// </summary>
        /// <param name="messageContent">Values of the message's attributes</param>
        public override void Populate(Dictionary <String, object> messageContent)
        {
            // cannot set the message fields if the content is not available
            if (messageContent == null)
            {
                return;
            }

            // set the basic fields of the message
            FillBasicMessageInfoFromDictionary(messageContent);

            if (messageContent.GetProp("sender") is Dictionary <String, object> )
            {
                this.Sender = User.Create(messageContent.GetProp("sender"));
            }

            if (messageContent.GetProp("recipient") is Dictionary <String, object> )
            {
                this.Receiver = User.Create(messageContent.GetProp("recipient"));
            }

            _messageCreatedFromApi = false;
        }
Exemple #3
0
        /// <summary>
        /// Iterate over the objects given in parameter and create the associated Users.
        /// Build the list containing all these users and assign it to the attribute members.
        /// If the table given in parameter is null, throw an exception and reset the attributes members to null.
        /// </summary>
        /// <param name="suggUsersTable">table in which each object represents a user</param>
        private void ExtractMembers(Dictionary <string, object>[] suggUsersTable)
        {
            // Throw exception if parameter is null
            if (suggUsersTable == null)
            {
                HandleMembersErrorFromTwitter();
            }

            // Build the list of users
            List <IUser> refreshedUserList = new List <IUser>();

            foreach (Dictionary <string, object> su in suggUsersTable)
            {
                User u = User.Create(su);

                if (u != null)
                {
                    refreshedUserList.Add(u);
                }
            }
            // Keep the resulting list to the attribute
            _members = refreshedUserList;
        }
Exemple #4
0
        /// <summary>
        /// Retrieve the users blocked by the current user.
        /// Populate the corresponding attributes according to the value of the boolean parameters.
        /// Return the list of users blocked by the current user.
        /// </summary>
        /// <param name="createBlockUsers">True by default. Update the attribute _blocked_users if this parameter is true</param>
        /// <param name="createBlockedUsersIds">True by default. Update the attribute _blocked_users_ids if this parameter is true</param>
        /// <returns>Null if there is no valid token for the current user. Otherwise, The list of users blocked by the current user.</returns>
        public List <IUser> GetBlockedUsers(bool createBlockUsers = true, bool createBlockedUsersIds = true)
        {
            if (_token == null)
            {
                return(null);
            }

            List <IUser>            blockedUsers    = new List <IUser>();
            DynamicResponseDelegate blockedUsersDel = delegate(dynamic twitterBlockedUsers, long previous_cursor, long next_cursor)
            {
                // Get the users from the Twitter API's response and store them in the users list
                foreach (var tbu in twitterBlockedUsers["users"])
                {
                    IUser blockedUser = User.Create(tbu);
                    if (blockedUser != null)
                    {
                        blockedUser.ObjectToken = _shareTokenWithChild ? this._token : null;
                        blockedUsers.Add(blockedUser);
                        if (createBlockedUsersIds)
                        {
                            // update the list of ids of the blocked users if required
                            _blocked_users_ids.Add((long)blockedUser.Id);
                        }
                    }
                }
            };

            executeBlockedUsersQuery(Resources.TokenUser_GetBlockedUsers, blockedUsersDel);

            // Update the list of blocked users if required
            if (createBlockUsers)
            {
                _blocked_users = blockedUsers;
            }

            return(blockedUsers);
        }
Exemple #5
0
        private void fillTweet(Dictionary <String, object> dTweet, bool cleanString = true)
        {
            if (dTweet.GetProp("id") != null)
            {
                created_at = DateTime.ParseExact(dTweet.GetProp("created_at") as string,
                                                 "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture);
                id                        = dTweet.GetProp("id") as long?;
                id_str                    = dTweet.GetProp("id_str") as string;
                text                      = dTweet.GetProp("text") as string;
                source                    = dTweet.GetProp("source ") as string;
                truncated                 = dTweet.GetProp("truncated") as bool?;
                in_reply_to_status_id     = dTweet.GetProp("in_reply_to_status_id") as int?;
                in_reply_to_status_id_str = dTweet.GetProp("in_reply_to_status_id_str") as string;
                in_reply_to_user_id       = dTweet.GetProp("in_reply_to_user_id") as int?;
                in_reply_to_user_id_str   = dTweet.GetProp("in_reply_to_user_id_str") as string;
                in_reply_to_screen_name   = dTweet.GetProp("in_reply_to_screen_name") as string;
                user                      = User.Create(dTweet.GetProp("user") as object);
                // Create Geo
                var geo = dTweet.GetProp("geo");
                // Create Coordinates
                var coordinates = dTweet.GetProp("coordinates");
                // Create Place
                var place = dTweet.GetProp("place");
                // Create Contributors
                var contributors = dTweet.GetProp("contributors");
                retweet_count      = dTweet.GetProp("retweet_count") as int?;
                entities           = new TweetEntities(dTweet["entities"] as Dictionary <String, object>);
                favorited          = dTweet.GetProp("favorited") as bool?;
                retweeted          = dTweet.GetProp("retweeted") as bool?;
                possibly_sensitive = dTweet.GetProp("possibly_sensitive") as bool?;

                if (cleanString == true)
                {
                    text = text.cleanString();
                }
            }
        }
Exemple #6
0
 /// <summary>
 /// Create a user from information retrieved from Twitter
 /// </summary>
 /// <param name="dynamicUser">Information retrieved from Twitter</param>
 /// <param name="shareTokenWithChild">Shall the token be shared to objects created from the user</param>
 public static User Create(object dynamicUser, bool shareTokenWithChild = true)
 {
     return(User.Create(dynamicUser as Dictionary <string, object>, shareTokenWithChild));
 }
Exemple #7
0
        /// <summary>
        /// Populate a tweet from information retrieved from twitter
        /// </summary>
        /// <param name="dTweet">Object containing Tweet information</param>
        /// <param name="cleanString">Does the string needs to be cleanup when retrieved</param>
        protected virtual void Populate(Dictionary <String, object> dTweet, bool cleanString)
        {
            if (dTweet.GetProp("id") != null)
            {
                CreatedAt = DateTime.ParseExact(dTweet.GetProp("created_at") as string,
                                                "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture);
                Id                   = Convert.ToInt64(dTweet.GetProp("id_str"));
                IdValue              = Convert.ToInt64(dTweet.GetProp("id"));
                IdStr                = dTweet.GetProp("id_str") as string;
                Text                 = dTweet.GetProp("text") as string;
                Source               = dTweet.GetProp("source ") as string;
                Truncated            = dTweet.GetProp("truncated") as bool?;
                InReplyToStatusId    = dTweet.GetProp("in_reply_to_status_id") as int?;
                InReplyToStatusIdStr = dTweet.GetProp("in_reply_to_status_id_str") as string;
                InReplyToUserId      = dTweet.GetProp("in_reply_to_user_id") as int?;
                InReplyToUserIdStr   = dTweet.GetProp("in_reply_to_user_id_str") as string;
                InReplyToScreenName  = dTweet.GetProp("in_reply_to_screen_name") as string;
                Creator              = User.Create(dTweet.GetProp("user"));

                if (_shareTokenWithChild)
                {
                    Creator.ObjectToken = _token;
                }

                Location = Geo.Create(dTweet.GetProp("geo"));

                if (Location != null)
                {
                    LocationCoordinates = Location.GeoCoordinates;
                }

                // Create Place
                _place = dTweet.GetProp("place") as string;

                // Create Contributors
                var contributors = dTweet.GetProp("contributors");

                RetweetCount = dTweet.GetProp("retweet_count") as int?;

                if (dTweet.ContainsKey("entities"))
                {
                    Entities = new TweetEntities(dTweet["entities"] as Dictionary <String, object>);
                }

                Favourited        = dTweet.GetProp("favorited") as bool?;
                Retweeted         = dTweet.GetProp("retweeted") as bool?;
                PossiblySensitive = dTweet.GetProp("possibly_sensitive") as bool?;

                if (dTweet.ContainsKey("retweeted_status"))
                {
                    var retweet = dTweet.GetProp("retweeted_status") as Dictionary <string, object>;

                    if (retweet != null)
                    {
                        Retweeting = new Tweet(retweet);
                    }
                }

                if (cleanString)
                {
                    Text = Text.CleanString();
                }

                _isTweetPublished = true;
            }
        }
Exemple #8
0
        /// <summary>
        /// Return a list of users corresponding to the list of user ids and screen names given in parameter.
        /// Throw an exception if the token given in parameter is null or if both lists given in parameters are null.
        /// </summary>
        /// <param name="userIds">
        ///     List of user screen names. This parameter can be null
        ///     List of user ids. This parameter can be null
        /// </param>
        /// <param name="screen_names"></param>
        /// <param name="token">Token used to request the users' information from the Twitter API</param>
        /// <returns>The list of users retrieved from the Twitter API</returns>
        public static List <IUser> Lookup(List <long> userIds, List <string> screen_names, IToken token)
        {
            if (token == null)
            {
                throw new ArgumentException("Token must not be null");
            }
            if (userIds == null && screen_names == null)
            {
                throw new ArgumentException("User ids or screen names must be specified");
            }

            List <IUser> users = new List <IUser>();

            if (userIds == null)
            {
                userIds = new List <long>();
            }
            if (screen_names == null)
            {
                screen_names = new List <string>();
            }

            int userIndex       = 0;
            int screenNameIndex = 0;

            while ((userIndex < userIds.Count) || (screenNameIndex < screen_names.Count))
            {
                // Keep track of the number of users that we are going to request from the Twitter API
                int    indicesSumBeforeLoop = userIndex + screenNameIndex;
                string userIdsStrList       = "user_id=";
                string screen_namesStrList  = "screen_name=";
                // Maximum number of users that can be requested from the Twitter API (in 1 single request)
                int listMaxSize = 100;

                // Take request parameters from both names list and id list
                // userIndex + screenNameIndex - indicesSumBeforeLoop) < listMaxSize ==> Check that the number of parameters given to the Twitter API request does not exceed the limit
                while (((userIndex + screenNameIndex - indicesSumBeforeLoop) < listMaxSize) &&
                       (userIndex < userIds.Count) &&
                       (screenNameIndex < screen_names.Count))
                {
                    screen_namesStrList += screen_names.ElementAt(screenNameIndex++) + "%2C";
                    userIdsStrList      += userIds.ElementAt(userIndex++) + "%2C";
                }
                // Take request from id list
                while (((userIndex + screenNameIndex - indicesSumBeforeLoop) < listMaxSize) &&
                       (userIndex < userIds.Count))
                {
                    userIdsStrList += userIds.ElementAt(userIndex++) + "%2C";
                }
                // Take name from id list
                while (((userIndex + screenNameIndex - indicesSumBeforeLoop) < listMaxSize) &&
                       (screenNameIndex < screen_names.Count))
                {
                    screen_namesStrList += screen_names.ElementAt(screenNameIndex++) + "%2C";
                }

                String query = Resources.UserUtils_Lookup;
                // Add new parameters to the query and format it
                query = enrichLookupQuery(query, screen_namesStrList);
                query = enrichLookupQuery(query, userIdsStrList);

                ObjectResponseDelegate objectDelegate = delegate(Dictionary <string, object> responseObject)
                {
                    User u = User.Create(responseObject);
                    users.Add(u);
                };

                token.ExecuteGETQuery(query, objectDelegate);
            }

            return(users);
        }