Created
February 22, 2010 22:46
-
-
Save moshen/311615 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 | |
def admob_ad(request, admob_params=None): | |
""" | |
Given a Tornado ``self.request`` object and dict of | |
admob parameters returns a Admob ad. | |
If no ad can be retrieved displays a one pixel | |
Admob tracker image. | |
Based on: http://www.djangosnippets.org/snippets/883/ | |
Usage: | |
def my_view(request): | |
admob_dict = {} | |
admob_dict["admob_site_id"] = "required_admob_site_id" | |
admob_dict["admob_postal_coode"] = "optional_postal_code" | |
admob_dict["admob_area_code"] = "optional_area_code" | |
admob_dict["admob_coordinates"] = "optional_coordinates" | |
admob_dict["admob_gender"] = "optional_gender" | |
admob_dict["admob_keywords"] = "optional_keywords" | |
admob_dict["admob_search"] = "optional_search" | |
ad = admob_ad(request, admob_dict) | |
return HttpResponse(ad) | |
""" | |
# 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"] = admob_params["admob_site_id"] | |
# Meta Parameters. | |
admob_post["u"] = request.headers['User-Agent'] | |
admob_post["i"] = request.remote_ip | |
admob_post["p"] = "http://%s%s" % (request.host , 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"] = admob_params.get("admob_markup", None) | |
admob_post["d[pc]"] = admob_params.get("admob_postal_code", None) | |
admob_post["d[ac]"] = admob_params.get("admob_area_code", None) | |
admob_post["d[coord]"] = admob_params.get("admob_coordinates", None) | |
admob_post["d[dob]"] = admob_params.get("admob_dob", None) | |
admob_post["d[gender]"] = admob_params.get("admob_gender", None) | |
admob_post["k"] = admob_params.get("admob_keywords", None) | |
admob_post["search"] = admob_params.get("admob_search", None) | |
for k, v in 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. | |
admob_success = True | |
try: | |
http = tornado.httpclient.HTTPClient() | |
admob_data = urlencode(admob_post) | |
admob_file = http.fetch(tornado.httpclient.HTTPRequest(url=admob_endpoint , method="POST" , body=admob_data)) | |
admob_contents = admob_file.body | |
if admob_contents is None or admob_contents == "": | |
admob_success = False | |
except Exception, e: | |
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" : 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 | |
return admob_contents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment