private static void GetTargetSearchResult(TwitterApi twitter, string target, bool isNewReq, HashSet <string> targetLists) { Console.WriteLine($"Search {target}..."); string json = twitter.searchPhase(Uri.EscapeDataString(target), isNewReq); if (!string.IsNullOrWhiteSpace(json)) { SearchResultObject result = JsonConvert.DeserializeObject <SearchResultObject>(json); while (result.search_metadata.count > 0) { targetLists.UnionWith(result.statuses.Select(x => x.user.id_str)); result = JsonConvert.DeserializeObject <SearchResultObject>( twitter.searchPhase(result.search_metadata.next_results, false)); if (result?.search_metadata.next_results == null) { break; } } } else { Console.WriteLine("There is no result."); } }
private static void GetTargetFollowers(TwitterApi twitter, string username, string cursor, HashSet <string> targetLists) { Console.WriteLine($"Get {username}'s Followers..."); string json = twitter.getFollowers(username, cursor); if (!string.IsNullOrWhiteSpace(json)) { UserIdsObject result = JsonConvert.DeserializeObject <UserIdsObject>(json); while (result != null) { targetLists.UnionWith(result.ids); if (result.next_cursor == 0) { break; } result = JsonConvert.DeserializeObject <UserIdsObject>(twitter.getFollowers(username, result.next_cursor_str)); } } else { Console.WriteLine("Unable to get target followers."); } }
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> whitelist = new HashSet <string>(); HashSet <string> blocklist = new HashSet <string>(); string readLine; if (!string.IsNullOrEmpty(twitter.MyUserInfo?.id_str)) { Console.WriteLine("Get My Friends..."); UserIdsObject result = JsonConvert.DeserializeObject <UserIdsObject>(twitter.getMyFriends(twitter.MyUserInfo.id_str, "-1")); while (result != null) { whitelist.UnionWith(result.ids); if (result.next_cursor == 0) { break; } result = JsonConvert.DeserializeObject <UserIdsObject>(twitter.getMyFriends(twitter.MyUserInfo.id_str, result.next_cursor_str)); } Console.Write("Do you want to include your Followers into whitelist? (Y/N)"); readLine = Console.ReadLine(); if ((readLine != null) && readLine.ToUpper().Trim().Equals("Y")) { Console.WriteLine("Get My Followers..."); result = JsonConvert.DeserializeObject <UserIdsObject>(twitter.getMyFollowers(twitter.MyUserInfo.id_str, "-1")); while (result != null) { whitelist.UnionWith(result.ids); if (result.next_cursor == 0) { break; } result = JsonConvert.DeserializeObject <UserIdsObject>(twitter.getMyFollowers(twitter.MyUserInfo.id_str, result.next_cursor_str)); } } Console.Write("Do you have backup of blocklist? (Y/N)"); readLine = Console.ReadLine(); if ((readLine != null) && readLine.ToUpper().Trim().Equals("Y")) { Console.Write("Enter path of your blocklist\n: "); string input = Console.ReadLine(); if ((input != null) && File.Exists(input.Replace("\"", ""))) { blocklist.UnionWith(File.ReadAllText(input.Replace("\"", "")).Split(',')); } } else { Console.WriteLine("Get My Block List... (Max 250000 per 15min)"); string cursor = "-1"; while (true) { try { 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($"Whitelist = {whitelist.Count}, Blocklist = {blocklist.Count}"); while (true) { Console.Write("Enter @usernames, search phase or filename to Block Them All\n: "); string input = Console.ReadLine(); if (input != null) { string[] targets = File.Exists(input.Replace("\"", "")) ? File.ReadAllText(input.Replace("\"", "")) .Split(new[] { ",", "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries) : input.Split(','); Console.WriteLine("Please check your input is correct!"); if (DialogResult.No == MessageBox.Show( string.Join("\n \n", "Please check your input is correct. ", string.Join("\n", targets), " Press Yes to go."), "Check your input is correct", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { continue; } while (Console.KeyAvailable) { Console.ReadKey(true); } bool userStopped = false; foreach (string target in targets) { if (userStopped) { break; } if (string.IsNullOrWhiteSpace(target)) { continue; } RateLimitException rateLimit = null; do { HashSet <string> targetLists = new HashSet <string>(); try { if (target.StartsWith("@")) { string username = target.Substring(1); blocklist.Add(twitter.Block(username, true)); GetTargetFollowers(twitter, username, rateLimit == null ? "-1" : rateLimit.cursor, targetLists); } else { if (rateLimit == null) { GetTargetSearchResult(twitter, target, true, targetLists); } else { GetTargetSearchResult(twitter, rateLimit.target, false, targetLists); } } rateLimit = null; } catch (RateLimitException e) { rateLimit = e; } Console.WriteLine("Processing list..."); long count = 0; foreach (string s in targetLists) { count++; if (whitelist.Contains(s) || blocklist.Contains(s)) { continue; } blocklist.Add(twitter.Block(s)); Console.WriteLine( $"Target= {(target.Length < 18 ? target : target.Substring(0, 17) + "...")}, " + $"Progress= {count}/{targetLists.Count} ({Math.Round(count * 100 / (double) targetLists.Count, 2)}%), Blocklist= {blocklist.Count}"); if (!Console.KeyAvailable) { continue; } while (Console.KeyAvailable) { Console.ReadKey(true); } Console.WriteLine("Do you want stop processing this list and targets?"); if (DialogResult.Yes != MessageBox.Show("Do you want stop processing this list and targets?", "Stop ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { continue; } rateLimit = null; userStopped = true; break; } if (rateLimit == null) { continue; } TimeSpan wait = rateLimit.thrownAt.AddMinutes(15) - DateTime.Now; if (wait < TimeSpan.Zero) { continue; } Console.WriteLine($"Wait {wait:g} for Rate limit..."); Thread.Sleep(wait); } while (rateLimit != null); } } Console.Write("Finished ! Do you want continue? (Y/N) : "); readLine = Console.ReadLine(); if ((readLine != null) && readLine.ToUpper().Trim().Equals("N")) { 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)); } }