示例#1
0
        /// <summary>
        /// Update Twitter colors
        /// </summary>
        /// <remarks>
        /// The # character prefix is optional.  At least one color argument must be provided.
        /// </remarks>
        /// <param name="background">background color</param>
        /// <param name="text">text color</param>
        /// <param name="link">link color</param>
        /// <param name="sidebarFill">sidebar color</param>
        /// <param name="sidebarBorder">sidebar border color</param>
        /// <returns>User info with new colors</returns>
        public User UpdateAccountColors(string background, string text, string link, string sidebarFill, string sidebarBorder)
        {
            var accountUrl = BaseUrl + "account/update_profile_colors.xml";

            if (string.IsNullOrEmpty(background) &&
                string.IsNullOrEmpty(text) &&
                string.IsNullOrEmpty(link) &&
                string.IsNullOrEmpty(sidebarFill) &&
                string.IsNullOrEmpty(sidebarBorder))
            {
                throw new ArgumentException("At least one of the colors (background, text, link, sidebarFill, or sidebarBorder) must be provided as arguments, but none are specified.");
            }

            var results =
                TwitterExecute.ExecuteTwitter(
                    accountUrl,
                    new Dictionary <string, string>
            {
                { "profile_background_color", background.TrimStart('#') },
                { "profile_text_color", text.TrimStart('#') },
                { "profile_link_color", link.TrimStart('#') },
                { "profile_sidebar_fill_color", sidebarFill.TrimStart('#') },
                { "profile_sidebar_border_color", sidebarBorder.TrimStart('#') }
            },
                    new UserRequestProcessor());

            return((results as IList <User>).FirstOrDefault());
        }
示例#2
0
        /// <summary>
        /// sends a new direct message to specified userr
        /// </summary>
        /// <param name="userID">id of user to send to</param>
        /// <param name="id">text to send</param>
        /// <returns>direct message element</returns>
        public DirectMessage NewDirectMessage(string userID, string text)
        {
            if (string.IsNullOrEmpty(userID))
            {
                throw new ArgumentException("userID is a required parameter.", "userID");
            }

            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentException("text is a required parameter.", "text");
            }

            if (text.Length > 140)
            {
                throw new ArgumentException("text must be no longer than 140 characters.", "text");
            }

            var newUrl = BaseUrl + "direct_messages/new.xml";

            var results =
                TwitterExecute.ExecuteTwitter(
                    newUrl,
                    new Dictionary <string, string>
            {
                { "user", userID },
                { "text", text }
            },
                    new DirectMessageRequestProcessor());

            return((results as IList <DirectMessage>).FirstOrDefault());
        }
示例#3
0
        /// <summary>
        /// lets logged-in user follow another user
        /// </summary>
        /// <param name="id">id of user to follow</param>
        /// <returns>followed friend user info</returns>
        public User CreateFriendship(string id, string userID, string screenName, bool follow)
        {
            if (string.IsNullOrEmpty(id) &&
                string.IsNullOrEmpty(userID) &&
                string.IsNullOrEmpty(screenName))
            {
                throw new ArgumentException("Either id, userID, or screenName is a required parameter.");
            }

            var destroyUrl = BaseUrl + "friendships/create/" + id + ".xml";

            var createParams = new Dictionary <string, string>
            {
                { "user_id", userID },
                { "screen_name", screenName }
            };

            // If follow exists in the parameter list, Twitter will
            // always treat it as true, even if the value is false;
            // Therefore, only add follow if it is true.
            if (follow)
            {
                createParams.Add("follow", "true");
            }

            var results =
                TwitterExecute.ExecuteTwitter(
                    destroyUrl,
                    createParams,
                    new UserRequestProcessor());

            return((results as IList <User>).FirstOrDefault());
        }
