Beispiel #1
0
        public async Task OnGet([FromQuery(Name = "id")] string twitterId)
        {
            if (string.IsNullOrEmpty(twitterId))
            {
                Redirect("/Error");
            }

            TwitterId = twitterId;

            var authKeys = await GetTwitterAuthKeys();

            Auth.SetUserCredentials(_config["Authentication:Twitter:ConsumerKey"], _config["Authentication:Twitter:ConsumerSecret"], authKeys.accessToken, authKeys.accessTokenSecret);

            // TODO: Ensure we do not create more than 1000 lists

            var query = string.Format("https://api.twitter.com/1.1/friends/ids.json?screen_name={0}", twitterId);

            // Ensure that we can get some information
            ExceptionHandler.SwallowWebExceptions = false;

            var usersFriends = await Tweetinvi.UserAsync.GetFriendIds(twitterId, 5000);

            var newList = TwitterList.CreateList($"private_{twitterId}", Tweetinvi.Models.PrivacyMode.Private, $"The list of accounts that {twitterId} follows as of {DateTime.Today.ToShortDateString()}");

            // Need to limit to 5000 accounts
            var result = newList.AddMultipleMembers(usersFriends.Select(u => new UserIdentifier(u)));

            var rateLimits = await RateLimitAsync.GetCurrentCredentialsRateLimits();

            Console.WriteLine($"Result:  {result}");
        }
        public async Task OnGet([FromQuery(Name = "id")] string twitterId, [FromQuery] bool DoIt = false)
        {
            if (string.IsNullOrEmpty(twitterId))
            {
                Redirect("/Error");
            }

            var sw = new Stopwatch();

            var(consumerKey, consumerSecret, accessToken, accessTokenSecret) = await GetTwitterAuthKeys();

            ExceptionHandler.SwallowWebExceptions = false;

            Auth.SetUserCredentials(consumerKey, consumerSecret, accessToken, accessTokenSecret);

            LoggedInTwitterId = (await UserAsync.GetAuthenticatedUser()).ScreenName;
            TwitterId         = twitterId;
            ListName          = $"private_{twitterId}";

            // Ensure that we can get some information

            sw.Start();
            var usersFriends = await Tweetinvi.UserAsync.GetFriendIds(twitterId, 5000);

            sw.Stop();
            ListMemberCount = usersFriends.Count() + 1;
            _logger.LogInformation($"TweetInvi command ({sw.ElapsedMilliseconds}ms) UserAsync.GetFriendIds() for {{{twitterId}}} found {ListMemberCount} friends");
            var membersToAdd = usersFriends.Select(f => new UserIdentifier(f));
            var list         = await TwitterListAsync.GetExistingList(ListNameSlug, LoggedInTwitterId);

            if (list == null)
            {
                list = await TwitterListAsync.CreateList(
                    ListName,
                    PrivacyMode.Private,
                    $"The list of accounts that @{twitterId} follows as of {DateTime.UtcNow.ToLongDateString()}. Should have {membersToAdd.Count()} members.");

                _logger.LogInformation($"Created list {{{ListName}}}.");
            }
            else
            {
                TwitterListUpdateParameters updateParameter = new TwitterListUpdateParameters()
                {
                    // Name = ListName,
                    // PrivacyMode = PrivacyMode.Private,
                    Description = $"The list of accounts that @{twitterId} follows as of {DateTime.UtcNow.ToLongDateString()}. Should have {membersToAdd.Count()} members."
                };
                sw.Restart();
                var existingMembers = await list.GetMembersAsync(5000);

                sw.Stop();
                _logger.LogInformation($"TweetInvi command ({sw.ElapsedMilliseconds}ms) list.GetMembersAsync() for {{{list.Slug}}} found {existingMembers.Count()} members");
                membersToAdd = membersToAdd.Where(m => !existingMembers.Any(existing => m.Id == existing.Id));
                if (DoIt || !_env.IsDevelopment())
                {
                    var success = await list.UpdateAsync(updateParameter);

                    if (success)
                    {
                        _logger.LogInformation($"Updated description of {{{ListName}}}.");
                    }
                    else
                    {
                        _logger.LogError($"Updating description of {{{ListName}}} failed.");
                    }
                }
            }

            NoOfMembersToAdd = membersToAdd.Count();

            // Need to limit to 5000 accounts
            if (DoIt || !_env.IsDevelopment())
            {
                await list.AddMemberAsync(await UserAsync.GetUserFromScreenName(twitterId));

                sw.Restart();
                var result = await list.AddMultipleMembersAsync(membersToAdd);

                sw.Stop();
                ResponseString = result.ToString();
                if (result == MultiRequestsResult.Success)
                {
                    _logger.LogInformation($"TweetInvi command ({sw.ElapsedMilliseconds}ms) list.AddMultipleMembersAsync() for {{{list.Slug}}} adding {membersToAdd.Count()} members finished with result: {result}");
                }
                else
                {
                    _logger.LogError($"TweetInvi command ({sw.ElapsedMilliseconds}ms) list.AddMultipleMembersAsync() for {{{list.Slug}}} adding {membersToAdd.Count()} members finished with result: {result}");
                }
            }
            else
            {
                _logger.LogWarning($"DEV MODE: AddMultipleMembersToList: Didn't add {membersToAdd.Count()} to list {{{ListName}}}.");
                ResponseString = "Nothing added in Development Mode";
            }

            sw.Restart();
            NoOfActualMembers = (await list.GetMembersAsync(5000)).Count();
            sw.Stop();
            _logger.LogInformation($"TweetInvi command ({sw.ElapsedMilliseconds}ms) list.GetMembersAsync() for {{{list.Slug}}} found {NoOfActualMembers} members");

            var rateLimits = await RateLimitAsync.GetCurrentCredentialsRateLimits();
        }