Blocking exact tweets

While playing with the Twitter API via a Go lib I saw someone call on people to troll reply to a tweet with a specific piece of text.

This seemed like a really easy thing to search for and block. So I modified the little Go program from starting with ephemeral and put in this as the function run in main().

It searches for the string it’s given and then if the tweet consists solely of that text (compared by turning the string lowercase and then stripping out everything that’s not a letter) it blocks the user.

Blocked over 1,000 accounts just by running a single command. Nice.

func blockUsers(api *anaconda.TwitterApi, needle string) {
  re, _ := regexp.Compile("[^a-z]")
  ndl := re.ReplaceAllString(strings.ToLower(needle), "")

  args := url.Values{}
  args.Add("result_type", "mixed")
  args.Add("count", "100")

  results, err := api.GetSearch(needle, args)
  for true {
    if err != nil {
      break
    }
    log.Printf("metadata: %v", results.Metadata)
    if results.Metadata.Count <= 0 {
      break
    }
    for _, t := range results.Statuses {
      txt := re.ReplaceAllString(strings.ToLower(t.Text), "")
      if txt == ndl {
        log.Printf("[%s] %s", txt, t.User.ScreenName)
        _, _ = api.BlockUserId(t.User.Id, url.Values{})
      }
    }
    time.Sleep(time.Second * 5)
    results, err = results.GetNext(api)
  }
}