public static void LookupUsers() { OAuthTokens tokens = Configuration.GetTokens(); LookupUsersOptions options = new LookupUsersOptions(); options.ScreenNames.Add("twitterapi"); options.ScreenNames.Add("digitallyborn"); options.ScreenNames.Add("trixtur"); options.ScreenNames.Add("twit_er_izer"); var result = TwitterUser.Lookup(tokens, options); Assert.That(result.Result == RequestResult.Success); Assert.IsNotNull(result.ResponseObject); }
public static void LookupUsersById() { OAuthTokens tokens = Configuration.GetTokens(); TwitterIdCollection userIds = new TwitterIdCollection { 14725805, // digitallyborn 16144513, // twit_er_izer 6253282 // twitterapi }; var result = TwitterUser.Lookup(tokens, new LookupUsersOptions { UserIds = userIds }); Assert.That(result.Result == RequestResult.Success); Assert.IsNotNull(result.ResponseObject); }
public static CommandResult LookUpUsers(object[] list, ListType listType, int UsersPerLookUp) { if (!IsInitiated) { return(CommandResult.NotInitiated); } LookupUsersOptions lookupOptions = new LookupUsersOptions(); //can lookup 100 ids at a time for (int i = 0; i < list.Length; i += UsersPerLookUp) { if (listType == ListType.Ids) { lookupOptions.UserIds = new TwitterIdCollection(list.Skip(i).Take(UsersPerLookUp).Cast <decimal>().ToList <decimal>()); } else if (listType == ListType.Screennames) { lookupOptions.ScreenNames = new Collection <string>(list.Skip(i).Take(UsersPerLookUp).Cast <string>().ToList <string>()); } TwitterResponse <TwitterUserCollection> UserLookupResponse = null; try { UserLookupResponse = TwitterUser.Lookup(Tokens, lookupOptions); } catch (TwitterizerException) { throw; //something is wrong with Twitterizer } catch (Exception ex) { //do nothing (and manually try again later) Form.AppendLineToOutput("Failed. " + ex.Message, Color.DarkGreen); } if (UserLookupResponse.Result == RequestResult.Success && UserLookupResponse.ResponseObject != null) { //refactor this? //SaveUsers(user, userLookupResponse.ResponseObject); foreach (var user in UserLookupResponse.ResponseObject) { user.Status = null; //remove the user's latest tweet (this causes problems with FKs because // the tweet's retweet, entities, places, etc. don't exist in the DB. } using (var scope = new System.Transactions.TransactionScope()) { TwitterContext db = null; try { db = new TwitterContext(); db.Configuration.AutoDetectChangesEnabled = false; db.Configuration.ValidateOnSaveEnabled = false; var LastUser = UserLookupResponse.ResponseObject.Last(); db.Users.AddRange(UserLookupResponse.ResponseObject); db.SaveChanges(); //add up to 100 users Form.AppendLineToOutput(string.Format("Added {0} users. Last user added is : {1} ({2})", UserLookupResponse.ResponseObject.Count, LastUser.ScreenName, LastUser.Id), Color.DarkGreen); } catch (System.Data.Entity.Infrastructure.DbUpdateException ex) { db = new TwitterContext(); db.Configuration.AutoDetectChangesEnabled = false; db.Configuration.ValidateOnSaveEnabled = false; Form.AppendLineToOutput(string.Format("Error with some users : {0}", ex.Message), Color.DarkGreen); Form.AppendLineToOutput(string.Format("Continuing with remaining {0} users", list.Length - i), Color.DarkGreen); } catch (Exception ex) { Form.AppendLineToOutput(string.Format("Error : {0}", ex.Message), Color.DarkGreen); //do nothing, really TimeSpan _OneSecond = new TimeSpan(0, 0, 1); System.Threading.Thread.Sleep(_OneSecond); db = new TwitterContext(); //to clear the follower in the current context which caused the exception db.Configuration.AutoDetectChangesEnabled = false; db.Configuration.ValidateOnSaveEnabled = false; } finally { if (db != null) { db.Dispose(); } } scope.Complete(); } } else if (UserLookupResponse.Result == RequestResult.RateLimited) { WaitForRateLimitReset(UserLookupResponse); Form.AppendLineToOutput("Resuming LookUpUsers command", Color.DarkGreen); continue; } else { HandleTwitterizerError <TwitterUserCollection>(UserLookupResponse); } } return(CommandResult.Success); }