示例#4
0
        /// <summary>
        /// sends an image file to Twitter to replace background image
        /// </summary>
        /// <param name="imageFilePath">full path to file, including file name</param>
        /// <returns>User with new image info</returns>
        public User UpdateAccountBackgroundImage(string imageFilePath, bool tile)
        {
            var accountUrl = BaseUrl + "account/update_profile_background_image.xml";

            if (string.IsNullOrEmpty(imageFilePath))
            {
                throw new ArgumentException("imageFilePath is required.", "imageFilePath");
            }

            Dictionary <string, string> parameters = null;

            // TODO: tile implementation doesn't seem to be working; numerous update background image problems reported in Twitter API; check again later - Joe
            if (tile)
            {
                parameters =
                    new Dictionary <string, string>
                {
                    { "tile", "true" }
                };
            }

            var results = TwitterExecute.PostTwitterFile(imageFilePath, parameters, accountUrl, new UserRequestProcessor());

            return((results as IList <User>).FirstOrDefault());
        }
示例#5
0
        /// <summary>
        /// sends a status update
        /// </summary>
        /// <param name="status">(optional @UserName) and (required) status text</param>
        /// <param name="inReplyToStatusID">id of status replying to - optional - pass null if not used</param>
        /// <returns>IQueryable of sent status</returns>
        public Status UpdateStatus(string status, string inReplyToStatusID)
        {
            if (string.IsNullOrEmpty(status))
            {
                throw new ArgumentException("status is a required parameter.");
            }

            if (status.Length > 140)
            {
                throw new ArgumentException("status length must be no more than 140 characters.", "status");
            }

            status = status.Substring(0, Math.Min(140, status.Length));

            var updateUrl = BaseUrl + "statuses/update.xml";

            var results =
                TwitterExecute.ExecuteTwitter(
                    updateUrl,
                    new Dictionary <string, string>
            {
                { "status", status },
                { "in_reply_to_status_id", inReplyToStatusID }
            },
                    new StatusRequestProcessor());

            return((results as IList <Status>).FirstOrDefault());
        }
示例#6
0
        /// <summary>
        /// sends a test message to twitter to check connectivity
        /// </summary>
        /// <returns>true</returns>
        public bool HelpTest()
        {
            var helpUrl = BaseUrl + "help/test.xml";

            var results =
                TwitterExecute.ExecuteTwitter(
                    helpUrl,
                    new Dictionary <string, string>(),
                    new HelpRequestProcessor());

            return((results as IList <bool>).FirstOrDefault());
        }
示例#7
0
        /// <summary>
        /// Update account profile info
        /// </summary>
        /// <param name="name">User Name</param>
        /// <param name="email">Email Address</param>
        /// <param name="url">Web Address</param>
        /// <param name="location">Geographic Location</param>
        /// <param name="description">Personal Description</param>
        /// <returns>User with new info</returns>
        public User UpdateAccountProfile(string name, string email, string url, string location, string description)
        {
            var accountUrl = BaseUrl + "account/update_profile.xml";

            if (string.IsNullOrEmpty(name) &&
                string.IsNullOrEmpty(email) &&
                string.IsNullOrEmpty(url) &&
                string.IsNullOrEmpty(location) &&
                string.IsNullOrEmpty(description))
            {
                throw new ArgumentException("At least one of the colors (name, email, url, location, or description) must be provided as arguments, but none are specified.");
            }

            if (!string.IsNullOrEmpty(name) && name.Length > 20)
            {
                throw new ArgumentException("name must be no longer than 20 characters", "name");
            }

            if (!string.IsNullOrEmpty(email) && email.Length > 40)
            {
                throw new ArgumentException("email must be no longer than 40 characters", "email");
            }

            if (!string.IsNullOrEmpty(url) && url.Length > 100)
            {
                throw new ArgumentException("url must be no longer than 100 characters", "url");
            }

            if (!string.IsNullOrEmpty(location) && location.Length > 30)
            {
                throw new ArgumentException("location must be no longer than 30 characters", "location");
            }

            if (!string.IsNullOrEmpty(description) && description.Length > 160)
            {
                throw new ArgumentException("description must be no longer than 160 characters", "description");
            }

            var results =
                TwitterExecute.ExecuteTwitter(
                    accountUrl,
                    new Dictionary <string, string>
            {
                { "name", name },
                { "email", email },
                { "url", url },
                { "location", location },
                { "description", description }
            },
                    new UserRequestProcessor());

            return((results as IList <User>).FirstOrDefault());
        }
示例#8
0
 /// <summary>
 /// initialize TwitterContext with credentials and custom BaseUrl
 /// </summary>
 /// <param name="userName">name of user</param>
 /// <param name="password">user's password</param>
 /// <param name="baseUrl">base url of Twitter API</param>
 /// <param name="searchUrl">base url of Twitter Search API</param>
 public TwitterContext(string userName, string password, string baseUrl, string searchUrl)
 {
     TwitterExecute = new TwitterExecute();
     TwitterExecute.OAuthTwitter = OAuthTwitter;
     UserName             = userName;
     Password             = password;
     BaseUrl              = string.IsNullOrEmpty(baseUrl) ? "http://twitter.com/" : baseUrl;
     SearchUrl            = string.IsNullOrEmpty(searchUrl) ? "http://search.twitter.com/" : searchUrl;
     OAuthAccessTokenUrl  = "http://twitter.com/oauth/access_token";
     OAuthAuthorizeUrl    = "http://twitter.com/oauth/authorize";
     OAuthRequestTokenUrl = "http://twitter.com/oauth/request_token";
 }
示例#9
0
        /// <summary>
        /// sends an image file to Twitter to replace user image
        /// </summary>
        /// <remarks>
        /// You can only run this method with a period of time between executions;
        /// otherwise you get WebException errors from Twitter
        /// </remarks>
        /// <param name="imageFilePath">full path to file, including file name</param>
        /// <returns>User with new image info</returns>
        public User UpdateAccountImage(string imageFilePath)
        {
            var accountUrl = BaseUrl + "account/update_profile_image.xml";

            if (string.IsNullOrEmpty(imageFilePath))
            {
                throw new ArgumentException("imageFilePath is required.", "imageFilePath");
            }

            var results = TwitterExecute.PostTwitterFile(imageFilePath, null, accountUrl, new UserRequestProcessor());

            return((results as IList <User>).FirstOrDefault());
        }
示例#10
0
        /// <summary>
        /// Updates notification device for account
        /// </summary>
        /// <param name="device">type of device to use</param>
        /// <returns>User info</returns>
        public User UpdateAccountDeliveryDevice(DeviceType device)
        {
            var accountUrl = BaseUrl + "account/update_delivery_device.xml";

            var results =
                TwitterExecute.ExecuteTwitter(
                    accountUrl,
                    new Dictionary <string, string>
            {
                { "device", device.ToString().ToLower() }
            },
                    new UserRequestProcessor());

            return((results as IList <User>).FirstOrDefault());
        }
示例#11
0
        /// <summary>
        /// deletes a direct message
        /// </summary>
        /// <param name="id">id of direct message</param>
        /// <returns>direct message element</returns>
        public DirectMessage DestroyDirectMessage(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("id is a required parameter.", "id");
            }

            var destroyUrl = BaseUrl + "direct_messages/destroy/" + id + ".xml";

            var results =
                TwitterExecute.ExecuteTwitter(
                    destroyUrl,
                    new Dictionary <string, string>(),
                    new DirectMessageRequestProcessor());

            return((results as IList <DirectMessage>).FirstOrDefault());
        }
示例#12
0
        /// <summary>
        /// Unblocks a user
        /// </summary>
        /// <param name="id">id of user to unblock</param>
        /// <returns>User that was unblocked</returns>
        public User DestroyBlock(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("id is a required parameter.", "id");
            }

            var blocksUrl = BaseUrl + "blocks/destroy/" + id + ".xml";

            var results =
                TwitterExecute.ExecuteTwitter(
                    blocksUrl,
                    new Dictionary <string, string>(),
                    new UserRequestProcessor());

            return((results as IList <User>).FirstOrDefault());
        }
示例#13
0
        /// <summary>
        /// Deletes a favorite from the logged-in user's profile
        /// </summary>
        /// <param name="id">id of status to add to favorites</param>
        /// <returns>status of favorite</returns>
        public Status DestroyFavorite(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("id is a required parameter.", "id");
            }

            var favoritesUrl = BaseUrl + "favorites/destroy/" + id + ".xml";

            var results =
                TwitterExecute.ExecuteTwitter(
                    favoritesUrl,
                    new Dictionary <string, string>(),
                    new StatusRequestProcessor());

            return((results as IList <Status>).FirstOrDefault());
        }
