Permalink
Browse files

twitter posting on events now uses the GAE taskqueue for async goodness

  • Loading branch information...
1 parent 4013ce3 commit fcc167e90b016aeeca12a3fda3087135388065ae Charlie Hsu committed Jul 31, 2012
Showing with 38 additions and 27 deletions.
  1. +34 −0 stashboard/handlers/admin.py
  2. +3 −27 stashboard/handlers/api.py
  3. +1 −0 stashboard/main.py
View
34 stashboard/handlers/admin.py
@@ -8,12 +8,15 @@
from google.appengine.api import taskqueue
from google.appengine.api import users
from google.appengine.ext import db
+from google.appengine.ext import webapp
from handlers import api
from handlers import site
from models import List, Service, Status, Event, Image, Profile
from utils import slugify
import oauth2 as oauth
+import socket
+import urllib
def default_template_data():
td = site.default_template_data()
@@ -430,6 +433,37 @@ def get(self):
self.redirect("/admin/credentials")
+class EventTweetHandler(webapp.RequestHandler):
+ def post(self):
+ if not (settings.TWITTER_CONSUMER_KEY and settings.TWITTER_CONSUMER_SECRET and \
+ settings.TWITTER_ACCESS_TOKEN and settings.TWITTER_ACCESS_TOKEN_SECRET):
+ logging.error('Twitter credentials not configured properly in settings.py')
+ return
+
+ service_name = self.request.get('service_name')
+ status_name = self.request.get('status_name')
+ message = self.request.get('message')
+
+ if not service_name or not status_name or not message:
+ logging.error('Internal Twitter endpoint not called correctly')
+ return
+
+ consumer = oauth.Consumer(key=settings.TWITTER_CONSUMER_KEY, secret=settings.TWITTER_CONSUMER_SECRET)
+ token = oauth.Token(key=settings.TWITTER_ACCESS_TOKEN, secret=settings.TWITTER_ACCESS_TOKEN_SECRET)
+
+ client = oauth.Client(consumer, token, timeout=10)
+
+ try:
+ resp, content = client.request(
+ 'http://api.twitter.com/1/statuses/update.json',
+ method='POST',
+ body=urllib.urlencode({'status': '[%s - %s] %s' % (service_name, status_name, message)})
+ )
+ logging.info('Tweet successful: [%s - %s] %s' % (service_name, status_name, message))
+ except socket.timeout:
+ logging.error('Unable to post to Twitter API.')
+
+
class InvalidateCacheHandler(site.BaseHandler):
def get(self):
View
30 stashboard/handlers/api.py
@@ -32,7 +32,6 @@
import re
import os
import cgi
-import urllib
import logging
from datetime import timedelta
@@ -47,9 +46,6 @@
from google.appengine.ext import webapp
from google.appengine.ext import db
from handlers import restful
-import oauth2 as oauth
-import settings
-import socket
from time import mktime
from utils import authorized
from utils import slugify
@@ -324,9 +320,6 @@ def delete(self, version, service_slug):
class EventsListHandler(restful.Controller):
def get(self, version, service_slug):
-
- consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
- token = oauth.Token(key=key, secret=secret)
if not self.valid_version(version):
self.error(404, "API Version %s not supported" % version)
return
@@ -401,27 +394,10 @@ def post(self, version, service_slug):
e.informational = informational and informational == "true"
e.put()
- # Post to Twitter
+ # Queue up a task that calls the Twitter API to make a tweet.
if self.request.get('tweet'):
- logging.info('Attempting to post a tweet for the latest event')
- if not (settings.TWITTER_CONSUMER_KEY and settings.TWITTER_CONSUMER_SECRET and \
- settings.TWITTER_ACCESS_TOKEN and settings.TWITTER_ACCESS_TOKEN_SECRET):
- logging.error('Twitter credentials not configured properly in settings.py')
- else:
- consumer = oauth.Consumer(key=settings.TWITTER_CONSUMER_KEY, secret=settings.TWITTER_CONSUMER_SECRET)
- token = oauth.Token(key=settings.TWITTER_ACCESS_TOKEN, secret=settings.TWITTER_ACCESS_TOKEN_SECRET)
-
- client = oauth.Client(consumer, token, timeout=10)
-
- try:
- resp, content = client.request(
- 'http://api.twitter.com/1/statuses/update.json',
- method='POST',
- body=urllib.urlencode({'status': '[%s - %s] %s' % (service.name, status.name, message)})
- )
- logging.info('Tweet successful: [%s - %s] %s' % (service.name, status.name, message))
- except socket.timeout:
- logging.error('Unable to post to Twitter API.')
+ logging.info('Attempting to post a tweet for the latest event via async GAE task queue.')
+ taskqueue.add(url='/admin/tweet', params={'service_name': service.name, 'status_name': status.name, 'message': message})
invalidate_cache()
self.json(e.rest(self.base_url(version)))
View
1 stashboard/main.py
@@ -92,6 +92,7 @@
(r'/admin/oauth/authorize', admin.OAuthRequestHandler),
(r'/admin/oauth/verify', admin.OAuthVerifyHandler),
(r'/admin/tasks/invalidate-cache', admin.InvalidateCacheHandler),
+ (r'/admin/tweet', admin.EventTweetHandler),
(r'/admin', admin.RootHandler),
]

0 comments on commit fcc167e

Please sign in to comment.