Skip to content

Instantly share code, notes, and snippets.

@ayanamist
Created June 30, 2011 14:05
Show Gist options
  • Select an option

  • Save ayanamist/1056298 to your computer and use it in GitHub Desktop.

Select an option

Save ayanamist/1056298 to your computer and use it in GitHub Desktop.
Google App Engine Twitter API Proxy (T Mode)
application: gtap
version: 1
runtime: python
api_version: 1
handlers:
- url: /t/.*
script: main.py
#!/usr/bin/python
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
IGNORE_URLFETCH_HEADERS = ('accept-encoding', 'content-length', 'host', 'vary', 'via', 'x-forwarded-for')
IGNORE_RESPONSE_HEADERS = ('content-encoding', 'content-length', 'date' , 'server', 'transfer-encoding')
class BaseHandler(webapp.RequestHandler):
_base_host = 'https://api.twitter.com/1/'
def post(self, path):
self.fetch('POST', path);
def get(self, path):
self.fetch('GET', path)
def fetch(self, method, path):
new_url = self._base_host + path
if self.request.query_string:
new_url += '?' + self.request.query_string
new_headers = dict()
for k in self.request.headers.keys():
if k.lower() not in IGNORE_URLFETCH_HEADERS:
new_headers[k] = self.request.headers.get(k)
result = urlfetch.fetch(url=new_url, method=method, headers=new_headers, payload=self.request.body)
self.response.set_status(result.status_code)
for k,v in result.headers.iteritems():
if k.lower() not in IGNORE_RESPONSE_HEADERS:
self.response.headers.add_header(k, v)
self.response.out.write(result.content)
class SearchHandler(BaseHandler):
_base_host = 'https://search.twitter.com/'
if __name__ == "__main__":
application = webapp.WSGIApplication([('/t/search/(.*)', SearchHandler),
('/t/(.*)', BaseHandler)])
run_wsgi_app(application)
@ayanamist
Copy link
Author

Use http://yourapp.appspot.com/t/ as Gravity Transport Mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment