New Programmer / May be running into API Limit

Nov 12, 2014 at 2:51 AM
So real quick - I'm writing a console application that reads a list of twitter accounts. Let's say for example you are a video game enthusiast, and you want to follow everyone associated with Unity 3d. So you pull up the Unity 3d list of twitter handles, and auto follow them.

What I don't quite understand is exactly when an API call is used and counted against me in this scenario, so if someone could hold my hand for a minute and tell me how to improve this that would be great.

Right now the app runs, authenticates the user, and follows a fairly decent number of accounts. I'd say it gets 50 at a time. But I haven't necessarily timed it. A few times my program has prematurely printed "press any key..." statement towards the bottom.

How would I put in checks, and read those checks, to debug this? Thanks a bunch for your help.
            // Setup your credentials
            string ConsumerKey = "HIDDEN ON PURPOSE";
            string ConsumerSecret = "HIDDEN ON PURPOSE"
            var ApplicationCredentials = CredentialsCreator.GenerateApplicationCredentials(ConsumerKey, ConsumerSecret);

            // Authorize the app
            var URL = CredentialsCreator.GetAuthorizationURL(ApplicationCredentials);
            ILoggedUser LoggedUser = null;
            while(LoggedUser == null)
            {
                try
                {
                    Process.Start(URL);
                    Console.WriteLine("We directed you to the following address: {0}, so you can authorize"
                    + " our app to access your Twitter Account. Please enter the captcha code provided on the"
                    + " website after loging in:", URL);
                    var Captcha = Console.ReadLine();
                    TwitterCredentials.ApplicationCredentials = CredentialsCreator.GetCredentialsFromVerifierCode(Captcha, ApplicationCredentials);
                    LoggedUser = User.GetLoggedUser(TwitterCredentials.ApplicationCredentials);
                    
                }
                catch (Exception)
                {
                    Console.WriteLine("\r\nWe couldn't log you in. Are you sure you typed the number correctly?"
                        + " Let's try again. Press any key to continue...\r\n");
                    Console.ReadLine();
                }
            }
            Console.WriteLine("\r\nWe successfully logged you in as: {0}\r\n", LoggedUser.Name);


            // Follow a list of accounts
            string[] users = System.IO.File.ReadAllLines(@"c:\twitterHandles.txt");
            if (users.Any())
            {
                foreach(string user in users.Skip(500))
                {
                    var userToFollow = User.GetUserFromScreenName(user);
                    if(userToFollow != null)
                    {
                        if (LoggedUser.FollowUser(userToFollow))
                        {
                            Console.WriteLine("You have successfully sent a request to follow {0}", userToFollow.Name);
                        }
                    }
                }
            }

            Console.WriteLine(@"\r\nThank you for following using our app!\r\nPress any key to quit the program.");
            Console.ReadLine();
Coordinator
Nov 12, 2014 at 11:54 PM
Hi,

It's getting late, I will try to answer your question tomorrow.

Regards,
Linvi
Nov 13, 2014 at 11:59 AM
linvi - I totally understand, not enough time in the day to do the things you like to do right? Thanks so much for your help, it's a great project and it's allowed me to get so far in such a short amount of time.
Editor
Nov 17, 2014 at 11:37 AM
Edited Nov 17, 2014 at 11:46 AM
You should be able to follow unlimited users because POST requests do not count towards your API limit. That is what a follow request uses.

The reason you're hitting the limit is because of:
User.GetUserFromScreenName(user)

The GET users/show call has an API limit associated to it.

One way to bypass this is to convert the list of screen names you have to user IDs by using User.GetUsersFromScreenNames() and then use ILoggedUser.FollowUser(long userId) to follow the users.

But we could definitely use an ILoggedUser.FollowUser(string screenname) overload if linvi had the time to implement it. ;)

If your application still ends prematurely, set ExceptionHandler.SwallowWebExceptions = false; and put your FollowUser call in a try catch block. This will tell you what's up.

imlokesh
Marked as answer by linvi on 12/2/2014 at 12:04 PM