GPS Event [7] – tweet
"When this happens, notify me." The basic expectation one could have from an EP application. Once upon a time, automated pager notification systems would cost a fortune, but times have changed for the better. Here is what it takes for our event (see pervious posts) to show up on twitter.
First we need a twitter account and a desktop client. It takes less than five minutes to get both of these, so just do it.
For this example I needed two accounts, one for me and the other one for the event generator (GPS). Instead of creating a new account for the GPS, I have simply borrowed my cat's account — just for the experiment.
Time for all that refactoring from the previous post to pay off; only four steps in adding a service now:
- Add template module;
- add authorization module;
- add service module;
- select the service in the gps_event.py.
Twitter is a short message service, so I have selected only: entity, time_stamp, latitude and longitude for this template.
gps_template_tweet.py
generic tweeter template for GPS event
"""
def template(pretty = False):
"""$entity_here$; $time_stamp_here$;
$latitude_here$; $longitude_here$"""
return compress_template(template.__doc__)
def compress_template(s):
return ' '.join(s.split())
if __name__ == "__main__":
print template(True)
print template()
Authorization data.
gps_auth_tweet.py
Authorization strings for Tweeter
"""
tw_user = 'username_here'
tw_pass = 'password_here'
if __name__ == "__main__":
print 'nuser= %snpassword= %s'% (tw_user, tw_pass)
Service module sends messages.
gps_service_tweet.py
Tweeter service for gps event
"""
import urllib
import gps_auth_tweet
(tw_max, rsp) = (140, '')
def send(msg):
global rsp
if len(msg)> tw_max:
s = msg[:tw_max-3] + '...'
else:
s = msg
dta = urllib.urlencode({'status':s})
tw_url = "http://%s:%s@twitter.com/statuses/update.xml" %
(gps_auth_tweet.tw_user, gps_auth_tweet.tw_pass)
try:
rsp = urllib.urlopen(tw_url, dta)
r = True
except:
r = False
return r
if __name__ == "__main__":
print send('Test msg')
print rsp.read()
Select 'tweet' as a service and 180 seconds for the delay in the gps_event.py. Twitter currently limits hourly number of request to about 40, so we'll be at half of that.
use_service = 'tweet'
# define sampling delay in seconds
sample_delay = 180
Those following phoebe_cat on twitter may have seen tweets like this one lately.
To be continued.
Leave a comment