-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredactedtweets.py
84 lines (73 loc) · 2.46 KB
/
redactedtweets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#! /usr/bin/env python
#
# redactedtweets.py
# Nick Loadholtes <[email protected]>
# Sept 26, 2011
#
import tweepy
import ConfigParser
import codecs
REDACTED_CHAR = u'\u2588'
ACTION_WORDS = ('to', 'with', 'in', 'the',)
TEST_TWEET = "I went to work with gusto."
api = None
config = None
def getTweets(api):
return [(x.user.name, '@' +str(x.user.screen_name), x.text, x.id) for x in api.home_timeline()]
def postTweet(api, tweettext):
pass
def scanTweets(tweets, action_words, redacted_char):
output = []
for tweet in tweets:
if tweet[2].find('RT @') != -1:
continue
tmp, count = redactTweet(tweet[2], action_words, redacted_char)
if tmp is not None and count > 1:
output.append((tweet[1] ,tmp, tweet[2]))
return output
def redactTweet(tweet, action_words, redacted_char):
words = tweet.split(' ')
output = unicode("", errors='ignore')
redact = None
count = 0
for word in words:
if word in action_words:
redact = word
if redact and redact != word and len(word) > 1:
tmp = [redacted_char for x in word] #length of word, substitute redacted_char
output += u''.join(tmp)
redact = None
count += 1
else:
output += word
output += " "
output = output.strip()
if output == tweet.strip():
return None, count
return output, count
def test():
result = scanTweets([TEST_TWEET])
print "->" + TEST_TWEET
print "".join(result)
assert(result != TEST_TWEET)
def main(configfilename='config.cfg'):
config = ConfigParser.RawConfigParser()
config.readfp(codecs.open(configfilename, "r", "utf8"))
auth = tweepy.OAuthHandler(config.get('twitter', 'CONSUMER_KEY'),
config.get('twitter', 'CONSUMER_SECRET'))
auth.set_access_token(config.get('twitter', 'ACCESS_TOKEN_KEY'),
config.get('twitter', 'ACCESS_TOKEN_SECRET'))
actionwords = config.get('twitter', 'ACTION_WORDS')
redactedchar = config.get('twitter', 'REDACTED_CHAR')
if actionwords is None:
actionwords = ACTION_WORDS
if redactedchar is None:
redactedchar = REDACTED_CHAR
api = tweepy.API(auth)
tweets = getTweets(api)
for x in scanTweets(tweets, actionwords, redactedchar):
txt = "RT " + x[0] + " " + x[1]
api.update_status(txt[:140], in_reply_to_status_id=x[2])
if __name__ == "__main__":
#test()
main()