-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTweetBot.py
107 lines (88 loc) · 3.07 KB
/
TweetBot.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
# Imports for sending tweets
import tweepy
import KeyParser
# Imports for Google Static Maps
from io import BytesIO
from PIL import Image
from urllib import request
# Debug library
from icecream import ic
'''
Helper method for creating a tag
'''
def tag(str=None):
if str is None:
return None
else:
return '@'+str
'''
Helper method for creating a hash
'''
def hash(str=None):
if str is None:
return None
else:
return '#'+str
'''
Uses Google Static Maps API to create a screenshot of
a centered map with a red marker at target locations
Name of the file created is MAP.gif, the name is constant,
since we do not need to save the screenshot for every tweet created.
Returns the name of the file created.
'''
def getMap(geo_lat, geo_long):
url = "http://maps.googleapis.com/maps/api/staticmap?center="+str(geo_lat)+\
","+str(geo_long)+"&size=800x800&markers=color:red%7Clabel:HAVANA%7C"+str(geo_lat)+\
","+str(geo_long)+"&zoom=14&sensor=false"
# BytesIO creates in-memory file object
# buffer can be used as input or output to most
# functions that expect a standard file object.
buffer = BytesIO(request.urlopen(url).read())
# Open the image from buffer (simulates a file)
# Save the buffer as a GIF (create the map image)
Image.open(buffer).save('MAP.gif','GIF')
return 'MAP.gif'
'''
Authenticate for Twitter
'''
def get_api(cfg):
auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
return tweepy.API(auth)
'''
Create and post a tweet with a message body, lat, and long
eg. tweet("Oh na na na na", 39.0392, 125.7625)
'''
def tweet(tweet=None, geo_lat=None, geo_long=None):
# Key value mapping for Twitter keys
cfg = {
"consumer_key" : KeyParser.consumer_key,
"consumer_secret" : KeyParser.consumer_secret,
"access_token" : KeyParser.access_token,
"access_token_secret" : KeyParser.access_token_secret
}
try:
api = get_api(cfg)
except:
print('Could not connect to Twitter API!')
if tweet is None:
raise Exception('No msg for tweet!')
# If only msg provided, send plain tweet
if geo_lat is None and geo_long is None:
status = api.update_status(status=tweet)
# Accept lat as float or int
if geo_lat is not None and not isinstance(geo_lat, float):
if not isinstance(geo_lat, int):
raise Exception('Latitude should be a float or int')
# Accept long as float or int
if geo_long is not None and not isinstance(geo_long, float):
if not isinstance(geo_long, int):
raise Exception('Longitude should be a float or int')
try:
# Send a tweet with map, lat, long
gif = getMap(geo_lat, geo_long)
status = api.update_with_media(filename=gif, status=tweet,lat=geo_lat,long=geo_long)
except:
ic("Error querying GoogleMaps")
# Send a tweet with lat, long
status = api.update_status(status=tweet,lat=geo_lat,long=geo_long)