Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Created December 16, 2015 07:15
Show Gist options
  • Save zaltoprofen/80467ced023a1fd3f27b to your computer and use it in GitHub Desktop.
Save zaltoprofen/80467ced023a1fd3f27b to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net/url"
"os"
"time"
"github.com/ChimeraCoder/anaconda"
"github.com/joho/godotenv"
)
func check(s *string, dict map[string]string, key string) bool {
str, ok := dict[key]
*s = str
return ok
}
func GetInactiveFollowers(api *anaconda.TwitterApi, blank time.Duration) []anaconda.User {
var inactiveFollowers []anaconda.User
v := url.Values{}
v.Set("count", "5000")
v.Set("cursor", "-1")
for v.Get("cursor") != "0" {
f, err := api.GetFollowersIds(v)
if err != nil {
log.Fatal(err)
}
v.Set("cursor", f.Next_cursor_str)
followerIds := f.Ids
for len(followerIds) != 0 {
val := url.Values{}
val.Set("include_entities", "true")
followers, err := api.GetUsersLookupByIds(followerIds[:100], val)
if err != nil {
log.Fatal(err)
}
for _, f := range followers {
if f.Status == nil {
continue
}
createdAt, err := f.Status.CreatedAtTime()
if err != nil {
log.Fatal(err)
}
duration := time.Since(createdAt)
if duration.Seconds() >= blank.Seconds() {
inactiveFollowers = append(inactiveFollowers, f)
}
}
if len(followerIds) > 100 {
followerIds = followerIds[100:]
} else {
followerIds = []int64{}
}
}
}
return inactiveFollowers
}
func main() {
env, err := godotenv.Read(os.Getenv("HOME") + "/.env")
if err != nil {
log.Fatal(err)
}
var ck, cs, at, as string
if !check(&ck, env, "CONSUMER_KEY") || !check(&cs, env, "CONSUMER_SECRET") ||
!check(&at, env, "ACCESS_TOKEN") || !check(&as, env, "ACCESS_TOKEN_SECRET") {
log.Fatal("Cannot construct twitter api")
}
anaconda.SetConsumerKey(ck)
anaconda.SetConsumerSecret(cs)
api := anaconda.NewTwitterApi(at, as)
for _, inactiveFollower := range GetInactiveFollowers(api, 30*24*time.Hour) {
for {
u, err := api.BlockUser(inactiveFollower.ScreenName, url.Values{})
if err != nil {
log.Println("Error while blocking:", inactiveFollower.ScreenName, ":", err)
if aerr, ok := err.(*anaconda.ApiError); ok {
if isRateLimitError, nextWindow := aerr.RateLimitCheck(); isRateLimitError {
log.Println("Waiting... for", nextWindow)
<-time.After(nextWindow.Sub(time.Now()))
continue
}
}
log.Println("Give up blocking:", inactiveFollower.ScreenName)
break
}
log.Println("Blocked:", u.ScreenName)
break
}
for {
u, err := api.UnblockUser(inactiveFollower.ScreenName, url.Values{})
if err != nil {
log.Println("Error while unblocking:", inactiveFollower.ScreenName, ":", err)
if aerr, ok := err.(*anaconda.ApiError); ok {
if isRateLimitError, nextWindow := aerr.RateLimitCheck(); isRateLimitError {
log.Println("Waiting... for", nextWindow)
<-time.After(nextWindow.Sub(time.Now()))
continue
}
}
log.Println("Give up unblocking:", inactiveFollower.ScreenName)
break
}
log.Println("Unblocked:", u.ScreenName)
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment