public static TwitterApi Login(IniSettings setting, string section) { string consumerKey = Convert.ToString(setting.GetValue(section, "ConsumerKey", string.Empty)); string consumerSecret = Convert.ToString(setting.GetValue(section, "ConsumerSecret", string.Empty)); string accessToken = Convert.ToString(setting.GetValue(section, "AccessToken", string.Empty)); string accessSecret = Convert.ToString(setting.GetValue(section, "AccessSecret", string.Empty)); if (string.IsNullOrWhiteSpace(consumerKey) || string.IsNullOrWhiteSpace(consumerSecret)) { setting.Save(); Console.WriteLine("Unable to get consumerKey / Secret. Please check config file."); Console.ReadKey(true); return(null); } TwitterApi api = new TwitterApi(); if (string.IsNullOrWhiteSpace(accessToken) || string.IsNullOrWhiteSpace(accessSecret)) { api.OAuth = new TwitterOAuth(consumerKey, consumerSecret); TwitterOAuth.TokenPair tokens = api.OAuth.RequestToken(); api.OAuth.User.Token = tokens.Token; api.OAuth.User.Secret = tokens.Secret; string authorizationUrlString = "https://api.twitter.com/oauth/authorize?oauth_token=" + tokens.Token; try { Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = authorizationUrlString }); } catch { Console.WriteLine($"Failed to open web browser.\nYou have to access manually this url:\n{authorizationUrlString}"); } string verifier; do { Console.Write("Please input verifier code : "); verifier = Console.ReadLine(); } while (string.IsNullOrWhiteSpace(verifier)); tokens = api.OAuth.AccessToken(verifier); if (tokens != null) { setting.SetValue(section, "AccessToken", accessToken = api.OAuth.User.Token = tokens.Token); setting.SetValue(section, "AccessSecret", accessSecret = api.OAuth.User.Secret = tokens.Secret); setting.Save(); } else { Console.WriteLine("Unable to login to your account."); api.OAuth = null; Console.ReadKey(true); return(api); } } api.OAuth = new TwitterOAuth(consumerKey, consumerSecret, accessToken, accessSecret); api.MyUserInfo = api.getMyUserInfo(); return(api); }
private static void Main() { Console.InputEncoding = Encoding.Unicode; Console.OutputEncoding = Encoding.Unicode; settings = new IniSettings(new FileInfo(ini_file)); Console.WriteLine("Loading login info..."); TwitterApi twitter = TwitterApi.Login(settings); if (twitter.OAuth?.User.Token == null) { return; } HashSet <string> blocklist = new HashSet <string>(); string readLine; if (!string.IsNullOrEmpty(twitter.MyUserInfo?.id_str)) { Console.WriteLine("Get My Block List... (Max 250000 per 15min)"); string cursor = "-1"; while (true) { try { UserIdsObject result = JsonConvert.DeserializeObject <UserIdsObject>(twitter.getMyBlockList(cursor)); while (result != null) { blocklist.UnionWith(result.ids); if (result.next_cursor == 0) { break; } result = JsonConvert.DeserializeObject <UserIdsObject>(twitter.getMyBlockList(cursor = result.next_cursor_str)); } break; } catch (RateLimitException) { if (Convert.ToBoolean(settings.GetValue("Configuration", "AutoRetry_GetBlockList", false)) == false) { Console.Write("Do you want retry get block list after 15min? (Yes/No/Auto)"); readLine = Console.ReadLine(); if (readLine != null) { if (readLine.ToUpper().Trim().StartsWith("N")) { break; } if (readLine.ToUpper().Trim().StartsWith("A")) { settings.SetValue("Configuration", "AutoRetry_GetBlockList", true); } } settings.Save(); } Console.WriteLine("Wait for 15min... The job will be resumed at : " + DateTime.Now.AddMinutes(15).ToString("hh:mm:ss")); Thread.Sleep(TimeSpan.FromMinutes(15)); } } } else { Console.WriteLine("Failed to get your info!"); Console.ReadKey(true); return; } Console.WriteLine($"Blocklist = {blocklist.Count}"); long count = 0; bool userStopped = false; RateLimitException rateLimit = null; foreach (string ids in blocklist) { count++; Console.WriteLine( $"Target= {(ids.Length < 18 ? ids : ids.Substring(0, 17) + "...")}, " + $"Progress= {count}/{blocklist.Count} ({Math.Round(count * 100 / (double)blocklist.Count, 2)}%)"); twitter.UnBlock(ids); if (!Console.KeyAvailable) { continue; } while (Console.KeyAvailable) { Console.ReadKey(true); } Console.WriteLine("Do you want stop reset blocklist?"); if (DialogResult.Yes != MessageBox.Show("Do you want stop reset blocklist?", "Stop ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { continue; } rateLimit = null; userStopped = true; break; } //Console.Write("Do you want export your block list? (Y/N) : "); //readLine = Console.ReadLine(); //if ((readLine != null) && readLine.ToUpper().Trim().Equals("Y")) // File.WriteAllText($"blocklist_{DateTime.Now:yyyy-MM-dd_HHmm}.csv", string.Join(",", blocklist)); Console.Write("Finished !"); settings.SetValue("Authenticate", "AccessToken", ""); settings.SetValue("Authenticate", "AccessSecret", ""); settings.Save(); readLine = Console.ReadLine(); }