Ejemplo n.º 1
0
        /// <summary>
        /// Retrieve the ids of the users blocked by the current user.
        /// Populate the corresponding attribute according to the value of the boolean parameter.
        /// Return the list of ids of the users blocked by the current user.
        /// </summary>
        /// <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 ids of the users blocked by the current user.</returns>
        public List <long> GetBlockedUsersIds(bool createBlockedUsersIds = true)
        {
            if (_token == null)
            {
                return(null);
            }

            List <long>             blockedUsersIds = new List <long>();
            DynamicResponseDelegate blockedUsersDel = delegate(dynamic twitterBlockedUsersIds, long previous_cursor, long next_cursor)
            {
                // Get the ids of the blocked users from the Twitter API's response and store them in the result list
                foreach (long id in twitterBlockedUsersIds["ids"])
                {
                    blockedUsersIds.Add(id);
                }
            };

            executeBlockedUsersQuery(Resources.TokenUser_GetBlockedUsersIds, blockedUsersDel, createBlockedUsersIds);

            // Update the attribute if required
            if (createBlockedUsersIds)
            {
                _blocked_users_ids = blockedUsersIds;
            }

            return(blockedUsersIds);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reinitialize the list of ids of the users blocked by the current user if required by the boolean parameter.
        /// Retrieve information about the users blocked by the current user from the Twitter API.
        /// </summary>
        /// <param name="blockedUsersQuery">Request to send to the Twitter API</param>
        /// <param name="del">delegate that will handle the data sent in the Twitter API's response</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 ids of the users blocked by the current user.</returns>
        private void executeBlockedUsersQuery(string blockedUsersQuery,
                                              DynamicResponseDelegate del,
                                              bool createBlockedUsersIds = true)
        {
            // Reset the list of blocked users' ids if required
            if (createBlockedUsersIds)
            {
                _blocked_users_ids = null;
                _blocked_users_ids = new List <long>();
            }

            WebExceptionHandlingDelegate wexDel = delegate(WebException wex)
            {
                int?wexStatusNumber = ExceptionUtils.GetWebExceptionStatusNumber(wex);
                switch (wexStatusNumber)
                {
                // Handle the Exception when the user's token is not valid
                case 401:
                    throw new WebException("Blocked users can only be accessed using the user's token. Any other token is rejected.", wex);

                default:
                    throw wex;
                }
            };

            _token.ExecuteCursorQuery(blockedUsersQuery, 0, Int32.MaxValue, del, wexDel);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Retrieve the ids of the users blocked by the current user.
        /// Populate the corresponding attribute according to the value of the boolean parameter.
        /// Return the list of ids of the users blocked by the current user.
        /// </summary>
        /// <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 ids of the users blocked by the current user.</returns>
        public List <long> GetBlockedUsersIds(bool createBlockedUsersIds = true)
        {
            if (_token == null)
            {
                return(null);
            }

            List <long>             blockedUsersIds = new List <long>();
            DynamicResponseDelegate blockedUsersDel = delegate(Dictionary <string, object> twitterBlockedUsersIds, long previousCursor, long nextCursor)
            {
                // Get the ids of the blocked users from the Twitter API's response and store them in the result list
                foreach (var id in (IEnumerable <object>)twitterBlockedUsersIds["ids"])
                {
                    blockedUsersIds.Add(Int64.Parse(id.ToString()));
                }

                return(((IEnumerable <object>)twitterBlockedUsersIds["ids"]).Count());
            };

            executeBlockedUsersQuery(Resources.TokenUser_GetBlockedUsersIds, blockedUsersDel, createBlockedUsersIds);

            // Update the attribute if required
            if (createBlockedUsersIds)
            {
                _blocked_users_ids = blockedUsersIds;
            }

            return(blockedUsersIds);
        }
Ejemplo n.º 4
0
 public Dictionary <string, object> ExecuteCursorQuery(
     string url,
     long cursor = 0,
     int max     = Int32.MaxValue,
     DynamicResponseDelegate responseHandler       = null,
     WebExceptionHandlingDelegate exceptionHandler = null)
 {
     return(ExecuteCursorQuery(url, ref cursor, max, responseHandler, exceptionHandler));
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Function that execute cursor query and send information for each query executed
        /// </summary>
        /// <param name="token"></param>
        static void ExecuteCursorQuery(IToken token)
        {
            // The delegate is a function that will be called for each cursor
            DynamicResponseDelegate del = delegate(dynamic jsonResponse, long previous_cursor, long next_cursor)
            {
                Console.WriteLine(previous_cursor + " -> " + next_cursor + " : " + jsonResponse.Count);
            };

            token.ExecuteCursorQuery("https://api.twitter.com/1/friends/ids.json?user_id=700562792", del);
        }
Ejemplo n.º 6
0
        public Dictionary <string, object> ExecuteCursorQuery(
            string url,
            ref long cursor,
            int max = Int32.MaxValue,
            DynamicResponseDelegate responseHandler       = null,
            WebExceptionHandlingDelegate exceptionHandler = null)
        {
            long previousCursor       = cursor;
            long nextCursor           = -1;
            int  nbOfObjectsProcessed = 0;

            while (previousCursor != nextCursor && nbOfObjectsProcessed < max)
            {
                try
                {
                    string queryDelimiter = "&";
                    if (url.EndsWith(".json") || url.EndsWith(".xml") || url.EndsWith("."))
                    {
                        queryDelimiter = "?";
                    }

                    Dictionary <string, object> responseObject = ExecuteGETQuery(String.Format("{0}" + queryDelimiter + "cursor={1}", url, nextCursor), null, exceptionHandler);

                    // Go to the next object
                    if (responseObject != null)
                    {
                        previousCursor = Int64.Parse(responseObject["previous_cursor"].ToString());
                        nextCursor     = Int64.Parse(responseObject["next_cursor"].ToString());
                    }

                    if (responseHandler != null)
                    {
                        // Manual response management
                        nbOfObjectsProcessed += responseHandler(responseObject, previousCursor, nextCursor);
                    }
                }
                catch (WebException wex)
                {
                    if (ExceptionHandler != null)
                    {
                        ExceptionHandler(wex);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Get the followers of a User by using the specified Token
        /// </summary>
        /// <param name="token">Token to operate a query</param>
        /// <param name="createFollowerList">Whether this method will fill the Follower list</param>
        /// <param name="cursor">Current Page of the query</param>
        /// <param name="maxFollowers">Max number of users</param>
        /// <returns>List of Followers Id</returns>
        public List <long> GetFollowerIds(IToken token, bool createFollowerList = false, long cursor = 0, int maxFollowers = Int32.MaxValue)
        {
            token = GetQueryToken(token);

            if (token == null)
            {
                return(null);
            }

            if (cursor == 0)
            {
                FollowerIds = new List <long>();
                Followers   = new List <IUser>();
            }

            string query = Resources.User_GetFollowers;

            if (!AddUserInformationInQuery(ref query))
            {
                return(null);
            }

            DynamicResponseDelegate del = delegate(Dictionary <string, object> responseObject, long previousCursor, long nextCursor)
            {
                var userIds = (responseObject["ids"] as IEnumerable <object>) != null ? (responseObject["ids"] as IEnumerable <object>).ToList() : null;

                if (userIds != null)
                {
                    foreach (var followerId in userIds)
                    {
                        FollowerIds.Add(Int64.Parse(followerId.ToString()));

                        if (createFollowerList)
                        {
                            Followers.Add(new User(Int64.Parse(followerId.ToString()))
                            {
                                ObjectToken = _shareTokenWithChild ? _token : null,
                            });
                        }
                    }

                    return(userIds.Count());
                }

                return(0);
            };

            token.ExecuteCursorQuery(query, del);
            return(FollowerIds);
        }
Ejemplo n.º 8
0
        public Dictionary <string, object> ExecuteCursorQuery(
            string url,
            long cursor = 0,
            DynamicResponseDelegate responseHandler       = null,
            WebExceptionHandlingDelegate exceptionHandler = null)
        {
            long previousCursor = cursor;
            long nextCursor     = -1;

            while (previousCursor != nextCursor)
            {
                try
                {
                    string queryDelimiter = "&";
                    if (url.EndsWith(".json") || url.EndsWith(".xml") || url.EndsWith("."))
                    {
                        queryDelimiter = "?";
                    }

                    dynamic responseObject = ExecuteGETQuery(String.Format("{0}" + queryDelimiter + "cursor={1}", url, nextCursor), null, exceptionHandler);

                    // Go to the next object
                    if (responseObject != null)
                    {
                        previousCursor = (long)responseObject["previous_cursor"];
                        nextCursor     = (long)responseObject["next_cursor"];
                    }

                    if (responseHandler != null)
                    {
                        // Manual response management
                        responseHandler(responseObject, previousCursor, nextCursor);
                    }
                }
                catch (WebException wex)
                {
                    if (ExceptionHandler != null)
                    {
                        ExceptionHandler(wex);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Get a List of Friends Ids by using the Current Token
        /// </summary>
        /// <param name="token">Token to operate the query</param>
        /// <param name="createUserIdsList">Whether this method will fill the Friends list</param>
        /// <param name="cursor">Current Page of the query</param>
        /// <returns>List of Friends Id</returns>
        public List <long> GetFriendsIds(IToken token, bool createUserIdsList = false, long cursor = 0)
        {
            token = GetQueryToken(token);

            if (token == null)
            {
                return(null);
            }

            if (cursor == 0)
            {
                FriendIds = new List <long>();
                Friends   = new List <IUser>();
            }

            DynamicResponseDelegate del = delegate(dynamic responseObject, long previous_cursor, long next_cursor)
            {
                foreach (var friend_id in responseObject["ids"])
                {
                    FriendIds.Add((long)friend_id);

                    if (createUserIdsList)
                    {
                        Friends.Add(new User((long)friend_id)
                        {
                            ObjectToken = _shareTokenWithChild ? this._token : null,
                        });
                    }
                }
            };

            if (Id != null)
            {
                token.ExecuteCursorQuery(String.Format(Resources.User_GetFriendsIdsFromId, Id), del);
            }
            else
            {
                if (_screen_name != null)
                {
                    token.ExecuteCursorQuery(String.Format(Resources.User_GetFriendsIdsFromScreenName, ScreenName), del);
                }
            }

            return(FriendIds);
        }
Ejemplo n.º 10
0
        public dynamic ExecuteCursorQuery(string method_url, long cursor          = 0,
                                          DynamicResponseDelegate cursor_delegate = null, WebExceptionHandlingDelegate exceptionHandlerDelegate = null)
        {
            long previous_cursor = cursor;
            long next_cursor     = -1;

            while (previous_cursor != next_cursor)
            {
                dynamic responseObject = null;
                try
                {
                    responseObject = this.ExecuteQuery(String.Format("{0}&cursor={1}", method_url, next_cursor));

                    if (responseObject != null)
                    {
                        previous_cursor = (long)responseObject["previous_cursor"];
                        next_cursor     = (long)responseObject["next_cursor"];
                    }

                    if (cursor_delegate != null)
                    {
                        cursor_delegate(responseObject, previous_cursor, next_cursor);
                    }
                }
                catch (WebException wex)
                {
                    if (exceptionHandlerDelegate != null)
                    {
                        exceptionHandlerDelegate(wex);
                    }
                    else
                    if (ExceptionHandler != null)
                    {
                        ExceptionHandler(wex);
                    }
                    else
                    {
                        throw wex;
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Get the followers of a User by using the specified Token
        /// </summary>
        /// <param name="token">Token to operate a query</param>
        /// <param name="createFollowerList">Whether this method will fill the Follower list</param>
        /// <param name="cursor">Current Page of the query</param>
        /// <returns>List of Followers Id</returns>
        public List <long> GetFollowers(IToken token, bool createFollowerList = false, long cursor = 0)
        {
            token = GetQueryToken(token);

            if (token == null)
            {
                return(null);
            }

            if (cursor == 0)
            {
                FollowerIds = new List <long>();
                Followers   = new List <IUser>();
            }

            string query = Resources.User_GetFollowers;

            AddUserInformationInQuery(ref query);

            DynamicResponseDelegate del = delegate(dynamic responseObject, long previous_cursor, long next_cursor)
            {
                foreach (var follower_id in responseObject["ids"])
                {
                    FollowerIds.Add((long)follower_id);

                    if (createFollowerList)
                    {
                        Followers.Add(new User((long)follower_id)
                        {
                            ObjectToken = _shareTokenWithChild ? this._token : null,
                        });
                    }
                }
            };

            token.ExecuteCursorQuery(query, del);

            return(FollowerIds);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
 public dynamic ExecuteCursorQuery(string method_url, DynamicResponseDelegate cursor_delegate)
 {
     return ExecuteCursorQuery(method_url, 0, cursor_delegate);
 }
Ejemplo n.º 15
0
        public dynamic ExecuteCursorQuery(string method_url, long cursor = 0,
            DynamicResponseDelegate cursor_delegate = null, WebExceptionHandlingDelegate exceptionHandlerDelegate = null)
        {
            long previous_cursor = cursor;
            long next_cursor = -1;

            while (previous_cursor != next_cursor)
            {
                dynamic responseObject = null;
                try
                {
                    responseObject = this.ExecuteQuery(String.Format("{0}&cursor={1}", method_url, next_cursor));

                    if (responseObject != null)
                    {
                        previous_cursor = (long)responseObject["previous_cursor"];
                        next_cursor = (long)responseObject["next_cursor"];
                    }

                    if (cursor_delegate != null)
                        cursor_delegate(responseObject, previous_cursor, next_cursor);
                }
                catch (WebException wex)
                {
                    if (exceptionHandlerDelegate != null)
                        exceptionHandlerDelegate(wex);
                    else
                        if (ExceptionHandler != null)
                            ExceptionHandler(wex);
                        else
                            throw wex;
                }
            }

            return null;
        }
Ejemplo n.º 16
0
 public dynamic ExecuteCursorQuery(string method_url, DynamicResponseDelegate cursor_delegate)
 {
     return(ExecuteCursorQuery(method_url, 0, cursor_delegate));
 }
Ejemplo n.º 17
0
 public Dictionary <string, object> ExecuteCursorQuery(
     string url,
     DynamicResponseDelegate cursorDelegate)
 {
     return(ExecuteCursorQuery(url, 0, cursorDelegate));
 }