Created
October 9, 2011 01:03
-
-
Save mbmccormick/1273130 to your computer and use it in GitHub Desktop.
Creating a Twitter Robot using Google App Engine (Part 1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Update(webapp.RequestHandler): | |
def get(self): | |
botlist = db.GqlQuery('SELECT * FROM Weatherbot ORDER BY zipcode ASC') | |
failed = False | |
try: | |
for bot in botlist: | |
tweet = self.get_feed(bot.wundurl) | |
payload = {'status' : tweet, 'source' : "tweatherbot"} | |
payload = urllib.urlencode(payload) | |
base64string = base64.encodestring('%s:%s' % (bot.username, bot.password))[:-1] | |
headers = {'Authorization': "Basic %s" % base64string} | |
url = 'http://twitter.com/statuses/update.xml' | |
result = urlfetch.fetch(url, payload=payload, method=urlfetch.POST, headers=headers) | |
bot.lastupdate = datetime.now() | |
self.response.out.write(result.content) | |
except Exception, detail: | |
self.response.out.write('An error occurred while accessing Twitter.\n\nException: %s' % detail) | |
failed = True | |
if failed == False: | |
db.put(botlist) | |
self.redirect('/') | |
def get_feed(self, url): | |
result = urlfetch.fetch(url) | |
for line in result.content.splitlines(): | |
if line.startswith('DESCRIPTION:'): | |
return line.replace('DESCRIPTION:', '').replace('\\n', '').strip() | |
def get_status(self, id): | |
current = urlfetch.fetch('http://twitter.com/statuses/user_timeline/%s.xml' % id) | |
for line in current.content.splitlines(): | |
if line.strip().startswith('<text>'): | |
return line.replace('<text>', '').replace('\\n', '').replace('</text>', '').strip() | |
def get_updatetime(self, id): | |
current = urlfetch.fetch('http://twitter.com/statuses/user_timeline/%s.xml' % id) | |
for line in current.content.splitlines(): | |
if line.strip().startswith('<created_at>'): | |
return line.replace('<created_at>', '').replace('\\n', '').replace('</created_at>', '').strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment