コード例 #1
0
        public static IEnumerable <TwitterUser> FollowersList(long?userId = null, string scrrenName = null)
        {
            if (userId == null && scrrenName == null)
            {
                return(new List <TwitterUser>());
            }

            TweetSharp.ListFollowersOptions option = new ListFollowersOptions
            {
                UserId     = userId,
                ScreenName = scrrenName,
            };

            TwitterCursorList <TwitterUser> followers = service.ListFollowers(option);

            IEnumerable <TwitterUser> returns = followers?.ToList();

            while (followers?.NextCursor != null)
            {
                option.Cursor = followers.NextCursor;

                followers = service.ListFollowers(option);

                if (followers != null)
                {
                    returns = returns.Concat(followers.ToList());
                }
            }

            return(returns);
        }
コード例 #2
0
        // returns location of the first 1000 user's followers
        public List <string> GetFollowerLocations(string name)
        {
            List <string> followerLoc = new List <string>();
            var           options     = new ListFollowersOptions {
                ScreenName = name, Count = 200, Cursor = -1
            };
            var followers = _service.ListFollowers(options);
            int iter      = 0;

            while (followers.NextCursor != null && iter < 5)
            {
                followerLoc.AddRange(from user in followers where user.Location != string.Empty select user.Location);
                if (followers.NextCursor != null && followers.NextCursor != 0)
                {
                    options.Cursor = followers.NextCursor;
                    followers      = _service.ListFollowers(options);
                    iter++;
                }
                else
                {
                    break;
                }
            }
            return(followerLoc);
        }
コード例 #3
0
        public static void FollowBackNotFollowed()
        {
            string _consumerKey       = ConfigurationManager.AppSettings["consumerKey"];
            string _consumerSecret    = ConfigurationManager.AppSettings["consumerSecret"];
            string _accessToken       = ConfigurationManager.AppSettings["accessToken"];
            string _accessTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"];

            var service = new TwitterService(_consumerKey, _consumerSecret);

            service.AuthenticateWith(_accessToken, _accessTokenSecret);

            TwitterUser self = service.GetUserProfile(new GetUserProfileOptions()
            {
                IncludeEntities = false, SkipStatus = false
            });

            ListFollowersOptions options = new ListFollowersOptions();

            options.UserId = self.Id;
            options.IncludeUserEntities = true;
            TwitterCursorList <TwitterUser> followers = service.ListFollowers(options);

            foreach (var follow in followers)
            {
                service.FollowUser(new FollowUserOptions {
                    UserId = follow.Id
                });                                                              //seems to work w/ no errors, even if already followed
            }
        }
コード例 #4
0
        public static void TestAPI()
        {
            TwitterService twitterService = new TwitterService(ConsumerKey, ConsumerKeySecret);

            ListFollowersOptions userID = new ListFollowersOptions();

            //options.UserId = tuSelf.Id;
            userID.ScreenName          = "narendramodi";
            userID.IncludeUserEntities = true;
            userID.SkipStatus          = false;
            userID.Cursor = -1;
            TwitterCursorList <TwitterUser> followers = twitterService.ListFollowers(userID);

            while (followers != null)
            {
                foreach (TwitterUser follower in followers)
                {
                    //Do something with the user profile here
                }

                userID.Cursor = followers.NextCursor;
                followers     = twitterService.ListFollowers(userID);
            }
        }
コード例 #5
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            name = name ?? data?.name;

            if (name != null)
            {
                var twitterService = new TwitterService(
                    consumerKey: Environment.GetEnvironmentVariable("twitterconsumerkey"),
                    consumerSecret: Environment.GetEnvironmentVariable("twitterconsumersecret"));

                twitterService.AuthenticateWith(
                    token: Environment.GetEnvironmentVariable("twitteraccesstoken"),
                    tokenSecret: Environment.GetEnvironmentVariable("twitteraccesstokensecret"));

                var followerList = new List <string>();

                var options = new ListFollowersOptions();
                options.ScreenName          = name;
                options.IncludeUserEntities = false;
                options.SkipStatus          = true;
                options.Count  = 200;
                options.Cursor = -1;

                TwitterCursorList <TwitterUser> cursorList = null;
                do
                {
                    var cursorRequest = await twitterService.ListFollowersAsync(options);

                    if (cursorRequest.Response.Error != null)
                    {
                        return(new BadRequestObjectResult(cursorRequest.Response.Error.Message));
                    }

                    cursorList = cursorRequest.Value;
                    foreach (TwitterUser user in cursorList)
                    {
                        followerList.Add(user.ScreenName);
                    }

                    if (options.Cursor != -1)
                    {
                        break;                      //only load two pages (2*200 followers) because of rate limiting :(
                    }
                    options.Cursor = cursorList.NextCursor;
                } while(cursorList.NextCursor != null);

                return(new OkObjectResult($"Followers of {name} (continue with cursor {cursorList.NextCursor}):"
                                          + Environment.NewLine + string.Join(Environment.NewLine, followerList)));
            }
            else
            {
                return(new BadRequestObjectResult($"Please pass a name on the query string or in the request body."));
            }
        }