| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | # This script autoblocks followers which seem to be obvious wastes of online oxygen. |
|---|
| 4 | # It does it by parsing notification emails. You need to plug it into a mail client filter. |
|---|
| 5 | # Definitely works with claws-mail. Needs the twyt python library. |
|---|
| 6 | # Required parameters: message body, twitter username, twitter password |
|---|
| 7 | # I can see the prints if I run claws-mail from a terminal. Logging to follow, maybe. |
|---|
| 8 | |
|---|
| 9 | # Written by Adrianna Pinska |
|---|
| 10 | # Licence: GPLv3 |
|---|
| 11 | |
|---|
| 12 | import sys |
|---|
| 13 | import re |
|---|
| 14 | try: |
|---|
| 15 | from twyt import twitter, data |
|---|
| 16 | except ImportError: |
|---|
| 17 | print "Dude, you need to install the python twyt module for this to work." |
|---|
| 18 | sys.exit(1) |
|---|
| 19 | |
|---|
| 20 | # maximum permitted following:followers ratios, in decreasing levels of rating severity |
|---|
| 21 | MAX_FOLLOW_RATIO_WTF = 15 |
|---|
| 22 | MAX_FOLLOW_RATIO_HARD = 10 |
|---|
| 23 | MAX_FOLLOW_RATIO_SOFT = 5 |
|---|
| 24 | # number of followed users below which ratio is not enforced |
|---|
| 25 | FOLLOW_RATIO_GRACE = 50 |
|---|
| 26 | # number of followed users below which tweet minimum is not enforced |
|---|
| 27 | MIN_TWEET_GRACE = 50 |
|---|
| 28 | # maximum number of followed people permitted, in decreasing levels of rating severity |
|---|
| 29 | MAX_FOLLOWING_WTF = 900 |
|---|
| 30 | MAX_FOLLOWING_HARD = 600 |
|---|
| 31 | MAX_FOLLOWING_SOFT = 300 |
|---|
| 32 | # minimum number of tweets required, in decreasing levels of rating severity |
|---|
| 33 | MIN_TWEETS_WTF = 2 |
|---|
| 34 | MIN_TWEETS_HARD = 5 |
|---|
| 35 | |
|---|
| 36 | MAX_BAD_SCORE = 12 # block anyone with a higher score than this |
|---|
| 37 | |
|---|
| 38 | if len(sys.argv) < 4: |
|---|
| 39 | print "Need these parameters: message body, twitter username, twitter password" |
|---|
| 40 | sys.exit(1) |
|---|
| 41 | |
|---|
| 42 | filename, username, password = sys.argv[1:4] |
|---|
| 43 | filename = filename.replace("\\","") |
|---|
| 44 | |
|---|
| 45 | f = open(filename,"r") |
|---|
| 46 | text = f.read() |
|---|
| 47 | f.close() |
|---|
| 48 | |
|---|
| 49 | try: |
|---|
| 50 | user = re.search("\(([^ ]+)\) is now following", text).group(1) |
|---|
| 51 | followers = int(re.search("(\d+) follower", text).group(1)) |
|---|
| 52 | tweets = int(re.search("(\d+) tweet", text).group(1)) |
|---|
| 53 | following = int(re.search("following (\d+)", text).group(1)) |
|---|
| 54 | except Exception, e: |
|---|
| 55 | print "Out of cheese error, redo from start: %s" % e |
|---|
| 56 | sys.exit(1) |
|---|
| 57 | |
|---|
| 58 | if followers > 0: |
|---|
| 59 | follow_ratio = float(following) / followers |
|---|
| 60 | else: |
|---|
| 61 | follow_ratio = following |
|---|
| 62 | |
|---|
| 63 | print user, followers, tweets, following, follow_ratio |
|---|
| 64 | |
|---|
| 65 | bad_score = 0 |
|---|
| 66 | |
|---|
| 67 | if following > FOLLOW_RATIO_GRACE: |
|---|
| 68 | if follow_ratio > MAX_FOLLOW_RATIO_WTF: |
|---|
| 69 | bad_score += 15 |
|---|
| 70 | elif follow_ratio > MAX_FOLLOW_RATIO_HARD: |
|---|
| 71 | bad_score += 10 |
|---|
| 72 | elif follow_ratio > MAX_FOLLOW_RATIO_SOFT: |
|---|
| 73 | bad_score += 5 |
|---|
| 74 | |
|---|
| 75 | if following > MAX_FOLLOWING_WTF: |
|---|
| 76 | bad_score += 15 |
|---|
| 77 | elif following > MAX_FOLLOWING_HARD: |
|---|
| 78 | bad_score += 10 |
|---|
| 79 | elif following > MAX_FOLLOWING_SOFT: |
|---|
| 80 | bad_score += 5 |
|---|
| 81 | |
|---|
| 82 | if following > MIN_TWEET_GRACE: |
|---|
| 83 | if tweets < MIN_TWEETS_WTF: |
|---|
| 84 | bad_score += 15 |
|---|
| 85 | elif tweets < MIN_TWEETS_HARD: |
|---|
| 86 | bad_score += 10 |
|---|
| 87 | |
|---|
| 88 | # crap name |
|---|
| 89 | if re.search("\d{2,}$", user): |
|---|
| 90 | bad_score += 10 |
|---|
| 91 | |
|---|
| 92 | print bad_score |
|---|
| 93 | |
|---|
| 94 | if bad_score > MAX_BAD_SCORE: |
|---|
| 95 | print "BAD!" |
|---|
| 96 | try: |
|---|
| 97 | t = twitter.Twitter() |
|---|
| 98 | t.set_auth(username, password) |
|---|
| 99 | |
|---|
| 100 | friendstring = t.user_friends() |
|---|
| 101 | friends = data.simplejson.loads(friendstring) |
|---|
| 102 | friendnames = [f["screen_name"] for f in friends] |
|---|
| 103 | if unicode(user) in friendnames: |
|---|
| 104 | print "You're following this person! Not blocking." |
|---|
| 105 | else: |
|---|
| 106 | print "BLOCKING!" |
|---|
| 107 | t.block_create(user) |
|---|
| 108 | except Exception, e: |
|---|
| 109 | print "Out of cheese error, redo from start: %s" % e |
|---|
| 110 | sys.exit(1) |
|---|