-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy patheverywordbot.py
133 lines (115 loc) · 5.19 KB
/
everywordbot.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
import tweepy
import os
class EverywordBot(object):
def __init__(self, consumer_key, consumer_secret,
access_token, token_secret,
source_file_name, index_file_name,
lat=None, long=None, place_id=None,
prefix=None, suffix=None, bbox=None,
dry_run=False):
self.source_file_name = source_file_name
self.index_file_name = index_file_name
self.lat = lat
self.long = long
self.place_id = place_id
self.prefix = prefix
self.suffix = suffix
self.bbox = bbox
self.dry_run = dry_run
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, token_secret)
self.twitter = tweepy.API(auth)
def _get_current_index(self):
if not(os.path.isfile(self.index_file_name)):
return 0
with open(self.index_file_name) as index_fh:
return int(index_fh.read().strip())
def _increment_index(self, index):
with open(self.index_file_name, "w") as index_fh:
index_fh.truncate()
index_fh.write("%d" % (index + 1))
index_fh.close()
def _get_current_line(self, index):
found = False
with open(self.source_file_name) as source_fh:
# read the desired line
for i, status_str in enumerate(source_fh):
if i == index:
found = True
break
if not found:
raise EOFError("No more words")
return status_str.strip()
def _random_point_in(self, bbox):
"""Given a bounding box of (swlat, swlon, nelat, nelon),
return random (lat, long)"""
import random
lat = random.uniform(bbox[0], bbox[2])
long = random.uniform(bbox[1], bbox[3])
return (lat, long)
def post(self):
index = self._get_current_index()
status_str = self._get_current_line(index)
if self.prefix:
status_str = self.prefix + status_str
if self.suffix:
status_str = status_str + self.suffix
if self.bbox:
self.lat, self.long = self._random_point_in(self.bbox)
if self.dry_run:
print(status_str)
else:
self.twitter.update_status(status=status_str,
lat=self.lat, long=self.long,
place_id=self.place_id)
self._increment_index(index)
def _csv_to_float_list(csv):
return list(map(float, csv.split(',')))
if __name__ == '__main__':
def _get_comma_separated_args(option, opt, value, parser):
setattr(parser.values, option.dest, _csv_to_float_list(value))
from optparse import OptionParser
parser = OptionParser()
parser.add_option('--consumer_key', dest='consumer_key',
help="twitter consumer key")
parser.add_option('--consumer_secret', dest='consumer_secret',
help="twitter consumer secret")
parser.add_option('--access_token', dest='access_token',
help="twitter token key")
parser.add_option('--token_secret', dest='token_secret',
help="twitter token secret")
parser.add_option('--source_file', dest='source_file',
default="tweet_list.txt",
help="source file (one line per tweet)")
parser.add_option('--index_file', dest='index_file',
default="index",
help="index file (must be able to write to this file)")
parser.add_option('--lat', dest='lat',
help="The latitude for tweets")
parser.add_option('--long', dest='long',
help="The longitude for tweets")
parser.add_option('--place_id', dest='place_id',
help="Twitter ID of location for tweets")
parser.add_option('--bbox', dest='bbox',
type='string',
action='callback',
callback=_get_comma_separated_args,
help="Bounding box (swlat, swlon, nelat, nelon) "
"of random tweet location")
parser.add_option('--prefix', dest='prefix',
help="string to add to the beginning of each post "
"(if you want a space, include a space)")
parser.add_option('--suffix', dest='suffix',
help="string to add to the end of each post "
"(if you want a space, include a space)")
parser.add_option('-n', '--dry_run', dest='dry_run', action='store_true',
help="Do everything except actually send the tweet or "
"update the index file")
(options, args) = parser.parse_args()
bot = EverywordBot(options.consumer_key, options.consumer_secret,
options.access_token, options.token_secret,
options.source_file, options.index_file,
options.lat, options.long, options.place_id,
options.prefix, options.suffix, options.bbox,
options.dry_run)
bot.post()