Created
March 6, 2010 01:01
-
-
Save moshen/323380 to your computer and use it in GitHub Desktop.
This file contains 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
import tornado.httpclient | |
from urllib import urlencode | |
from hashlib import md5 | |
class admobAsync(): | |
""" | |
Does admob ad requests asynchronously. I don't know if this is the right way to do this, but I found it handy. Based on: http://www.djangosnippets.org/snippets/883/ | |
Use this class as RequestHandler a mixin like so: | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.web | |
from admobAsync import admobAsync | |
class MyHandler(tornado.web.RequestHandler , admobAsync): | |
def __init__(self): | |
tornado.web.RequestHandler.__init__(self, application, request, transforms) | |
admobAsync.__init__(self , 'my admob id') | |
class AdHandler(MyHandler): | |
@tornado.web.asynchronous | |
def get(self): | |
self.admob_ad_async(on_response) | |
def on_response(self, response): | |
self.write(response) | |
self.finish() | |
application = tornado.web.Application([ | |
(r"/", AdHandler), | |
]) | |
if __name__ == "__main__": | |
http_server = tornado.httpserver.HTTPServer(application) | |
http_server.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() | |
""" | |
def __init__(self , admob_id): | |
self.admob_params = {} | |
self.admob_params["admob_site_id"] = admob_id | |
@tornado.web.asynchronous | |
def admob_ad_async(self, callback): | |
# Change to "live" when ready to deploy. | |
admob_mode = "test" | |
admob_endpoint = "http://r.admob.com/ad_source.php" | |
admob_version = "20100222-PYTHON-TORNADO" | |
admob_timeout = 1.0 | |
admob_ignore = ("HTTP_PRAGMA", "HTTP_CACHE_CONTROL", "HTTP_CONNECTION", "User-Agent", "HTTP_COOKIE",) | |
# Build URL. | |
admob_post = {} | |
# Required Parameters - will raise if not found. | |
admob_post["s"] = self.admob_params["admob_site_id"] | |
# Meta Parameters. | |
admob_post["u"] = self.request.headers['User-Agent'] | |
admob_post["i"] = self.request.remote_ip | |
admob_post["p"] = "http://%s%s" % (self.request.host , self.request.uri) | |
#admob_post["t"] = md5(request.session.session_key).hexdigest() | |
# Hardcoded Parameters. | |
admob_post["e"] = "UTF-8" | |
admob_post["v"] = admob_version | |
# Optional Parameters. | |
admob_post["ma"] = self.admob_params.get("admob_markup", None) | |
admob_post["d[pc]"] = self.admob_params.get("admob_postal_code", None) | |
admob_post["d[ac]"] = self.admob_params.get("admob_area_code", None) | |
admob_post["d[coord]"] = self.admob_params.get("admob_coordinates", None) | |
admob_post["d[dob]"] = self.admob_params.get("admob_dob", None) | |
admob_post["d[gender]"] = self.admob_params.get("admob_gender", None) | |
admob_post["k"] = self.admob_params.get("admob_keywords", None) | |
admob_post["search"] = self.admob_params.get("admob_search", None) | |
for k, v in self.request.headers.items(): | |
if k not in admob_ignore: | |
admob_post["h[%s]" % k] = v | |
# Strip all ``None`` and empty values from admob_post. | |
for k, v in admob_post.items(): | |
if v is None or v == "": | |
admob_post.pop(k) | |
if admob_mode == "test": | |
admob_post["m"] = "test" | |
# Request the Ad. | |
http = tornado.httpclient.AsyncHTTPClient() | |
admob_data = urlencode(admob_post) | |
http.fetch(tornado.httpclient.HTTPRequest(url=admob_endpoint , method="POST" , body=admob_data), callback=self.async_callback( self._admob_callback , callback , admob_timeout , admob_post )) | |
def _admob_callback(self, callback, admob_timeout, admob_post, response): | |
admob_success = True | |
admob_contents = response.body | |
if admob_contents is None or admob_contents == "" or response.error: | |
admob_success = False | |
if not admob_success: | |
admob_contents = "<img src=\"http://t.admob.com/li.php/c.gif/%(admob_site_id)s/1/%(admob_timeout)F/%(absolute_uri)s\" alt=\"\" width=\"1\" height=\"1\" />" \ | |
% {"admob_site_id" : self.admob_params["admob_site_id"], | |
"admob_timeout" : admob_timeout, | |
"absolute_uri" : md5(admob_post["p"]).hexdigest()} | |
# DEBUG: | |
#print 'Connecting to: %s' % admob_endpoint | |
#print 'Sending Parameters:' | |
#print admob_post | |
#print 'Got reponse:' | |
#print admob_contents | |
callback(admob_contents) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment