Skip to content

Instantly share code, notes, and snippets.

@peteretelej
Last active November 5, 2017 10:59
Show Gist options
  • Save peteretelej/3022d1cbed320bd16b52899ca366c4e2 to your computer and use it in GitHub Desktop.
Save peteretelej/3022d1cbed320bd16b52899ca366c4e2 to your computer and use it in GitHub Desktop.
find twitter unfollowers
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"strings"
)
func main() {
dat, err := ioutil.ReadFile("untitled")
if err != nil {
log.Fatal("unable to readFile")
}
parts := bytes.Split(dat, []byte("ProfileCard-userFields"))
log.Printf("parts: %d", len(parts))
var unfollowers []string
parts = parts[1:]
for _, p := range parts {
if follows(p) {
continue
}
u := username(p)
if u != "" {
unfollowers = append(unfollowers, u)
}
}
var urls []string
for _, val := range unfollowers {
urls = append(urls, fmt.Sprintf("https://twitter.com/%s", val))
}
all := strings.Join(urls, "\n")
err = ioutil.WriteFile("out.txt", []byte(all), 0644)
if err != nil {
log.Fatal(err)
}
}
func follows(dat []byte) bool {
i := bytes.Index(dat, []byte("ProfileCard-bio"))
if i < 0 {
log.Fatalf("missing ProfileCard-bio: %s", dat)
}
return bytes.Contains(dat[:i], []byte("follows you"))
}
func username(dat []byte) string {
i := bytes.Index(dat, []byte("ProfileCard-screenname"))
j := bytes.Index(dat, []byte("screennameLink"))
if i < 0 || j < i {
return ""
}
s := string(dat[i:j])
i = strings.Index(s, "href=")
j = strings.Index(s, "class=\"ProfileCard")
if i < 0 || j < i {
return ""
}
return s[i+7 : j-2]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment