import datetime
import gzip
import uuid
# import cProfile, pstats, io
import uwsgi
from urllib.parse import parse_qs
import errors as exceptions
from handlers import bid, impression, install, click, win
import timers
import settings
from logger import log
def application(env, start_response):
#pr = cProfile.Profile()
#pr.enable()
# ... do something ...
uwsgi.set_logvar('request_id', str(uuid.uuid4()))
now = datetime.datetime.utcnow()
response = ""
path = env['PATH_INFO']
payload = env['wsgi.input'].read()
if env.get("HTTP_CONTENT_ENCODING", "") == "gzip":
payload = gzip.decompress(payload)
# log.debug(payload.decode("utf8"))
if path.startswith("/bidrequest/"):
try:
# if settings.SLOWDOWN:
# slowdown()
ssp_name = path[len("/bidrequest/"):]
response = bid.handle_request(ssp_name, payload, now)
start_response('200 OK', [('Content-Type','application/json; charset=utf-8'), ('Content-Length', str(len(response)))])
except UnicodeDecodeError:
start_response('204 No Content', [('Content-Length', '0')])
except exceptions.NoBid:
start_response('204 No Content', [('Content-Length', '0')])
except exceptions.NoBidBase:
start_response('204 No Content', [('Content-Length', '0')])
except ValueError as e:
log.warn(f"Invalid JSON: {e}")
start_response('204 No Content', [('Content-Length', '0')])
elif path.startswith("/trk/v2/win"):
try:
params = parse_qs(env['QUERY_STRING'])
auction_id = params.get("auction_id")[0]
price = params.get("auction_price")[0]
timestamp = params.get("time")[0]
win.handle_request(auction_id, price, timestamp)
start_response('200 Ok', [('Content-Length', '0')])
except KeyError:
log.warn(f"Bad event: {path} - {env['QUERY_STRING']}")
start_response('200 Ok', [('Content-Length', '0')])
response = ""
elif path.startswith("/trk/v3/impression"):
try:
params = parse_qs(env['QUERY_STRING'])
price = params.get("auction_price")[0]
auction_id = params.get("auction_id")[0]
timestamp = params.get("time")[0]
impression.handle_request(auction_id, price, timestamp)
start_response('200 Ok', [('Content-Length', '0')])
except KeyError:
try:
params = parse_qs(env['QUERY_STRING'])
timestamp = params.get("time")[0]
delta = datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(int(timestamp))
log.warn(f"Bad event: {path} {auction_id} days: {delta.days}")
start_response('200 Ok', [('Content-Length', '0')])
except TypeError:
log.warn(f"Bad event: {path}")
start_response('200 Ok', [('Content-Length', '0')])
response = ""
elif path.startswith("/trk/v3/click"):
try:
params = parse_qs(env['QUERY_STRING'])
auction_id = params.get("auction_id")[0]
timestamp = params.get("time")[0]
market_link = click.handle_request(auction_id, timestamp)
log.debug(f"Redirected to: {market_link}")
start_response('302 Found', [('Content-Length', '0'), ('Location', market_link)])
except KeyError:
try:
params = parse_qs(env['QUERY_STRING'])
auction_id = params.get("auction_id")[0]
timestamp = params.get("time")[0]
delta = datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(int(timestamp))
log.warn(f"Bad event: {path} {auction_id} days: {delta.days}")
start_response('200 Ok', [('Content-Length', '0')])
except TypeError:
log.warn(f"Bad event: {path}")
start_response('200 Ok', [('Content-Length', '0')])
response = ""
elif path.startswith("/trk/v4/click"):
try:
params = parse_qs(env['QUERY_STRING'])
auction_id = params.get("auction_id")[0]
timestamp = params.get("time")[0]
market_link = click.handle_request(auction_id, timestamp)
start_response('200 Ok', [('Content-Length', '0')])
except KeyError:
try:
params = parse_qs(env['QUERY_STRING'])
auction_id = params.get("auction_id")[0]
timestamp = params.get("time")[0]
delta = datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(int(timestamp))
log.warn(f"Bad event: {path} {auction_id} days: {delta.days}")
start_response('200 Ok', [('Content-Length', '0')])
except TypeError:
log.warn(f"Bad event: {path}")
start_response('200 Ok', [('Content-Length', '0')])
response = ""
elif path.startswith("/install") or path.startswith("//install"):
try:
params = parse_qs(env['QUERY_STRING'])
revenue = params.get("revenue")[0]
auction_id = params.get("postback-parameter")[0]
install.handle_request(auction_id, revenue)
start_response('200 Ok', [('Content-Length', '0')])
except KeyError:
log.warn(f"Bad event: {path}")
start_response('200 Ok', [('Content-Length', '0')])
response = ""
else:
start_response('200 Ok', [('Content-Length', '0')])
#pr.disable()
#s = io.StringIO()
#sortby = 'cumulative'
#ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
#ps.print_stats()
#print(s.getvalue())
yield response