public static void FollowersIds()
        {
            OAuthTokens tokens = Configuration.GetTokens();

            TwitterResponse <UserIdCollection> response = TwitterFriendship.FollowersIds(tokens, new UsersIdsOptions
            {
                ScreenName = "twitterapi"
            });

            Assert.IsNotNull(response);
            Assert.That(response.Result == RequestResult.Success);
            Assert.IsNotNull(response.ResponseObject);
            Assert.That(response.ResponseObject.Count > 0);

            decimal firstId = response.ResponseObject[0];

            response = TwitterFriendship.FollowersIds(tokens, new UsersIdsOptions {
                ScreenName = "twitterapi", Cursor = response.ResponseObject.NextCursor
            });
            Assert.IsNotNull(response);
            Assert.That(response.Result == RequestResult.Success);
            Assert.IsNotNull(response.ResponseObject);
            Assert.That(response.ResponseObject.Count > 0);
            Assert.AreNotEqual(response.ResponseObject[0], firstId);
        }
Exemplo n.º 2
0
        public void FollowersIds()
        {
            OAuthTokens tokens = Configuration.GetTokens();

            TwitterResponse <UserIdCollection> response = TwitterFriendship.FollowersIds(tokens, new UsersIdsOptions
            {
                ScreenName = "twitterapi"
            });

            Assert.IsNotNull(response, "response is null");
            Assert.IsTrue(response.Result == RequestResult.Success, response.ErrorMessage ?? response.Result.ToString());
            Assert.IsNotNull(response.ResponseObject, response.ErrorMessage ?? response.Result.ToString());
            Assert.IsTrue(response.ResponseObject.Count > 0, response.ErrorMessage ?? response.Result.ToString());

            decimal firstId = response.ResponseObject[0];

            response = TwitterFriendship.FollowersIds(tokens, new UsersIdsOptions {
                ScreenName = "twitterapi", Cursor = response.ResponseObject.NextCursor
            });
            Assert.IsNotNull(response, "response is null");
            Assert.IsTrue(response.Result == RequestResult.Success, response.ErrorMessage ?? response.Result.ToString());
            Assert.IsNotNull(response.ResponseObject, response.ErrorMessage ?? response.Result.ToString());
            Assert.IsTrue(response.ResponseObject.Count > 0, response.ErrorMessage ?? response.Result.ToString());
            Assert.AreNotEqual(response.ResponseObject[0], firstId, response.ErrorMessage ?? response.Result.ToString());
        }
        public static CommandResult GetFollowerList(string[] screennames)
        {
            if (!IsInitiated)
            {
                return(CommandResult.NotInitiated);
            }

            foreach (var screenname in screennames)
            {
                var user = CheckUserExistsInDb(screenname);
                if (user == null)
                {
                    Form.AppendLineToOutput("Failed. @" + screenname + " not found in Db", Color.Maroon);
                    continue;
                }

                DateTime WhenSaved      = DateTime.Today;
                int      FollowersSaved = 0;
                int      NotSaved       = 0;
                long     NextCursor     = -1;

                #region log to file for resuming
                var logFilePath = @"./" + screenname + "-GetFollowerList.resume";

                if (File.Exists(logFilePath))
                {
                    var NextCursorAsText = File.ReadAllText(logFilePath);
                    long.TryParse(NextCursorAsText, out NextCursor);
                }
                #endregion

                do
                {
                    var db = new TwitterContext();
                    db.Configuration.AutoDetectChangesEnabled = false;

                    TwitterResponse <UserIdCollection> Response = TwitterFriendship.FollowersIds(Tokens, new UsersIdsOptions {
                        ScreenName = screenname, Cursor = NextCursor
                    });

                    if (Response.Result == RequestResult.Success && Response.ResponseObject != null)
                    {
                        File.WriteAllText(logFilePath, NextCursor.ToString()); //to note where to resume from

                        try
                        {
                            var followersID = Response.ResponseObject;
                            NextCursor = followersID.NextCursor;

                            foreach (var id in followersID.ToList())
                            {
                                try
                                {
                                    db.Friendships.Add(new Friendship {
                                        UserId = user.Id, FollowerId = id, WhenSaved = WhenSaved
                                    });
                                    FollowersSaved++;
                                    db.SaveChanges();
                                }
                                catch (Exception ex)
                                {
                                    Form.AppendLineToOutput(string.Format("Unexpected error with user {0} and follower {1} : {2}", user.ScreenName, id, ex.Message), Color.Maroon);
                                    db = new TwitterContext();
                                    NotSaved++;
                                }
                            }
                            Form.AppendLineToOutput(string.Format("Saved {0} followers so far; {1} not saved", FollowersSaved, NotSaved), Color.Maroon);
                        }
                        catch (Exception ex)
                        {
                            Form.AppendLineToOutput("Unexpected error: " + ex.Message);
                        }
                    }
                    else if (Response.Result == RequestResult.Unauthorized || Response.Result == RequestResult.FileNotFound)
                    {
                        Form.AppendLineToOutput("User " + user.ScreenName + " is now protected or no longer exists.", Color.Maroon);
                    }
                    else if (Response.Result == RequestResult.RateLimited)
                    {
                        WaitForRateLimitReset(Response);

                        Form.AppendLineToOutput("Resuming GetFollowerList command", Color.Maroon);
                        continue;
                    }
                    else //Just in case if the program is unexpectedly failed and closed, record last processed page number
                    {
                        Form.AppendLineToOutput("Error: " + Response.Result, Color.Maroon);

                        //StreamWriter w1 = new StreamWriter("SaveFollowingsList_PageSoFar.txt");
                        //w1.WriteLine("Record datetime: " + DateTime.Now);
                        //w1.WriteLine("Page number next to be: " + nextCursor);
                        //w1.Close();
                        //Console.WriteLine("End of Process");

                        break;
                    }
                } while (NextCursor > 0);

                if (File.Exists(logFilePath))
                {
                    File.Delete(logFilePath); //remove log file. We're done with this user now.
                }
            } //end loop for each screenname

            return(CommandResult.Success);
        }