示例#14
0
        /// <summary>
        /// Ends the session for the currently logged in user
        /// </summary>
        /// <returns>true</returns>
        public TwitterHashResponse EndAccountSession()
        {
            var accountUrl = BaseUrl + "account/end_session.xml";

            var results =
                TwitterExecute.ExecuteTwitter(
                    accountUrl,
                    new Dictionary <string, string>(),
                    new AccountRequestProcessor());

            var acct = (results as IList <Account>).FirstOrDefault();

            if (acct != null)
            {
                return(acct.EndSessionStatus);
            }
            else
            {
                throw new WebException("Unknown Twitter Response.");
            }
        }
示例#15
0
        /// <summary>
        /// Disables notifications from specified user. (Notification Leave)
        /// </summary>
        /// <remarks>
        /// A least one parameter is required.
        /// </remarks>
        /// <param name="id">ID of user to disable notifications on.</param>
        /// <param name="userID">ID of user - disambiguates when ID is screen name.</param>
        /// <param name="screenName">Screen Name of user - disambiguates when ID is screen name.</param>
        /// <returns>Specified user info</returns>
        public User DisableNotifications(string id, string userID, string screenName)
        {
            if (string.IsNullOrEmpty(id) &&
                string.IsNullOrEmpty(userID) &&
                string.IsNullOrEmpty(screenName))
            {
                throw new ArgumentException("Either id, userID, or screenName is a required parameter.");
            }

            var notificationsUrl = BaseUrl + "notifications/leave/" + id + ".xml";

            var results =
                TwitterExecute.ExecuteTwitter(
                    notificationsUrl,
                    new Dictionary <string, string>
            {
                { "user_id", userID },
                { "screen_name", screenName }
            },
                    new UserRequestProcessor());

            return((results as IList <User>).FirstOrDefault());
        }
示例#16
0
        /// <summary>
        /// Called by QueryProvider to execute queries
        /// </summary>
        /// <param name="expression">ExpressionTree to parse</param>
        /// <returns>list of objects with query results</returns>
        internal object Execute(Expression expression, bool isEnumerable)
        {
            Dictionary <string, string> parameters = null;

            // request processor is specific to request type (i.e. Status, User, etc.)
            var reqProc = CreateRequestProcessor(expression, isEnumerable);

            // we need the where expression because it contains the criteria for the request
            var whereFinder     = new FirstWhereClauseFinder();
            var whereExpression = whereFinder.GetFirstWhere(expression);

            if (whereExpression != null)
            {
                var lambdaExpression = (LambdaExpression)
                                       ((UnaryExpression)(whereExpression.Arguments[1])).Operand;

                // translate variable references in expression into constants
                lambdaExpression = (LambdaExpression)Evaluator.PartialEval(lambdaExpression);

                parameters = reqProc.GetParameters(lambdaExpression);
            }

            // construct REST endpoint, based on input parameters
            var url = reqProc.BuildURL(parameters);

            // execute the query and return results
            var queryableList = TwitterExecute.QueryTwitter(url, reqProc);

            if (isEnumerable)
            {
                return(queryableList);
            }
            else
            {
                return(queryableList[0]);
            }
        }
 public TwitterExecuteErrorHandlingTests()
 {
     var authMock = new Mock<ITwitterAuthorizer>();
     twitExe = new TwitterExecute(authMock.Object);
 }
        public void TwitterContext_Three_Param_Constructor_Sets_Defaults()
        {
            ITwitterExecute execute = new TwitterExecute(new PinAuthorizer());
            const string BaseUrl = "http://api.twitter.com/1/";
            const string SearchUrl = "http://search.twitter.com/";
            var ctx = new TwitterContext(execute, BaseUrl, SearchUrl);

            Assert.Equal(BaseUrl, ctx.BaseUrl);
            Assert.Equal(SearchUrl, ctx.SearchUrl);
        }