- Author:
- johnboxall
- Posted:
- July 15, 2008
- Language:
- Python
- Version:
- .96
- Score:
- 1 (after 1 ratings)
Update:
Django AdMob pluggable app on github.
Given a Django request
object and dict of admob parameters returns a Admob ad.
If no ad can be retrieved displays a one pixel Admob tracker image.
For the future:
-
Make a template tag for this?
-
Filter out other unneeded META parameters for the admob_post dict?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | from urllib2 import urlopen
from urllib import urlencode
try:
# Python >= 2.5
from hashlib import md5
except ImportError:
# Python < 2.5
import md5
def admob_ad(request, admob_params=None):
"""
Given a Django ``request`` object and dict of
admob parameters returns a Admob ad.
If no ad can be retrieved displays a one pixel
Admob tracker image.
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 = "20080714-PYTHON"
admob_timeout = 1.0
admob_ignore = ("HTTP_PRAGMA", "HTTP_CACHE_CONTROL", "HTTP_CONNECTION", "HTTP_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.META.get("HTTP_USER_AGENT", None)
admob_post["i"] = request.META.get("REMOTE_ADDR", None)
admob_post["p"] = request.build_absolute_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.META.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:
admob_data = urlencode(admob_post)
admob_file = urlopen(admob_endpoint, admob_data)
admob_contents = admob_file.read()
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(request.build_absolute_uri()).hexdigest()}
# DEBUG:
# print 'Connecting to: %s' % admob_endpoint
# print 'Sending Parameters:'
# print admob_post
# print 'Got reponse:'
# print admob_contents
return admob_contents
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 8 months, 4 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 3 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
Thanks a lot for this convenient port, I was just about to do the same thing. I've added a couple of more extras, as well as one custom tag to make it more usable and packed it in a standalone django application. If you agree to it, I will host my application somewhere in the open - google code perhaps.
#
Hey Atilla - please go ahead!
(I'm just thinking about sitting down to code an Admob Analytic Django app)
#
Please login first before commenting.