-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjifbot.py
executable file
·178 lines (144 loc) · 5.12 KB
/
jifbot.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
# encoding: utf-8
"""
Twitter bot fixing English by following the lead of GIF.
"""
from __future__ import print_function, unicode_literals
import argparse
import random
import re
import sys
import twitter
import webbrowser
import yaml
TWITTER = None
def print_it(text):
""" cmd.exe cannot do Unicode so encode first """
print(text.encode('utf-8'))
def load_yaml(filename):
"""
File should contain:
consumer_key: TODO_ENTER_YOURS
consumer_secret: TODO_ENTER_YOURS
access_token: TODO_ENTER_YOURS
access_token_secret: TODO_ENTER_YOURS
"""
f = open(filename)
data = yaml.safe_load(f)
f.close()
if not data.viewkeys() >= {
'access_token', 'access_token',
'consumer_key', 'consumer_secret'}:
sys.exit("Twitter credentials missing from YAML: " + filename)
return data
def tweet_it(string, credentials):
""" Tweet string using credentials """
global TWITTER
if len(string) <= 0:
return
# Create and authorise an app with (read and) write access at:
# https://dev.twitter.com/apps/new
# Store credentials in YAML file
if TWITTER is None:
TWITTER = twitter.Twitter(auth=twitter.OAuth(
credentials['access_token'],
credentials['access_token_secret'],
credentials['consumer_key'],
credentials['consumer_secret']))
print_it("TWEETING THIS:\n" + string)
if args.test:
print("(Test mode, not actually tweeting)")
else:
result = TWITTER.statuses.update(status=string)
url = "http://twitter.com/" + \
result['user']['screen_name'] + "/status/" + result['id_str']
print("Tweeted:\n" + url)
if not args.no_web:
webbrowser.open(url, new=2) # 2 = open in a new tab, if possible
def random_line(afile):
"""
Based on algorithm R(3.4.2) (Waterman's "Reservoir Algorithm") from
Knuth's "The Art of Computer Programming"
"""
line = next(afile)
for num, aline in enumerate(afile):
if random.randrange(num + 2):
continue
line = aline
return line
def hardg(gwords):
""" Pick a random word and find its correct pronunciation """
print("Pick a random word...")
# This file is based on:
# http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b
with open(gwords) as afile:
word = random_line(afile).strip()
print_it(word)
correct_pronounciation = "j" + word[1:]
print_it(correct_pronounciation)
return word, correct_pronounciation
def get_a_tweet(query, credentials):
global TWITTER
if TWITTER is None:
TWITTER = twitter.Twitter(auth=twitter.OAuth(
credentials['access_token'],
credentials['access_token_secret'],
credentials['consumer_key'],
credentials['consumer_secret']))
tweets = TWITTER.search.tweets(q=query, count=100)['statuses']
kept = []
print("Tweets found:", len(tweets))
for tweet in tweets:
text = tweet['text']
if "@" in text:
continue # No spamming
if "jif" in text.lower() and "gif" in text.lower():
kept.append(text)
print("Tweets kept:", len(kept))
if kept:
rando = random.choice(kept)
print_it(rando)
return rando
# Nothing found
return None
def fix_tweet(tweet, word, correct_pronounciation):
""" In tweet, replace gif with word and jif with correct_pronounciation """
# Caps
tweet = re.sub("GIF", word.upper(), tweet)
tweet = re.sub("JIF", correct_pronounciation.upper(), tweet)
# Initial
tweet = re.sub("Gif", word.title(), tweet)
tweet = re.sub("Jif", correct_pronounciation.title(), tweet)
# Lower case, mixed case
tweet = re.sub("gif", word, tweet, flags=re.I)
tweet = re.sub("jif", correct_pronounciation, tweet, flags=re.I)
return tweet
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Twitter bot fixing English by following the lead of GIF.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'-g', '--gwords',
default='/Users/hugo/Dropbox/bin/data/hard-g-words.txt',
help="File containing a list of words mistakenly pronounced with "
"a hard G")
parser.add_argument(
'-y', '--yaml',
default='/Users/hugo/Dropbox/bin/data/jifbot.yaml',
help="YAML file location containing Twitter keys and secrets")
parser.add_argument(
'-nw', '--no-web', action='store_true',
help="Don't open a web browser to show the tweeted tweet")
parser.add_argument(
'-x', '--test', action='store_true',
help="Test mode: go through the motions but don't tweet anything")
args = parser.parse_args()
credentials = load_yaml(args.yaml)
word, correct_pronounciation = hardg(args.gwords)
print("Find a tweet...")
tweet = get_a_tweet("gif jif", credentials)
if not tweet:
sys.exit("No tweet found, maybe next time.")
tweet = fix_tweet(tweet, word, correct_pronounciation)
tweet_it(tweet, credentials)
# End of file