root/confluence/emailfilters/twitterbouncer.py

Revision 717, 3.3 kB (checked in by confluence, 6 months ago)

raised the followed ratio grace number

  • Property svn:executable set to *
Line 
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
12import sys
13import re
14try:
15    from twyt import twitter, data
16except 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
21MAX_FOLLOW_RATIO_WTF = 15
22MAX_FOLLOW_RATIO_HARD = 10
23MAX_FOLLOW_RATIO_SOFT = 5
24# number of followed users below which ratio is not enforced
25FOLLOW_RATIO_GRACE = 50
26# number of followed users below which tweet minimum is not enforced
27MIN_TWEET_GRACE = 50
28# maximum number of followed people permitted, in decreasing levels of rating severity
29MAX_FOLLOWING_WTF = 900
30MAX_FOLLOWING_HARD = 600
31MAX_FOLLOWING_SOFT = 300
32# minimum number of tweets required, in decreasing levels of rating severity
33MIN_TWEETS_WTF = 2
34MIN_TWEETS_HARD = 5
35
36MAX_BAD_SCORE = 12 # block anyone with a higher score than this
37
38if len(sys.argv) < 4:
39    print "Need these parameters: message body, twitter username, twitter password"
40    sys.exit(1)
41
42filename, username, password = sys.argv[1:4]
43filename = filename.replace("\\","")
44
45f = open(filename,"r")
46text = f.read()
47f.close()
48
49try:
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))
54except Exception, e:
55    print "Out of cheese error, redo from start: %s" % e
56    sys.exit(1)
57
58if followers > 0:
59    follow_ratio = float(following) / followers
60else:
61    follow_ratio = following
62
63print user, followers, tweets, following, follow_ratio
64
65bad_score = 0
66
67if 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
75if following > MAX_FOLLOWING_WTF:
76    bad_score += 15
77elif following > MAX_FOLLOWING_HARD:
78    bad_score += 10
79elif following > MAX_FOLLOWING_SOFT:
80    bad_score += 5
81
82if 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
89if re.search("\d{2,}$", user):
90    bad_score += 10
91
92print bad_score
93
94if 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)
Note: See TracBrowser for help on using the browser.