/// <summary> /// Inits this instance. /// </summary> public override void Init() { LookupUsersOptions options = this.OptionalProperties as LookupUsersOptions; if (options == null) { throw new NullReferenceException("The optional parameters class is not valid for this command."); } if (options.UserIds.Count > 0) { this.RequestParameters.Add("user_id", string.Join(",", options.UserIds.Where(id => id > 0).Select(id => id.ToString()).ToArray())); } if (options.ScreenNames.Count > 0) { this.RequestParameters.Add("screen_name", string.Join(",", options.ScreenNames.ToArray())); } if (options.IncludeEntities) { this.RequestParameters.Add("include_entities", "true"); } }
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); }
/// <summary> /// Initializes a new instance of the <see cref="LookupUsersCommand"/> class. /// </summary> /// <param name="tokens">The tokens.</param> /// <param name="options">The options.</param> public LookupUsersCommand(OAuthTokens tokens, LookupUsersOptions options) : base(HTTPVerb.GET, "users/lookup.json", tokens, options) { if (tokens == null) { throw new ArgumentNullException("tokens"); } if (options == null) { throw new ArgumentNullException("options"); } if (options.ScreenNames.Count == 0 && options.UserIds.Count == 0) { throw new ArgumentException("At least one screen name or user id must be specified."); } }
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); }